Skip to content

Commit a3b6b45

Browse files
committed
feat(client): allow binary returns (#99)
1 parent ae6f32b commit a3b6b45

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.tryfinch.api.core.http
2+
3+
import java.io.Closeable
4+
import java.io.IOException
5+
import java.io.InputStream
6+
import java.io.OutputStream
7+
8+
interface BinaryResponseContent : Closeable {
9+
10+
fun contentType(): String?
11+
12+
fun body(): InputStream
13+
14+
@Throws(IOException::class) fun writeTo(outputStream: OutputStream)
15+
}

finch-java-core/src/main/kotlin/com/tryfinch/api/services/Handlers.kt

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package com.tryfinch.api.services
44

55
import com.fasterxml.jackson.databind.json.JsonMapper
66
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
7+
import com.tryfinch.api.core.http.BinaryResponseContent
78
import com.tryfinch.api.core.http.HttpResponse
89
import com.tryfinch.api.core.http.HttpResponse.Handler
910
import com.tryfinch.api.errors.BadRequestException
@@ -16,6 +17,8 @@ import com.tryfinch.api.errors.RateLimitException
1617
import com.tryfinch.api.errors.UnauthorizedException
1718
import com.tryfinch.api.errors.UnexpectedStatusCodeException
1819
import 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+
2833
private 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
3546
internal 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

Comments
 (0)