@@ -4,6 +4,7 @@ package com.tryfinch.api.services
44
55import com.fasterxml.jackson.databind.json.JsonMapper
66import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
7+ import com.tryfinch.api.core.http.BinaryResponseContent
78import com.tryfinch.api.core.http.HttpResponse
89import com.tryfinch.api.core.http.HttpResponse.Handler
910import com.tryfinch.api.errors.BadRequestException
@@ -16,6 +17,8 @@ import com.tryfinch.api.errors.RateLimitException
1617import com.tryfinch.api.errors.UnauthorizedException
1718import com.tryfinch.api.errors.UnexpectedStatusCodeException
1819import com.tryfinch.api.errors.UnprocessableEntityException
20+ import java.io.InputStream
21+ import java.io.OutputStream
1922
2023@JvmSynthetic internal fun emptyHandler (): Handler <Void ?> = EmptyHandler
2124
@@ -25,12 +28,20 @@ private object EmptyHandler : Handler<Void?> {
2528
2629@JvmSynthetic internal fun stringHandler (): Handler <String > = StringHandler
2730
31+ @JvmSynthetic internal fun binaryHandler (): Handler <BinaryResponseContent > = BinaryHandler
32+
2833private object StringHandler : Handler<String> {
2934 override fun handle (response : HttpResponse ): String {
3035 return response.body().readBytes().toString(Charsets .UTF_8 )
3136 }
3237}
3338
39+ private object BinaryHandler : Handler<BinaryResponseContent> {
40+ override fun handle (response : HttpResponse ): BinaryResponseContent {
41+ return BinaryResponseContentImpl (response)
42+ }
43+ }
44+
3445@JvmSynthetic
3546internal inline fun <reified T > jsonHandler (jsonMapper : JsonMapper ): Handler <T > {
3647 return object : Handler <T > {
@@ -96,3 +107,24 @@ internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<FinchError>):
96107 }
97108 }
98109}
110+
111+ class BinaryResponseContentImpl
112+ constructor (
113+ private val response: HttpResponse ,
114+ ) : BinaryResponseContent {
115+ override fun contentType (): String? {
116+ return response.headers().get(" Content-Type" ).firstOrNull()
117+ }
118+
119+ override fun body (): InputStream {
120+ return response.body()
121+ }
122+
123+ override fun writeTo (outputStream : OutputStream ) {
124+ response.body().copyTo(outputStream)
125+ }
126+
127+ override fun close () {
128+ response.body().close()
129+ }
130+ }
0 commit comments