Skip to content

Instantly share code, notes, and snippets.

@hirokazumiyaji
Created February 2, 2018 02:50
Show Gist options
  • Save hirokazumiyaji/6242a7e364f052cb452d7cff0d3ea937 to your computer and use it in GitHub Desktop.
Save hirokazumiyaji/6242a7e364f052cb452d7cff0d3ea937 to your computer and use it in GitHub Desktop.
package http;
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
import com.fasterxml.jackson.module.kotlin.readValue
import okhttp3.OkHttpClient
import okhttp3.Request
import reactor.core.publisher.Mono
class Client(val httpClient: OkHttpClient = OkHttpClient(), val mapper: ObjectMapper = jacksonObjectMapper()) {
inline fun <reified T : Any> request(req: Request): Mono<T> {
return Mono.just(httpClient.newCall(req).execute())
.doOnSuccess {
if (!it.isSuccessful) {
throw Exception("request error.", it)
}
}.map {
mapper.readValue<T>(response.body()?.string() ?: "")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment