Skip to content

Commit fbabaaa

Browse files
feat: add HttpRequestBody.contentLength() (#174)
1 parent cf34132 commit fbabaaa

3 files changed

Lines changed: 27 additions & 2 deletions

File tree

finch-java-client-okhttp/src/main/kotlin/com/tryfinch/api/client/okhttp/OkHttpClient.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,12 +115,17 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
115115

116116
private fun HttpRequestBody.toRequestBody(): RequestBody {
117117
val mediaType = contentType()?.toMediaType()
118+
val length = contentLength()
118119

119120
return object : RequestBody() {
120121
override fun contentType(): MediaType? {
121122
return mediaType
122123
}
123124

125+
override fun contentLength(): Long {
126+
return length
127+
}
128+
124129
override fun isOneShot(): Boolean {
125130
return !repeatable()
126131
}

finch-java-core/src/main/kotlin/com/tryfinch/api/core/http/HttpRequestBody.kt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ interface HttpRequestBody : Closeable {
1010

1111
fun contentType(): String?
1212

13+
fun contentLength(): Long
14+
1315
/**
1416
* Determines if a request can be repeated in a meaningful way, for example before doing a
1517
* retry.

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package com.tryfinch.api.services
55
import com.fasterxml.jackson.databind.json.JsonMapper
66
import com.tryfinch.api.core.http.HttpRequestBody
77
import com.tryfinch.api.errors.FinchException
8+
import java.io.ByteArrayOutputStream
89
import java.io.OutputStream
910
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
1011

@@ -14,16 +15,31 @@ internal inline fun <reified T> json(
1415
value: T,
1516
): HttpRequestBody {
1617
return object : HttpRequestBody {
17-
override fun writeTo(outputStream: OutputStream) {
18+
private var cachedBytes: ByteArray? = null
19+
20+
private fun serialize(): ByteArray {
21+
if (cachedBytes != null) return cachedBytes!!
22+
23+
val buffer = ByteArrayOutputStream()
1824
try {
19-
return jsonMapper.writeValue(outputStream, value)
25+
jsonMapper.writeValue(buffer, value)
26+
cachedBytes = buffer.toByteArray()
27+
return cachedBytes!!
2028
} catch (e: Exception) {
2129
throw FinchException("Error writing request", e)
2230
}
2331
}
2432

33+
override fun writeTo(outputStream: OutputStream) {
34+
outputStream.write(serialize())
35+
}
36+
2537
override fun contentType(): String = "application/json"
2638

39+
override fun contentLength(): Long {
40+
return serialize().size.toLong()
41+
}
42+
2743
override fun repeatable(): Boolean = true
2844

2945
override fun close() {}
@@ -49,6 +65,8 @@ internal fun multipartFormData(
4965

5066
override fun contentType(): String = entity.contentType
5167

68+
override fun contentLength(): Long = -1
69+
5270
override fun repeatable(): Boolean = entity.isRepeatable
5371

5472
override fun close() = entity.close()

0 commit comments

Comments
 (0)