Created
February 26, 2018 04:00
-
-
Save barinek/b6729137d05b96439a11c8caae2b901b 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
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