Skip to content
Open
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
2 changes: 1 addition & 1 deletion openai-java-client-okhttp/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ listOf(configurations.testCompileClasspath, configurations.testRuntimeClasspath)
dependencies {
api(project(":openai-java-core"))

implementation("com.squareup.okhttp3:okhttp:4.12.0")
api("com.squareup.okhttp3:okhttp:4.12.0")

testImplementation(kotlin("test"))
testImplementation("org.assertj:assertj-core:3.27.7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import okhttp3.ConnectionPool
import okhttp3.Dispatcher
import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor
import okhttp3.MediaType
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.Request
Expand Down Expand Up @@ -120,6 +121,7 @@ internal constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClie
private var sslSocketFactory: SSLSocketFactory? = null
private var trustManager: X509TrustManager? = null
private var hostnameVerifier: HostnameVerifier? = null
private val interceptors: MutableList<Interceptor> = mutableListOf()

fun timeout(timeout: Timeout) = apply { this.timeout = timeout }

Expand All @@ -136,6 +138,9 @@ internal constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClie
this.proxyAuthenticator = proxyAuthenticator
}

/** Adds an application interceptor to the underlying OkHttp transport. */
fun addInterceptor(interceptor: Interceptor) = apply { interceptors.add(interceptor) }
Comment thread
sylvesterkaczmarek marked this conversation as resolved.

/**
* Sets the maximum number of idle connections kept by the underlying [ConnectionPool].
*
Expand Down Expand Up @@ -187,6 +192,8 @@ internal constructor(@JvmSynthetic internal val okHttpClient: okhttp3.OkHttpClie
.callTimeout(timeout.request())
.proxy(proxy)
.apply {
interceptors.forEach(::addInterceptor)

proxyAuthenticator?.let { auth ->
proxyAuthenticator { route, response ->
auth
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo
import com.github.tomakehurst.wiremock.junit5.WireMockTest
import com.openai.core.http.HttpMethod
import com.openai.core.http.HttpRequest
import okhttp3.Interceptor
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
Expand Down Expand Up @@ -41,4 +42,40 @@ internal class OkHttpClientTest {
// Should have cancelled the underlying call
assertThat(call.isCanceled()).isTrue()
}

@Test
fun execute_runsConfiguredApplicationInterceptor() {
stubFor(get(urlPathEqualTo("/something")).willReturn(ok()))
val client =
OkHttpClient.builder()
.addInterceptor(
Interceptor { chain ->
chain.proceed(
chain
.request()
.newBuilder()
.header("X-Test-Interceptor", "applied")
.build()
)
}
)
.build()

client
.execute(
HttpRequest.builder()
.method(HttpMethod.GET)
.baseUrl(baseUrl)
.addPathSegment("something")
.build()
)
.use { assertThat(it.statusCode()).isEqualTo(200) }

verify(
1,
getRequestedFor(urlPathEqualTo("/something"))
.withHeader("X-Test-Interceptor", equalTo("applied")),
)
client.close()
}
}