Skip to content

Instantly share code, notes, and snippets.

@s1monw1
Last active September 15, 2022 20:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save s1monw1/66159735830b7362a6d79afb18a152c4 to your computer and use it in GitHub Desktop.
Save s1monw1/66159735830b7362a6d79afb18a152c4 to your computer and use it in GitHub Desktop.
object GitHubApiCaller {
private val client = OkHttpClient()
private var cachedLeadResults =
mutableMapOf<String, Contributor>()
private val mapper = jacksonObjectMapper()
/**
* Invokes Github API and looks for a match based on inpit `name`
**/
@Synchronized
fun getKotlinContributor(name: String): Contributor {
val cachedLeadResult = cachedLeadResults[name]
if (cachedLeadResult != null) {
LOG.debug("return cached: $cachedLeadResult")
return cachedLeadResult
}
val request = Request.Builder().url(ENDPOINT).build()
val response = client.newCall(request).execute()
val responseAsString = response.use {
val responseBytes = it.body()?.source()?.readByteArray()
if (responseBytes != null) {
String(responseBytes)
} else throw IllegalStateException("No response from server!")
}
LOG.debug("response from git api: $responseAsString\n")
val contributors =
mapper.readValue(responseAsString)
val match = contributors.first { it.login == name }
this.cachedLeadResults[name] = match
LOG.debug("found kotlin contributor: $match")
return match
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment