Created
January 11, 2020 22:32
-
-
Save brunapereira/cb01830359f5aa21a3d94af0b563481d 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
abstract class MicroserviceGateway { | |
abstract var webClient: WebClient | |
// outros métodos HTTP | |
fun <ResponseT : Any> get( | |
url: String, | |
responseClass: KClass<ResponseT>, | |
queryParameters: Map<String, String> = emptyMap(), | |
headers: Map<String, String> = emptyMap() | |
): ResponseT { | |
return webClient.get() | |
.uri { uriBuilder -> createUrl(uriBuilder, url, queryParameters) } | |
.headers { httpHeaders -> addHeaders(httpHeaders, headers) } | |
.retrieve() | |
.also { response -> handleErrors(response) } | |
.bodyToMono(responseClass.java) | |
.block() as ResponseT | |
} | |
private fun handleErrors(response: WebClient.ResponseSpec): WebClient.ResponseSpec { | |
// handle errors | |
} | |
private fun createUrl(uriBuilder: UriBuilder, url: String, queryParameters: Map<String, String>): URI { | |
uriBuilder.path(url) | |
queryParameters.forEach { (key, value) -> uriBuilder.queryParam(key, value) } | |
return uriBuilder.build() | |
} | |
private fun addHeaders(httpHeaders: HttpHeaders, headers: Map<String, String>): HttpHeaders { | |
print(headers) | |
headers.forEach { (key, value) -> httpHeaders.add(key, value) } | |
return httpHeaders | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment