Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BraintrustOkHttpClient 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 BraintrustOkHttpClient private constructor() {

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

fun build(): BraintrustClient {
return BraintrustClientImpl(
fun build(): BraintrustClient =
BraintrustClientImpl(
clientOptions
.httpClient(
OkHttpClient.builder()
Expand All @@ -78,6 +78,5 @@ class BraintrustOkHttpClient private constructor() {
)
.build()
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BraintrustOkHttpClientAsync 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 BraintrustOkHttpClientAsync private constructor() {

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

fun build(): BraintrustClientAsync {
return BraintrustClientAsyncImpl(
fun build(): BraintrustClientAsync =
BraintrustClientAsyncImpl(
clientOptions
.httpClient(
OkHttpClient.builder()
Expand All @@ -78,6 +78,5 @@ class BraintrustOkHttpClientAsync private constructor() {
)
.build()
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
request.body?.run { future.whenComplete { _, _ -> close() } }

val call = getClient(requestOptions).newCall(request.toRequest())

call.enqueue(
object : Callback {
override fun onResponse(call: Call, response: Response) {
Expand All @@ -90,7 +89,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 @@ -118,43 +117,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 @@ -163,9 +146,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 @@ -176,7 +157,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 @@ -186,8 +167,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 @@ -197,6 +178,5 @@ private constructor(private val okHttpClient: okhttp3.OkHttpClient, private val
.build(),
checkNotNull(baseUrl) { "`baseUrl` is required but was not set" },
)
}
}
}
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 com.braintrustdata.api.client

import com.braintrustdata.api.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 com.braintrustdata.api.client

import com.braintrustdata.api.models.*
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("OVERLOADS_INTERFACE") // See https://youtrack.jetbrains.com/issue/KT-36102

package com.braintrustdata.api.core.http

import com.braintrustdata.api.core.RequestOptions
Expand All @@ -6,11 +8,13 @@ import java.util.concurrent.CompletableFuture

interface HttpClient : AutoCloseable {

@JvmOverloads
fun execute(
request: HttpRequest,
requestOptions: RequestOptions = RequestOptions.none(),
): HttpResponse

@JvmOverloads
fun executeAsync(
request: HttpRequest,
requestOptions: RequestOptions = RequestOptions.none(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
@file:JvmSynthetic

package com.braintrustdata.api.core.http

import com.braintrustdata.api.core.RequestOptions
Expand Down