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
17 changes: 17 additions & 0 deletions src/main/java/com/google/genai/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public abstract class ApiClient implements AutoCloseable {
final OkHttpClient httpClient;
HttpOptions httpOptions;
final boolean vertexAI;
private final boolean defaultApiVersion;
final Optional<ClientOptions> clientOptions;

final Optional<String> customBaseUrl;
Expand Down Expand Up @@ -114,6 +115,9 @@ protected ApiClient(
this.location = Optional.empty();
this.credentials = Optional.empty();
this.vertexAI = false;
this.defaultApiVersion =
computeIsDefaultApiVersion(
customHttpOptions, defaultEnvironmentVariables().get("apiVersion"));
this.clientOptions = clientOptions;
this.customBaseUrl =
customHttpOptions.flatMap(HttpOptions::baseUrl).map(url -> url.replaceAll("/$", ""));
Expand Down Expand Up @@ -273,6 +277,8 @@ protected ApiClient(
this.httpOptions = mergeHttpOptions(customHttpOptions.get());
}
this.vertexAI = true;
this.defaultApiVersion =
computeIsDefaultApiVersion(customHttpOptions, environmentVariables.get("apiVersion"));
this.httpClient = createHttpClient(httpOptions, clientOptions);

}
Expand Down Expand Up @@ -651,11 +657,22 @@ static String libraryVersion() {
return libraryLabel + " " + languageLabel;
}

private static boolean computeIsDefaultApiVersion(
Optional<HttpOptions> customHttpOptions, @Nullable String envApiVersion) {
return !customHttpOptions.flatMap(HttpOptions::apiVersion).isPresent()
&& (envApiVersion == null || envApiVersion.isEmpty());
}

/** Returns whether the client is using Vertex AI APIs. */
public boolean vertexAI() {
return vertexAI;
}

/** Returns whether no API version was configured for the client. */
public boolean isDefaultApiVersion() {
return defaultApiVersion;
}

/** Returns the project ID for Vertex AI APIs. */
public @Nullable String project() {
return project.orElse(null);
Expand Down
39 changes: 26 additions & 13 deletions src/main/java/com/google/genai/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,31 +174,31 @@ public Client build() {
asyncRetryScheduler);
}

/** Sets the API key for Gemini API. */
/** Sets the API key for the Gemini Developer API or the Gemini Enterprise API. */
@CanIgnoreReturnValue
public Builder apiKey(String apiKey) {
checkNotNull(apiKey, "apiKey cannot be null");
this.apiKey = Optional.of(apiKey);
return this;
}

/** Sets the project ID for Vertex AI APIs. */
/** Sets the project ID for the Gemini Enterprise API. */
@CanIgnoreReturnValue
public Builder project(String project) {
checkNotNull(project, "project cannot be null");
this.project = Optional.of(project);
return this;
}

/** Sets the location for Vertex AI APIs. */
/** Sets the location for the Gemini Enterprise API. */
@CanIgnoreReturnValue
public Builder location(String location) {
checkNotNull(location, "location cannot be null");
this.location = Optional.of(location);
return this;
}

/** Sets the {@link GoogleCredentials} for Vertex AI APIs. */
/** Sets the {@link GoogleCredentials} for the Gemini Enterprise API. */
@CanIgnoreReturnValue
public Builder credentials(GoogleCredentials credentials) {
checkNotNull(credentials, "credentials cannot be null");
Expand All @@ -222,16 +222,17 @@ public Builder httpOptions(HttpOptions httpOptions) {
return this;
}

/** Sets whether to use Gemini Enterprise Agent Platform.
* When both enterprise and vertexAI are set, and they have different values, an IllegalArgumentException will be thrown.
/**
* Sets whether to use the Gemini Enterprise API. When both enterprise and vertexAI are set, and
* they have different values, an IllegalArgumentException will be thrown.
*/
@CanIgnoreReturnValue
public Builder enterprise(boolean enterprise) {
this.enterprise = Optional.of(enterprise);
return this;
}

/** Sets whether to use Vertex AI APIs. */
/** Sets whether to use the Gemini Enterprise API. Prefer {@link #enterprise(boolean)}. */
@CanIgnoreReturnValue
public Builder vertexAI(boolean vertexAI) {
this.vertexAI = Optional.of(vertexAI);
Expand Down Expand Up @@ -605,29 +606,41 @@ public void onResponse(Call call, Response okResponse) {
});
}
}

// android:strip_end

/** Returns whether the client is using Vertex AI APIs. */
/**
* Returns whether the client is using the Gemini Enterprise API. Prefer {@link #enterprise()}.
*/
public boolean vertexAI() {
return apiClient.vertexAI();
}

/** Returns whether the client is using Gemini Enterprise Agent Platform. */
/** Returns whether the client is using the Gemini Enterprise API. */
public boolean enterprise() {
return apiClient.vertexAI();
}

/** Returns the project ID for Vertex AI APIs. */
/**
* Returns {@code true} if no API version was configured via {@link HttpOptions#apiVersion()} or
* the {@code GOOGLE_GENAI_API_VERSION} environment variable, so the client is using the SDK's
* default API version for its backend.
*/
public boolean isDefaultApiVersion() {
return apiClient.isDefaultApiVersion();
}

/** Returns the project ID for the Gemini Enterprise API. */
public String project() {
return apiClient.project();
}

/** Returns the location for Vertex AI APIs. */
/** Returns the location for the Gemini Enterprise API. */
public String location() {
return apiClient.location();
}

/** Returns the API key for Gemini API. */
/** Returns the API key for the Gemini Developer API or the Gemini Enterprise API. */
public String apiKey() {
return apiClient.apiKey();
}
Expand Down Expand Up @@ -673,7 +686,7 @@ public void close() {
}

/**
* Overrides the base URLs for the Gemini API and/or Vertex AI API.
* Overrides the base URLs for the Gemini Developer API and/or the Gemini Enterprise API.
*
* <p>Note: This function should be called before initializing the SDK. If the base URLs are set
* after initializing the SDK, the base URLs will not be updated.
Expand Down
156 changes: 156 additions & 0 deletions src/test/java/com/google/genai/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,162 @@ void explicitApiVersion_overridesEnvVar_withoutThrowing() {
}
}

@Test
void isDefaultApiVersion_trueWhenUnconfigured() {
try (MockedStatic<ApiClient> mocked =
Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) {
mocked.when(ApiClient::defaultEnvironmentVariables).thenReturn(ImmutableMap.of());

Client mldevClient = Client.builder().apiKey(API_KEY).enterprise(false).build();
assertTrue(mldevClient.isDefaultApiVersion());

Client enterpriseClient =
Client.builder()
.project(PROJECT)
.location(LOCATION)
.credentials(CREDENTIALS)
.enterprise(true)
.build();
assertTrue(enterpriseClient.isDefaultApiVersion());

HttpOptions nonVersionOptions =
HttpOptions.builder()
.baseUrl("https://custom.example.com")
.headers(ImmutableMap.of("X-Test", "1"))
.timeout(5000)
.build();
Client mldevWithNonVersionOptions =
Client.builder().apiKey(API_KEY).enterprise(false).httpOptions(nonVersionOptions).build();
assertTrue(mldevWithNonVersionOptions.isDefaultApiVersion());

Client enterpriseWithNonVersionOptions =
Client.builder()
.project(PROJECT)
.location(LOCATION)
.credentials(CREDENTIALS)
.enterprise(true)
.httpOptions(nonVersionOptions)
.build();
assertTrue(enterpriseWithNonVersionOptions.isDefaultApiVersion());
}
}

@Test
void isDefaultApiVersion_falseWhenHttpOptionsApiVersionSet() {
try (MockedStatic<ApiClient> mocked =
Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) {
mocked.when(ApiClient::defaultEnvironmentVariables).thenReturn(ImmutableMap.of());

for (String version :
new String[] {ApiClient.GOOGLE_AI_API_DEFAULT_VERSION, "v1", "v1beta", "v1alpha", ""}) {
Client mldevClient =
Client.builder()
.apiKey(API_KEY)
.enterprise(false)
.httpOptions(HttpOptions.builder().apiVersion(version).build())
.build();
assertFalse(mldevClient.isDefaultApiVersion(), "version=" + version);
}

for (String version : new String[] {"v1beta1", "v1", ""}) {
Client enterpriseClient =
Client.builder()
.project(PROJECT)
.location(LOCATION)
.credentials(CREDENTIALS)
.enterprise(true)
.httpOptions(HttpOptions.builder().apiVersion(version).build())
.build();
assertFalse(enterpriseClient.isDefaultApiVersion(), "version=" + version);
}
}
}

@Test
void isDefaultApiVersion_envVarHandling() {
try (MockedStatic<ApiClient> mocked =
Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) {
HttpOptions nonVersionOptions =
HttpOptions.builder()
.baseUrl("https://custom.example.com")
.headers(ImmutableMap.of("X-Test", "1"))
.timeout(5000)
.build();
for (String envVersion : new String[] {"v1", "v1alpha", "beta"}) {
mocked
.when(ApiClient::defaultEnvironmentVariables)
.thenReturn(ImmutableMap.of("apiVersion", envVersion));

Client mldevClient = Client.builder().apiKey(API_KEY).enterprise(false).build();
assertFalse(mldevClient.isDefaultApiVersion(), "envVersion=" + envVersion);

Client enterpriseClient =
Client.builder()
.project(PROJECT)
.location(LOCATION)
.credentials(CREDENTIALS)
.enterprise(true)
.build();
assertFalse(enterpriseClient.isDefaultApiVersion(), "envVersion=" + envVersion);

Client mldevWithNonVersionOptions =
Client.builder()
.apiKey(API_KEY)
.enterprise(false)
.httpOptions(nonVersionOptions)
.build();
assertFalse(mldevWithNonVersionOptions.isDefaultApiVersion(), "envVersion=" + envVersion);

Client enterpriseWithNonVersionOptions =
Client.builder()
.project(PROJECT)
.location(LOCATION)
.credentials(CREDENTIALS)
.enterprise(true)
.httpOptions(nonVersionOptions)
.build();
assertFalse(
enterpriseWithNonVersionOptions.isDefaultApiVersion(), "envVersion=" + envVersion);
}

mocked
.when(ApiClient::defaultEnvironmentVariables)
.thenReturn(ImmutableMap.of("googleApiKey", API_KEY, "apiVersion", ""));
Client emptyEnvClient = new Client();
assertTrue(emptyEnvClient.isDefaultApiVersion());

Client emptyEnvEnterpriseClient =
Client.builder()
.project(PROJECT)
.location(LOCATION)
.credentials(CREDENTIALS)
.enterprise(true)
.build();
assertTrue(emptyEnvEnterpriseClient.isDefaultApiVersion());
}
}

@Test
void isDefaultApiVersion_replayClient() {
try (MockedStatic<ApiClient> mocked =
Mockito.mockStatic(ApiClient.class, Mockito.CALLS_REAL_METHODS)) {
mocked.when(ApiClient::defaultEnvironmentVariables).thenReturn(ImmutableMap.of());

Client defaultReplay =
Client.builder().apiKey(API_KEY).enterprise(false).debugConfig(DEBUG_CONFIG).build();
assertTrue(defaultReplay.isDefaultApiVersion());

Client configuredReplay =
Client.builder()
.apiKey(API_KEY)
.enterprise(false)
.debugConfig(DEBUG_CONFIG)
.httpOptions(HttpOptions.builder().apiVersion("v1alpha").build())
.build();
assertFalse(configuredReplay.isDefaultApiVersion());
}
}

static class TestApiClient extends ApiClient {
TestApiClient(HttpOptions httpOptions) {
super(Optional.of("dummy-api-key"), Optional.of(httpOptions), Optional.empty());
Expand Down
Loading