Skip to content

Commit

Permalink
style(internal): improve terseness of handlers (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Nov 1, 2024
1 parent 7fd02f6 commit ac87a78
Showing 1 changed file with 17 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,16 @@ private object EmptyHandler : Handler<Void?> {

internal fun stringHandler(): Handler<String> = StringHandler

internal fun binaryHandler(): Handler<BinaryResponseContent> = BinaryHandler

private object StringHandler : Handler<String> {
override fun handle(response: HttpResponse): String {
return response.body().readBytes().toString(Charsets.UTF_8)
}
override fun handle(response: HttpResponse): String =
response.body().readBytes().toString(Charsets.UTF_8)
}

internal fun binaryHandler(): Handler<BinaryResponseContent> = BinaryHandler

private object BinaryHandler : Handler<BinaryResponseContent> {
override fun handle(response: HttpResponse): BinaryResponseContent {
return object : BinaryResponseContent {
override fun handle(response: HttpResponse): BinaryResponseContent =
object : BinaryResponseContent {
override fun contentType(): String? =
response.headers().get("Content-Type").firstOrNull()

Expand All @@ -52,11 +51,10 @@ private object BinaryHandler : Handler<BinaryResponseContent> {
response.body().copyTo(outputStream)
}
}
}
}

internal inline fun <reified T> jsonHandler(jsonMapper: JsonMapper): Handler<T> {
return object : Handler<T> {
internal inline fun <reified T> jsonHandler(jsonMapper: JsonMapper): Handler<T> =
object : Handler<T> {
override fun handle(response: HttpResponse): T {
try {
return jsonMapper.readValue(response.body(), jacksonTypeRef())
Expand All @@ -65,24 +63,22 @@ internal inline fun <reified T> jsonHandler(jsonMapper: JsonMapper): Handler<T>
}
}
}
}

internal fun errorHandler(jsonMapper: JsonMapper): Handler<LithicError> {
val handler = jsonHandler<LithicError>(jsonMapper)

return object : Handler<LithicError> {
override fun handle(response: HttpResponse): LithicError {
override fun handle(response: HttpResponse): LithicError =
try {
return handler.handle(response)
handler.handle(response)
} catch (e: Exception) {
return LithicError.builder().build()
LithicError.builder().build()
}
}
}
}

internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<LithicError>): Handler<T> {
return object : Handler<T> {
internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<LithicError>): Handler<T> =
object : Handler<T> {
override fun handle(response: HttpResponse): T {
when (val statusCode = response.statusCode()) {
in 200..299 -> {
Expand Down Expand Up @@ -157,26 +153,17 @@ internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<LithicError>)
}
}
}
}

private fun HttpResponse.buffered(): HttpResponse {
val body = body().readBytes()

return object : HttpResponse {
override fun statusCode(): Int {
return this@buffered.statusCode()
}
override fun statusCode(): Int = this@buffered.statusCode()

override fun headers(): ListMultimap<String, String> {
return this@buffered.headers()
}
override fun headers(): ListMultimap<String, String> = this@buffered.headers()

override fun body(): InputStream {
return ByteArrayInputStream(body)
}
override fun body(): InputStream = ByteArrayInputStream(body)

override fun close() {
this@buffered.close()
}
override fun close() = this@buffered.close()
}
}

0 comments on commit ac87a78

Please sign in to comment.