Skip to content

Commit

Permalink
feat(api): api update (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
stainless-app[bot] committed Oct 22, 2024
1 parent 0b07330 commit 1707ccb
Show file tree
Hide file tree
Showing 123 changed files with 454 additions and 338 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val

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

return object : RequestBody() {
override fun contentType(): MediaType? {
return mediaType
}
override fun contentType(): MediaType? = mediaType

override fun contentLength(): Long {
return length
}
override fun contentLength(): Long = length

override fun isOneShot(): Boolean {
return !repeatable()
}
override fun isOneShot(): Boolean = !repeatable()

override fun writeTo(sink: BufferedSink) {
writeTo(sink.outputStream())
}
override fun writeTo(sink: BufferedSink) = writeTo(sink.outputStream())
}
}

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

return object : HttpResponse {
override fun statusCode(): Int {
return code
}
override fun statusCode(): Int = code

override fun headers(): ListMultimap<String, String> {
return headers
}
override fun headers(): ListMultimap<String, String> = headers

override fun body(): InputStream {
return body!!.byteStream()
}
override fun body(): InputStream = body!!.byteStream()

override fun close() {
body!!.close()
}
override fun close() = body!!.close()
}
}

Expand All @@ -153,9 +137,7 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
MultimapBuilder.treeKeys(String.CASE_INSENSITIVE_ORDER)
.arrayListValues()
.build<String, String>()

forEach { pair -> headers.put(pair.first, pair.second) }

return headers
}

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

private var baseUrl: HttpUrl? = null
// default timeout is 1 minute
// The default timeout is 1 minute.
private var timeout: Duration = Duration.ofSeconds(60)
private var proxy: Proxy? = null

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

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

fun build(): OkHttpClient {
return OkHttpClient(
fun build(): OkHttpClient =
OkHttpClient(
okhttp3.OkHttpClient.Builder()
.connectTimeout(timeout)
.readTimeout(timeout)
Expand All @@ -187,7 +169,6 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
.build(),
checkNotNull(baseUrl) { "`baseUrl` is required but was not set" },
)
}
}

private suspend fun Call.executeAsync(): Response =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OnebusawaySdkOkHttpClient private constructor() {

private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
private var baseUrl: String = ClientOptions.PRODUCTION_URL
// default timeout for client is 1 minute
// The default timeout for the client is 1 minute.
private var timeout: Duration = Duration.ofSeconds(60)
private var proxy: Proxy? = null

Expand Down Expand Up @@ -66,8 +66,8 @@ class OnebusawaySdkOkHttpClient private constructor() {

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

fun build(): OnebusawaySdkClient {
return OnebusawaySdkClientImpl(
fun build(): OnebusawaySdkClient =
OnebusawaySdkClientImpl(
clientOptions
.httpClient(
OkHttpClient.builder()
Expand All @@ -78,6 +78,5 @@ class OnebusawaySdkOkHttpClient private constructor() {
)
.build()
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {

private var clientOptions: ClientOptions.Builder = ClientOptions.builder()
private var baseUrl: String = ClientOptions.PRODUCTION_URL
// default timeout for client is 1 minute
// The default timeout for the client is 1 minute.
private var timeout: Duration = Duration.ofSeconds(60)
private var proxy: Proxy? = null

Expand Down Expand Up @@ -66,8 +66,8 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {

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

fun build(): OnebusawaySdkClientAsync {
return OnebusawaySdkClientAsyncImpl(
fun build(): OnebusawaySdkClientAsync =
OnebusawaySdkClientAsyncImpl(
clientOptions
.httpClient(
OkHttpClient.builder()
Expand All @@ -78,6 +78,5 @@ class OnebusawaySdkOkHttpClientAsync private constructor() {
)
.build()
)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// File generated from our OpenAPI spec by Stainless.

@file:Suppress("OVERLOADS_INTERFACE") // See https://youtrack.jetbrains.com/issue/KT-36102

package org.onebusaway.client

import org.onebusaway.models.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// File generated from our OpenAPI spec by Stainless.

@file:Suppress("OVERLOADS_INTERFACE") // See https://youtrack.jetbrains.com/issue/KT-36102

package org.onebusaway.client

import org.onebusaway.models.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
package org.onebusaway.client

import org.onebusaway.core.ClientOptions
import org.onebusaway.core.http.HttpResponse.Handler
import org.onebusaway.errors.OnebusawaySdkError
import org.onebusaway.models.*
import org.onebusaway.services.async.*
import org.onebusaway.services.errorHandler

class OnebusawaySdkClientAsyncImpl
constructor(
private val clientOptions: ClientOptions,
) : OnebusawaySdkClientAsync {

private val errorHandler: Handler<OnebusawaySdkError> = errorHandler(clientOptions.jsonMapper)

private val sync: OnebusawaySdkClient by lazy { OnebusawaySdkClientImpl(clientOptions) }

private val agenciesWithCoverage: AgenciesWithCoverageServiceAsync by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,14 @@
package org.onebusaway.client

import org.onebusaway.core.ClientOptions
import org.onebusaway.core.http.HttpResponse.Handler
import org.onebusaway.errors.OnebusawaySdkError
import org.onebusaway.models.*
import org.onebusaway.services.blocking.*
import org.onebusaway.services.errorHandler

class OnebusawaySdkClientImpl
constructor(
private val clientOptions: ClientOptions,
) : OnebusawaySdkClient {

private val errorHandler: Handler<OnebusawaySdkError> = errorHandler(clientOptions.jsonMapper)

private val async: OnebusawaySdkClientAsync by lazy {
OnebusawaySdkClientAsyncImpl(clientOptions)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
@file:JvmName("HttpRequestBodies")

package org.onebusaway.core

import com.fasterxml.jackson.databind.json.JsonMapper
import java.io.ByteArrayOutputStream
import java.io.OutputStream
import org.apache.hc.client5.http.entity.mime.MultipartEntityBuilder
import org.onebusaway.core.http.HttpRequestBody
import org.onebusaway.errors.OnebusawaySdkException

internal inline fun <reified T> json(
jsonMapper: JsonMapper,
value: T,
): HttpRequestBody {
return object : HttpRequestBody {
private var cachedBytes: ByteArray? = null

private fun serialize(): ByteArray {
if (cachedBytes != null) return cachedBytes!!

val buffer = ByteArrayOutputStream()
try {
jsonMapper.writeValue(buffer, value)
cachedBytes = buffer.toByteArray()
return cachedBytes!!
} catch (e: Exception) {
throw OnebusawaySdkException("Error writing request", e)
}
}

override fun writeTo(outputStream: OutputStream) {
outputStream.write(serialize())
}

override fun contentType(): String = "application/json"

override fun contentLength(): Long {
return serialize().size.toLong()
}

override fun repeatable(): Boolean = true

override fun close() {}
}
}

internal fun multipartFormData(
jsonMapper: JsonMapper,
parts: Array<MultipartFormValue<*>?>
): HttpRequestBody {
val builder = MultipartEntityBuilder.create()
parts.forEach { part ->
if (part?.value != null) {
when (part.value) {
is JsonValue -> {
val buffer = ByteArrayOutputStream()
try {
jsonMapper.writeValue(buffer, part.value)
} catch (e: Exception) {
throw OnebusawaySdkException("Error serializing value to json", e)
}
builder.addBinaryBody(
part.name,
buffer.toByteArray(),
part.contentType,
part.filename
)
}
is Boolean ->
builder.addTextBody(
part.name,
if (part.value) "true" else "false",
part.contentType
)
is Int -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
is Long -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
is Double -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
is ByteArray ->
builder.addBinaryBody(part.name, part.value, part.contentType, part.filename)
is String -> builder.addTextBody(part.name, part.value, part.contentType)
is Enum -> builder.addTextBody(part.name, part.value.toString(), part.contentType)
else ->
throw IllegalArgumentException(
"Unsupported content type: ${part.value::class.java.simpleName}"
)
}
}
}
val entity = builder.build()

return object : HttpRequestBody {
override fun writeTo(outputStream: OutputStream) {
try {
return entity.writeTo(outputStream)
} catch (e: Exception) {
throw OnebusawaySdkException("Error writing request", e)
}
}

override fun contentType(): String = entity.contentType

override fun contentLength(): Long = -1

override fun repeatable(): Boolean = entity.isRepeatable

override fun close() = entity.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,9 @@ fun getOsName(): String {
}
}

fun getOsVersion(): String {
return System.getProperty("os.version", "unknown")
}
fun getOsVersion(): String = System.getProperty("os.version", "unknown")

fun getPackageVersion(): String {
return Properties::class.java.`package`.implementationVersion ?: "unknown"
}
fun getPackageVersion(): String =
Properties::class.java.`package`.implementationVersion ?: "unknown"

fun getJavaVersion(): String {
return System.getProperty("java.version", "unknown")
}
fun getJavaVersion(): String = System.getProperty("java.version", "unknown")
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,8 @@ internal constructor(
}
}

override fun toString(): String {
return "MultipartFormValue(name='$name', contentType=$contentType, filename=$filename, value=${valueToString()})"
}
override fun toString(): String =
"MultipartFormValue{name=$name, contentType=$contentType, filename=$filename, value=${valueToString()}}"

private fun valueToString(): String =
when (value) {
Expand Down
Loading

0 comments on commit 1707ccb

Please sign in to comment.