Last active
September 15, 2022 20:49
-
-
Save s1monw1/66159735830b7362a6d79afb18a152c4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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