Skip to content

Commit 6571db4

Browse files
Stainless Botstainless-app[bot]
authored andcommitted
feat(api): api update (#314)
1 parent 81cda11 commit 6571db4

201 files changed

Lines changed: 4941 additions & 8257 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.stats.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
configured_endpoints: 38
2-
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/finch%2Ffinch-811587ac4ba3634e1b0dd781c54c7a0cb302d612f719036f9c9683166c7197cd.yml
2+
openapi_spec_url: https://storage.googleapis.com/stainless-sdk-openapi-specs/finch%2Ffinch-612b573cabcee39c562dc91f5b185e2a0bfdce3a262618f0098602af2199af67.yml

buildSrc/src/main/kotlin/finch.java.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ configure<SpotlessExtension> {
2121
importOrder()
2222
removeUnusedImports()
2323
palantirJavaFormat()
24+
toggleOffOn()
2425
}
2526
}
2627

buildSrc/src/main/kotlin/finch.kotlin.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ kotlin {
1515
configure<SpotlessExtension> {
1616
kotlin {
1717
ktfmt().kotlinlangStyle()
18+
toggleOffOn()
1819
}
1920
}
2021

finch-java-core/src/main/kotlin/com/tryfinch/api/client/FinchClientAsyncImpl.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.tryfinch.api.core.http.HttpRequest
99
import com.tryfinch.api.core.http.HttpResponse.Handler
1010
import com.tryfinch.api.errors.FinchError
1111
import com.tryfinch.api.errors.FinchException
12+
import com.tryfinch.api.models.*
1213
import com.tryfinch.api.services.async.*
1314
import com.tryfinch.api.services.errorHandler
1415
import com.tryfinch.api.services.json
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
@file:JvmName("HttpRequestBodies")
2+
3+
package com.tryfinch.api.core
4+
5+
import com.fasterxml.jackson.databind.json.JsonMapper
6+
import com.tryfinch.api.core.http.HttpRequestBody
7+
import com.tryfinch.api.errors.FinchException
8+
import java.io.ByteArrayOutputStream
9+
import java.io.OutputStream
10+
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
11+
12+
@JvmSynthetic
13+
internal inline fun <reified T> json(
14+
jsonMapper: JsonMapper,
15+
value: T,
16+
): HttpRequestBody {
17+
return object : HttpRequestBody {
18+
private var cachedBytes: ByteArray? = null
19+
20+
private fun serialize(): ByteArray {
21+
if (cachedBytes != null) return cachedBytes!!
22+
23+
val buffer = ByteArrayOutputStream()
24+
try {
25+
jsonMapper.writeValue(buffer, value)
26+
cachedBytes = buffer.toByteArray()
27+
return cachedBytes!!
28+
} catch (e: Exception) {
29+
throw FinchException("Error writing request", e)
30+
}
31+
}
32+
33+
override fun writeTo(outputStream: OutputStream) {
34+
outputStream.write(serialize())
35+
}
36+
37+
override fun contentType(): String = "application/json"
38+
39+
override fun contentLength(): Long {
40+
return serialize().size.toLong()
41+
}
42+
43+
override fun repeatable(): Boolean = true
44+
45+
override fun close() {}
46+
}
47+
}
48+
49+
@JvmSynthetic
50+
internal fun multipartFormData(
51+
jsonMapper: JsonMapper,
52+
parts: Array<MultipartFormValue<*>?>
53+
): HttpRequestBody {
54+
val builder = MultipartEntityBuilder.create()
55+
parts.forEach { part ->
56+
if (part?.value != null) {
57+
when (part.value) {
58+
is JsonValue -> {
59+
val buffer = ByteArrayOutputStream()
60+
try {
61+
jsonMapper.writeValue(buffer, part.value)
62+
} catch (e: Exception) {
63+
throw FinchException("Error serializing value to json", e)
64+
}
65+
builder.addBinaryBody(
66+
part.name,
67+
buffer.toByteArray(),
68+
part.contentType,
69+
part.filename
70+
)
71+
}
72+
is Boolean ->
73+
builder.addTextBody(
74+
part.name,
75+
if (part.value) "true" else "false",
76+
part.contentType
77+
)
78+
is Int -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
79+
is Long -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
80+
is Double -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
81+
is ByteArray ->
82+
builder.addBinaryBody(part.name, part.value, part.contentType, part.filename)
83+
is String -> builder.addTextBody(part.name, part.value, part.contentType)
84+
is Enum -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
85+
else ->
86+
throw IllegalArgumentException(
87+
"Unsupported content type: ${part.value::class.java.simpleName}"
88+
)
89+
}
90+
}
91+
}
92+
val entity = builder.build()
93+
94+
return object : HttpRequestBody {
95+
override fun writeTo(outputStream: OutputStream) {
96+
try {
97+
return entity.writeTo(outputStream)
98+
} catch (e: Exception) {
99+
throw FinchException("Error writing request", e)
100+
}
101+
}
102+
103+
override fun contentType(): String = entity.contentType
104+
105+
override fun contentLength(): Long = -1
106+
107+
override fun repeatable(): Boolean = entity.isRepeatable
108+
109+
override fun close() = entity.close()
110+
}
111+
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,9 @@ fun getOsName(): String {
3434
}
3535
}
3636

37-
fun getOsVersion(): String {
38-
return System.getProperty("os.version", "unknown")
39-
}
37+
fun getOsVersion(): String = System.getProperty("os.version", "unknown")
4038

41-
fun getPackageVersion(): String {
42-
return Properties::class.java.`package`.implementationVersion ?: "unknown"
43-
}
39+
fun getPackageVersion(): String =
40+
Properties::class.java.`package`.implementationVersion ?: "unknown"
4441

45-
fun getJavaVersion(): String {
46-
return System.getProperty("java.version", "unknown")
47-
}
42+
fun getJavaVersion(): String = System.getProperty("java.version", "unknown")
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
@file:JvmName("ErrorHandler")
2+
3+
package com.tryfinch.api.core.handlers
4+
5+
import com.fasterxml.jackson.databind.json.JsonMapper
6+
import com.google.common.collect.ListMultimap
7+
import com.tryfinch.api.core.http.HttpResponse
8+
import com.tryfinch.api.core.http.HttpResponse.Handler
9+
import com.tryfinch.api.errors.BadRequestException
10+
import com.tryfinch.api.errors.FinchError
11+
import com.tryfinch.api.errors.InternalServerException
12+
import com.tryfinch.api.errors.NotFoundException
13+
import com.tryfinch.api.errors.PermissionDeniedException
14+
import com.tryfinch.api.errors.RateLimitException
15+
import com.tryfinch.api.errors.UnauthorizedException
16+
import com.tryfinch.api.errors.UnexpectedStatusCodeException
17+
import com.tryfinch.api.errors.UnprocessableEntityException
18+
import java.io.ByteArrayInputStream
19+
import java.io.InputStream
20+
21+
@JvmSynthetic
22+
internal fun errorHandler(jsonMapper: JsonMapper): Handler<FinchError> {
23+
val handler = jsonHandler<FinchError>(jsonMapper)
24+
25+
return object : Handler<FinchError> {
26+
override fun handle(response: HttpResponse): FinchError =
27+
try {
28+
handler.handle(response)
29+
} catch (e: Exception) {
30+
FinchError.builder().build()
31+
}
32+
}
33+
}
34+
35+
@JvmSynthetic
36+
internal fun <T> Handler<T>.withErrorHandler(errorHandler: Handler<FinchError>): Handler<T> =
37+
object : Handler<T> {
38+
override fun handle(response: HttpResponse): T {
39+
when (val statusCode = response.statusCode()) {
40+
in 200..299 -> {
41+
return this@withErrorHandler.handle(response)
42+
}
43+
400 -> {
44+
val buffered = response.buffered()
45+
throw BadRequestException(
46+
buffered.headers(),
47+
stringHandler().handle(buffered),
48+
errorHandler.handle(buffered),
49+
)
50+
}
51+
401 -> {
52+
val buffered = response.buffered()
53+
throw UnauthorizedException(
54+
buffered.headers(),
55+
stringHandler().handle(buffered),
56+
errorHandler.handle(buffered),
57+
)
58+
}
59+
403 -> {
60+
val buffered = response.buffered()
61+
throw PermissionDeniedException(
62+
buffered.headers(),
63+
stringHandler().handle(buffered),
64+
errorHandler.handle(buffered),
65+
)
66+
}
67+
404 -> {
68+
val buffered = response.buffered()
69+
throw NotFoundException(
70+
buffered.headers(),
71+
stringHandler().handle(buffered),
72+
errorHandler.handle(buffered),
73+
)
74+
}
75+
422 -> {
76+
val buffered = response.buffered()
77+
throw UnprocessableEntityException(
78+
buffered.headers(),
79+
stringHandler().handle(buffered),
80+
errorHandler.handle(buffered),
81+
)
82+
}
83+
429 -> {
84+
val buffered = response.buffered()
85+
throw RateLimitException(
86+
buffered.headers(),
87+
stringHandler().handle(buffered),
88+
errorHandler.handle(buffered),
89+
)
90+
}
91+
in 500..599 -> {
92+
val buffered = response.buffered()
93+
throw InternalServerException(
94+
statusCode,
95+
buffered.headers(),
96+
stringHandler().handle(buffered),
97+
errorHandler.handle(buffered),
98+
)
99+
}
100+
else -> {
101+
val buffered = response.buffered()
102+
throw UnexpectedStatusCodeException(
103+
statusCode,
104+
buffered.headers(),
105+
stringHandler().handle(buffered),
106+
errorHandler.handle(buffered),
107+
)
108+
}
109+
}
110+
}
111+
}
112+
113+
private fun HttpResponse.buffered(): HttpResponse {
114+
val body = body().readBytes()
115+
116+
return object : HttpResponse {
117+
override fun statusCode(): Int = this@buffered.statusCode()
118+
119+
override fun headers(): ListMultimap<String, String> = this@buffered.headers()
120+
121+
override fun body(): InputStream = ByteArrayInputStream(body)
122+
123+
override fun close() = this@buffered.close()
124+
}
125+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
@file:JvmName("JsonHandler")
2+
3+
package com.tryfinch.api.core.handlers
4+
5+
import com.fasterxml.jackson.databind.json.JsonMapper
6+
import com.fasterxml.jackson.module.kotlin.jacksonTypeRef
7+
import com.tryfinch.api.core.http.HttpResponse
8+
import com.tryfinch.api.core.http.HttpResponse.Handler
9+
import com.tryfinch.api.errors.FinchException
10+
11+
@JvmSynthetic
12+
internal inline fun <reified T> jsonHandler(jsonMapper: JsonMapper): Handler<T> =
13+
object : Handler<T> {
14+
override fun handle(response: HttpResponse): T {
15+
try {
16+
return jsonMapper.readValue(response.body(), jacksonTypeRef())
17+
} catch (e: Exception) {
18+
throw FinchException("Error reading response", e)
19+
}
20+
}
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@file:JvmName("StringHandler")
2+
3+
package com.tryfinch.api.core.handlers
4+
5+
import com.tryfinch.api.core.http.HttpResponse
6+
import com.tryfinch.api.core.http.HttpResponse.Handler
7+
8+
@JvmSynthetic internal fun stringHandler(): Handler<String> = StringHandlerInternal
9+
10+
private object StringHandlerInternal : Handler<String> {
11+
override fun handle(response: HttpResponse): String =
12+
response.body().readBytes().toString(Charsets.UTF_8)
13+
}

finch-java-core/src/main/kotlin/com/tryfinch/api/errors/FinchError.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ constructor(
2626
return true
2727
}
2828

29-
return other is FinchError && this.additionalProperties == other.additionalProperties
29+
return /* spotless:off */ other is FinchError && this.additionalProperties == other.additionalProperties /* spotless:on */
3030
}
3131

3232
override fun hashCode(): Int {
33-
return Objects.hash(additionalProperties)
33+
return /* spotless:off */ Objects.hash(additionalProperties) /* spotless:on */
3434
}
3535

3636
override fun toString() = "FinchError{additionalProperties=$additionalProperties}"

0 commit comments

Comments
 (0)