Skip to main content
Applies to:


Summary

Goal: Configure proxy authentication for the Braintrust Java SDK using a custom OkHttp client. Features: Custom HTTP client configuration, OkHttp proxy authenticator, braintrust-java-core package.

Configuration Steps

Step 1: Replace dependency

Replace braintrust-java with braintrust-java-core in your project dependencies.
<dependency>
  <groupId>com.braintrustdata.api</groupId>
  <artifactId>braintrust-java-core</artifactId>
  <version>VERSION</version>
</dependency>

Step 2: Customize OkHttpClient

Copy the OkHttpClient class from braintrust-java-client-okhttp and add proxyAuthenticator support to the builder.
class Builder internal constructor() {
    private var baseUrl: HttpUrl? = null
    private var timeout: Timeout = Timeout.default()
    private var proxy: Proxy? = null
    private var proxyAuthenticator: okhttp3.Authenticator? = null

    fun baseUrl(baseUrl: String) = apply { this.baseUrl = baseUrl.toHttpUrl() }

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

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

    fun proxyAuthenticator(authenticator: okhttp3.Authenticator?) = apply {
        this.proxyAuthenticator = authenticator
    }

    fun build(): OkHttpClient =
        OkHttpClient(
            okhttp3.OkHttpClient.Builder()
                .connectTimeout(timeout.connect())
                .readTimeout(timeout.read())
                .writeTimeout(timeout.write())
                .callTimeout(timeout.request())
                .proxy(proxy)
                .proxyAuthenticator(proxyAuthenticator ?: okhttp3.Authenticator.NONE)
                .build(),
            checkRequired("baseUrl", baseUrl),
        )
}

Step 3: Configure client with proxy authentication

Construct BraintrustClientImpl with your customized HTTP client, setting both proxy and authenticator.
BraintrustClient client =
    BraintrustOkHttpClient.builder()
        .baseUrl(apiUrl)
        .apiKey(apiKey)
        .proxy(
            new Proxy(
                Proxy.Type.HTTP,
                new InetSocketAddress(proxyHost, proxyPort)))
        .proxyAuthenticator(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                    proxyUsername, proxyPassword.toCharArray());
            }
        })
        .build();

Notes

Java’s Authenticator.setDefault() does not work with OkHttp, which requires explicit proxyAuthenticator configuration on the builder.