Skip to content

Commit

Permalink
style(internal): improve terseness of handlers
Browse files Browse the repository at this point in the history
chore: unknown commit message
  • Loading branch information
stainless-bot authored and Stainless Bot committed Oct 14, 2024
1 parent c49334f commit 3989402
Showing 1 changed file with 17 additions and 30 deletions.
47 changes: 17 additions & 30 deletions openai-java-core/src/main/kotlin/com/openai/services/Handlers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,16 @@ private object EmptyHandler : Handler<Void?> {

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

@JvmSynthetic 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)
}

@JvmSynthetic 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(): Optional<String> =
Optional.ofNullable(response.headers().get("Content-Type").firstOrNull())

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

@JvmSynthetic
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 @@ -67,26 +65,24 @@ internal inline fun <reified T> jsonHandler(jsonMapper: JsonMapper): Handler<T>
}
}
}
}

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

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

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

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 3989402

Please sign in to comment.