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
43 changes: 2 additions & 41 deletions src/main/java/com/google/genai/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,10 @@
import java.util.stream.Stream;
import okhttp3.Credentials;
import okhttp3.Dispatcher;
import okhttp3.Interceptor;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import org.jspecify.annotations.Nullable;

/** Interface for an API client which issues HTTP requests to the GenAI APIs. */
Expand Down Expand Up @@ -307,8 +305,6 @@ private OkHttpClient createHttpClient(
HttpRetryOptions retryOptions =
httpOptions.retryOptions().orElse(HttpRetryOptions.builder().build());
builder.addInterceptor(new RetryInterceptor(retryOptions));
// Must come after RetryInterceptor so that it runs once per attempt.
builder.addInterceptor(this::reauthorize);

return builder.build();
}
Expand Down Expand Up @@ -529,9 +525,8 @@ private void setHeaders(Request.Builder request, HttpOptions requestHttpOptions)
} catch (IOException e) {
throw new GenAiIOException("Failed to refresh credentials.", e);
}
String authorization = "Bearer " + cred.getAccessToken().getTokenValue();
request.header("Authorization", authorization);
request.tag(SdkAuthorization.class, new SdkAuthorization(authorization));
String accessToken = cred.getAccessToken().getTokenValue();
request.header("Authorization", "Bearer " + accessToken);

if (cred.getQuotaProjectId() != null) {
request.header("x-goog-user-project", cred.getQuotaProjectId());
Expand All @@ -541,40 +536,6 @@ private void setHeaders(Request.Builder request, HttpOptions requestHttpOptions)
}
}

/**
* Re-applies credentials before each attempt, so that a retried or queued request does not
* carry an access token that expired after the request was built.
*
* <p>The refresh goes through {@link #setHeaders} for the same Boq conformance reason as {@link
* #refreshAndGetAccessToken}. A request whose Authorization header was changed after the SDK set
* it is sent unchanged.
*/
private Response reauthorize(Interceptor.Chain chain) throws IOException {
Request request = chain.request();
SdkAuthorization sdkAuthorization = request.tag(SdkAuthorization.class);
if (sdkAuthorization == null
|| !sdkAuthorization.headerValue.equals(request.header("Authorization"))) {
return chain.proceed(request);
}
Request.Builder builder = request.newBuilder();
try {
setHeaders(builder, HttpOptions.builder().build());
} catch (GenAiIOException e) {
// OkHttp only reports IOExceptions to async callbacks; others kill the dispatcher thread.
throw new IOException(e.getMessage(), e);
}
return chain.proceed(builder.build());
}

/** The Authorization header value that {@link #setHeaders} set on a request. */
private static final class SdkAuthorization {
final String headerValue;

SdkAuthorization(String headerValue) {
this.headerValue = headerValue;
}
}

/**
* Refreshes credentials (if needed) and returns the current access token.
*
Expand Down
208 changes: 0 additions & 208 deletions src/test/java/com/google/genai/HttpApiClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,13 @@
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Dispatcher;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Protocol;
import okhttp3.Request;
Expand Down Expand Up @@ -100,11 +92,6 @@ public class HttpApiClientTest {
GoogleCredentials.newBuilder()
.setAccessToken(AccessToken.newBuilder().setTokenValue("").build())
.build();
private static final HttpOptions FAST_RETRY_HTTP_OPTIONS =
HttpOptions.builder()
.retryOptions(
HttpRetryOptions.builder().attempts(2).initialDelay(0.0).jitter(0.0).build())
.build();

@Mock OkHttpClient mockHttpClient;
@Mock Call mockCall;
Expand Down Expand Up @@ -1862,199 +1849,4 @@ public void testInitHttpClientWithCustomHttpClient() throws Exception {
client.httpClient().interceptors().stream()
.anyMatch(interceptor -> interceptor instanceof RetryInterceptor));
}

/** Credentials that issue whatever token the test sets, refreshing on every use. */
private static final class SwitchableCredentials extends GoogleCredentials {
final AtomicReference<String> token;
volatile boolean failRefresh = false;

SwitchableCredentials(String token) {
this.token = new AtomicReference<>(token);
}

@Override
public AccessToken refreshAccessToken() throws IOException {
if (failRefresh) {
throw new IOException("Token endpoint unavailable.");
}
// Expiring inside the refresh margin makes every refreshIfExpired() call refresh again.
return AccessToken.newBuilder()
.setTokenValue(token.get())
.setExpirationTime(new Date(System.currentTimeMillis() + 60_000))
.build();
}
}

private static HttpApiClient vertexClient(
GoogleCredentials credentials, Optional<ClientOptions> clientOptions) {
return new HttpApiClient(
Optional.empty(),
Optional.of(PROJECT),
Optional.of(LOCATION),
Optional.of(credentials),
Optional.of(FAST_RETRY_HTTP_OPTIONS),
clientOptions);
}

/** Appends a fake server after the SDK interceptors, so each attempt is answered locally. */
private static void addFakeServer(HttpApiClient client, Interceptor fakeServer)
throws Exception {
Field clientField = ApiClient.class.getDeclaredField("httpClient");
clientField.setAccessible(true);
clientField.set(client, client.httpClient().newBuilder().addInterceptor(fakeServer).build());
}

private static Response respond(Request request, int code) {
return new Response.Builder()
.request(request)
.protocol(Protocol.HTTP_1_1)
.code(code)
.message("")
.body(ResponseBody.create("{}", null))
.build();
}

@Test
public void testRequest_retryAfterTokenRotates_sendsCurrentToken() throws Exception {
SwitchableCredentials credentials = new SwitchableCredentials("token-a");
HttpApiClient client = vertexClient(credentials, Optional.empty());
List<Request> attempts = new ArrayList<>();
addFakeServer(
client,
chain -> {
attempts.add(chain.request());
if (attempts.size() == 1) {
credentials.token.set("token-b");
return respond(chain.request(), 503);
}
return respond(chain.request(), 200);
});

client.request("POST", TEST_PATH, TEST_REQUEST_JSON, Optional.empty()).close();

assertEquals(2, attempts.size());
assertEquals("Bearer token-a", attempts.get(0).header("Authorization"));
assertEquals("Bearer token-b", attempts.get(1).header("Authorization"));
}

@Test
public void testAsyncRequest_queuedBehindDispatcherLimit_sendsTokenCurrentAtSendTime()
throws Exception {
SwitchableCredentials credentials = new SwitchableCredentials("token-a");
HttpApiClient client =
vertexClient(
credentials, Optional.of(ClientOptions.builder().maxConnectionsPerHost(1).build()));
CountDownLatch firstCallStarted = new CountDownLatch(1);
CountDownLatch releaseFirstCall = new CountDownLatch(1);
List<Request> attempts = Collections.synchronizedList(new ArrayList<>());
addFakeServer(
client,
chain -> {
attempts.add(chain.request());
if (attempts.size() == 1) {
firstCallStarted.countDown();
try {
releaseFirstCall.await();
} catch (InterruptedException e) {
throw new IOException(e);
}
}
return respond(chain.request(), 200);
});

CompletableFuture<ApiResponse> first =
client.asyncRequest("POST", TEST_PATH, TEST_REQUEST_JSON, Optional.empty());
assertTrue(firstCallStarted.await(5, TimeUnit.SECONDS));
// Built with token-a, then queued until the first call finishes.
CompletableFuture<ApiResponse> second =
client.asyncRequest("POST", TEST_PATH, TEST_REQUEST_JSON, Optional.empty());
credentials.token.set("token-b");
releaseFirstCall.countDown();
first.get(5, TimeUnit.SECONDS).close();
second.get(5, TimeUnit.SECONDS).close();

assertEquals(2, attempts.size());
assertEquals("Bearer token-b", attempts.get(1).header("Authorization"));
}

@Test
public void testRequest_authorizationChangedByCustomClient_isKeptOnRetry() throws Exception {
SwitchableCredentials credentials = new SwitchableCredentials("token-a");
OkHttpClient customClient =
new OkHttpClient.Builder()
.addInterceptor(
chain ->
chain.proceed(
chain
.request()
.newBuilder()
.header("Authorization", "Bearer caller-token")
.build()))
.build();
HttpApiClient client =
vertexClient(
credentials, Optional.of(ClientOptions.builder().customHttpClient(customClient).build()));
List<Request> attempts = new ArrayList<>();
addFakeServer(
client,
chain -> {
attempts.add(chain.request());
if (attempts.size() == 1) {
credentials.token.set("token-b");
return respond(chain.request(), 503);
}
return respond(chain.request(), 200);
});

client.request("POST", TEST_PATH, TEST_REQUEST_JSON, Optional.empty()).close();

assertEquals(2, attempts.size());
assertEquals("Bearer caller-token", attempts.get(0).header("Authorization"));
assertEquals("Bearer caller-token", attempts.get(1).header("Authorization"));
}

@Test
public void testRequest_apiKeyRetry_sendsNoAuthorizationHeader() throws Exception {
HttpApiClient client =
new HttpApiClient(
Optional.of(API_KEY), Optional.of(FAST_RETRY_HTTP_OPTIONS), Optional.empty());
List<Request> attempts = new ArrayList<>();
addFakeServer(
client,
chain -> {
attempts.add(chain.request());
return respond(chain.request(), attempts.size() == 1 ? 503 : 200);
});

client.request("POST", TEST_PATH, TEST_REQUEST_JSON, Optional.empty()).close();

assertEquals(2, attempts.size());
for (Request attempt : attempts) {
assertEquals(API_KEY, attempt.header("x-goog-api-key"));
assertNull(attempt.header("Authorization"));
}
}

@Test
public void testAsyncRequest_refreshFailsOnRetry_completesWithRefreshError() throws Exception {
SwitchableCredentials credentials = new SwitchableCredentials("token-a");
HttpApiClient client = vertexClient(credentials, Optional.empty());
addFakeServer(
client,
chain -> {
credentials.failRefresh = true;
return respond(chain.request(), 503);
});

ExecutionException exception =
assertThrows(
ExecutionException.class,
() ->
client
.asyncRequest("POST", TEST_PATH, TEST_REQUEST_JSON, Optional.empty())
.get(5, TimeUnit.SECONDS));

assertTrue(exception.getCause() instanceof GenAiIOException);
assertEquals("Failed to refresh credentials.", exception.getCause().getCause().getMessage());
}
}
Loading