Object mapper for http utils is now created once instead of per request.

This commit is contained in:
Clinton Alexander 2016-12-19 14:53:08 +00:00
parent 9f9fa1de1c
commit ba5934e3e1

View File

@ -18,6 +18,9 @@ object HttpUtils {
.connectTimeout(5, TimeUnit.SECONDS) .connectTimeout(5, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS).build() .readTimeout(60, TimeUnit.SECONDS).build()
} }
val defaultMapper: ObjectMapper by lazy {
ObjectMapper().registerModule(JsonSupport.createJavaTimeModule()).registerModule(KotlinModule())
}
fun putJson(url: URL, data: String) : Boolean { fun putJson(url: URL, data: String) : Boolean {
val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data) val body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), data)
@ -32,7 +35,7 @@ object HttpUtils {
inline fun<reified T: Any> getJson(url: URL, params: Map<String, String> = mapOf()) : T { inline fun<reified T: Any> getJson(url: URL, params: Map<String, String> = mapOf()) : T {
val paramString = if(params.isEmpty()) "" else "?" + params.map { "${it.key}=${it.value}" }.joinToString("&") val paramString = if(params.isEmpty()) "" else "?" + params.map { "${it.key}=${it.value}" }.joinToString("&")
val parameterisedUrl = URL(url.toExternalForm() + paramString) val parameterisedUrl = URL(url.toExternalForm() + paramString)
return createDefaultMapper().readValue(parameterisedUrl, T::class.java) return defaultMapper.readValue(parameterisedUrl, T::class.java)
} }
private fun makeRequest(request: Request): Boolean { private fun makeRequest(request: Request): Boolean {
@ -44,8 +47,4 @@ object HttpUtils {
return response.isSuccessful return response.isSuccessful
} }
fun createDefaultMapper(): ObjectMapper {
return ObjectMapper().registerModule(JsonSupport.createJavaTimeModule()).registerModule(KotlinModule())
}
} }