Skip to content

Instantly share code, notes, and snippets.

@barinek
Created February 26, 2018 04:00
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 barinek/b6729137d05b96439a11c8caae2b901b to your computer and use it in GitHub Desktop.
Save barinek/b6729137d05b96439a11c8caae2b901b to your computer and use it in GitHub Desktop.
open class DiscoveryClient(val mapper: ObjectMapper, val template: RestTemplate) {
private fun <E> List<E>.random(random: java.util.Random): E? = if (size > 0) get(random.nextInt(size)) else null
fun getUrl(appId: String): String? {
val endpoint = System.getenv("DISCOVERY_SERVER_ENDPOINT")
val response = template.get("$endpoint/discovery/apps", "application/json", BasicNameValuePair("appId", appId))
val instances: List<InstanceInfo> = mapper.readValue(response, object : TypeReference<List<InstanceInfo>>() {})
return when {
instances.isEmpty() -> null
else -> instances.random(Random())!!.url
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment