Created
February 2, 2018 02:50
-
-
Save hirokazumiyaji/6242a7e364f052cb452d7cff0d3ea937 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
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