Skip to content

Commit 1fef2a8

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
feat(api): api update (#15)
1 parent 0b07330 commit 1fef2a8

File tree

123 files changed

+454
-338
lines changed

Some content is hidden

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

123 files changed

+454
-338
lines changed

onebusaway-sdk-kotlin-client-okhttp/src/main/kotlin/org/onebusaway/client/okhttp/OkHttpClient.kt

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
8080

8181
private fun HttpRequest.toRequest(): Request {
8282
var body: RequestBody? = body?.toRequestBody()
83-
// OkHttpClient always requires a request body for PUT and POST methods
83+
// OkHttpClient always requires a request body for PUT and POST methods.
8484
if (body == null && (method == HttpMethod.PUT || method == HttpMethod.POST)) {
8585
body = "".toRequestBody()
8686
}
@@ -108,43 +108,27 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
108108
val length = contentLength()
109109

110110
return object : RequestBody() {
111-
override fun contentType(): MediaType? {
112-
return mediaType
113-
}
111+
override fun contentType(): MediaType? = mediaType
114112

115-
override fun contentLength(): Long {
116-
return length
117-
}
113+
override fun contentLength(): Long = length
118114

119-
override fun isOneShot(): Boolean {
120-
return !repeatable()
121-
}
115+
override fun isOneShot(): Boolean = !repeatable()
122116

123-
override fun writeTo(sink: BufferedSink) {
124-
writeTo(sink.outputStream())
125-
}
117+
override fun writeTo(sink: BufferedSink) = writeTo(sink.outputStream())
126118
}
127119
}
128120

129121
private fun Response.toResponse(): HttpResponse {
130122
val headers = headers.toHeaders()
131123

132124
return object : HttpResponse {
133-
override fun statusCode(): Int {
134-
return code
135-
}
125+
override fun statusCode(): Int = code
136126

137-
override fun headers(): ListMultimap<String, String> {
138-
return headers
139-
}
127+
override fun headers(): ListMultimap<String, String> = headers
140128

141-
override fun body(): InputStream {
142-
return body!!.byteStream()
143-
}
129+
override fun body(): InputStream = body!!.byteStream()
144130

145-
override fun close() {
146-
body!!.close()
147-
}
131+
override fun close() = body!!.close()
148132
}
149133
}
150134

@@ -153,9 +137,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
153137
MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
154138
.arrayListValues()
155139
.build<String, String>()
156-
157140
forEach { pair -> headers.put(pair.first, pair.second) }
158-
159141
return headers
160142
}
161143

@@ -166,7 +148,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
166148
class Builder {
167149

168150
private var baseUrl: HttpUrl? = null
169-
// default timeout is 1 minute
151+
// The default timeout is 1 minute.
170152
private var timeout: Duration = Duration.ofSeconds(60)
171153
private var proxy: Proxy? = null
172154

@@ -176,8 +158,8 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
176158

177159
fun proxy(proxy: Proxy?) = apply { this.proxy = proxy }
178160

179-
fun build(): OkHttpClient {
180-
return OkHttpClient(
161+
fun build(): OkHttpClient =
162+
OkHttpClient(
181163
okhttp3.OkHttpClient.Builder()
182164
.connectTimeout(timeout)
183165
.readTimeout(timeout)
@@ -187,7 +169,6 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
187169
.build(),
188170
checkNotNull(baseUrl) { "`baseUrl` is required but was not set" },
189171
)
190-
}
191172
}
192173

193174
private suspend fun Call.executeAsync(): Response =

onebusaway-sdk-kotlin-client-okhttp/src/main/kotlin/org/onebusaway/client/okhttp/OnebusawaySdkOkHttpClient.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class OnebusawaySdkOkHttpClient private constructor() {
2323

2424
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
2525
private var baseUrl: String = ClientOptions.PRODUCTION_URL
26-
// default timeout for client is 1 minute
26+
// The default timeout for the client is 1 minute.
2727
private var timeout: Duration = Duration.ofSeconds(60)
2828
private var proxy: Proxy? = null
2929

@@ -66,8 +66,8 @@ class OnebusawaySdkOkHttpClient private constructor() {
6666

6767
fun fromEnv() = apply { clientOptions.fromEnv() }
6868

69-
fun build(): OnebusawaySdkClient {
70-
return OnebusawaySdkClientImpl(
69+
fun build(): OnebusawaySdkClient =
70+
OnebusawaySdkClientImpl(
7171
clientOptions
7272
.httpClient(
7373
OkHttpClient.builder()
@@ -78,6 +78,5 @@ class OnebusawaySdkOkHttpClient private constructor() {
7878
)
7979
.build()
8080
)
81-
}
8281
}
8382
}

onebusaway-sdk-kotlin-client-okhttp/src/main/kotlin/org/onebusaway/client/okhttp/OnebusawaySdkOkHttpClientAsync.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
2323

2424
private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
2525
private var baseUrl: String = ClientOptions.PRODUCTION_URL
26-
// default timeout for client is 1 minute
26+
// The default timeout for the client is 1 minute.
2727
private var timeout: Duration = Duration.ofSeconds(60)
2828
private var proxy: Proxy? = null
2929

@@ -66,8 +66,8 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
6666

6767
fun fromEnv() = apply { clientOptions.fromEnv() }
6868

69-
fun build(): OnebusawaySdkClientAsync {
70-
return OnebusawaySdkClientAsyncImpl(
69+
fun build(): OnebusawaySdkClientAsync =
70+
OnebusawaySdkClientAsyncImpl(
7171
clientOptions
7272
.httpClient(
7373
OkHttpClient.builder()
@@ -78,6 +78,5 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
7878
)
7979
.build()
8080
)
81-
}
8281
}
8382
}

onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/client/OnebusawaySdkClient.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// File generated from our OpenAPI spec by Stainless.
22

3-
@file:Suppress("OVERLOADS_INTERFACE") // See https://youtrack.jetbrains.com/issue/KT-36102
4-
53
package org.onebusaway.client
64

75
import org.onebusaway.models.*

onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/client/OnebusawaySdkClientAsync.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// File generated from our OpenAPI spec by Stainless.
22

3-
@file:Suppress("OVERLOADS_INTERFACE") // See https://youtrack.jetbrains.com/issue/KT-36102
4-
53
package org.onebusaway.client
64

75
import org.onebusaway.models.*

onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/client/OnebusawaySdkClientAsyncImpl.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@
33
package org.onebusaway.client
44

55
import org.onebusaway.core.ClientOptions
6-
import org.onebusaway.core.http.HttpResponse.Handler
7-
import org.onebusaway.errors.OnebusawaySdkError
86
import org.onebusaway.models.*
97
import org.onebusaway.services.async.*
10-
import org.onebusaway.services.errorHandler
118

129
class OnebusawaySdkClientAsyncImpl
1310
constructor(
1411
private val clientOptions: ClientOptions,
1512
) : OnebusawaySdkClientAsync {
1613

17-
private val errorHandler: Handler<OnebusawaySdkError> = errorHandler(clientOptions.jsonMapper)
18-
1914
private val sync: OnebusawaySdkClient by lazy { OnebusawaySdkClientImpl(clientOptions) }
2015

2116
private val agenciesWithCoverage: AgenciesWithCoverageServiceAsync by lazy {

onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/client/OnebusawaySdkClientImpl.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,14 @@
33
package org.onebusaway.client
44

55
import org.onebusaway.core.ClientOptions
6-
import org.onebusaway.core.http.HttpResponse.Handler
7-
import org.onebusaway.errors.OnebusawaySdkError
86
import org.onebusaway.models.*
97
import org.onebusaway.services.blocking.*
10-
import org.onebusaway.services.errorHandler
118

129
class OnebusawaySdkClientImpl
1310
constructor(
1411
private val clientOptions: ClientOptions,
1512
) : OnebusawaySdkClient {
1613

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

onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/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")

onebusaway-sdk-kotlin-core/src/main/kotlin/org/onebusaway/core/Values.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -475,9 +475,8 @@ internal constructor(
475475
}
476476
}
477477

478-
override fun toString(): String {
479-
return "MultipartFormValue(name='$name', contentType=$contentType, filename=$filename, value=${valueToString()})"
480-
}
478+
override fun toString(): String =
479+
"MultipartFormValue{name=$name, contentType=$contentType, filename=$filename, value=${valueToString()}}"
481480

482481
private fun valueToString(): String =
483482
when (value) {

0 commit comments

Comments
 (0)