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
16 changes: 12 additions & 4 deletions src/main/java/com/google/genai/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.api.core.InternalApi;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.base.Ascii;
import com.google.common.collect.ImmutableMap;
Expand All @@ -45,7 +46,8 @@
import org.jspecify.annotations.Nullable;

/** Interface for an API client which issues HTTP requests to the GenAI APIs. */
abstract class ApiClient {
@InternalApi
public abstract class ApiClient {

// {x-version-update-start:google-genai:released}
private static final String SDK_VERSION = "1.28.0";
Expand Down Expand Up @@ -437,10 +439,15 @@ public boolean vertexAI() {
}

/** Returns the HttpClient for API calls. */
OkHttpClient httpClient() {
public OkHttpClient httpClient() {
return httpClient;
}

/** Returns the HTTP options for API calls. */
public HttpOptions httpOptions() {
return httpOptions;
}

/**
* Merges two maps recursively. If a key exists in both maps, the value from `source` overwrites
* the value in `target`. If the value is a list, then update the whole list. A warning is logged
Expand Down Expand Up @@ -611,7 +618,7 @@ static String getApiKeyFromEnv() {
* <li>vertexBaseUrl -> GOOGLE_VERTEX_BASE_URL: Base URL for Vertex AI APIs.
* </ul>
*/
static ImmutableMap<String, String> defaultEnvironmentVariables() {
public static ImmutableMap<String, String> defaultEnvironmentVariables() {
ImmutableMap.Builder<String, String> mapBuilder = ImmutableMap.builder();
String value;
value = System.getenv("GOOGLE_API_KEY");
Expand Down Expand Up @@ -646,7 +653,8 @@ static ImmutableMap<String, String> defaultEnvironmentVariables() {
}

/** Overrides the base URLs for the Gemini API and/or Vertex AI API. */
static void setDefaultBaseUrls(Optional<String> geminiBaseUrl, Optional<String> vertexBaseUrl) {
public static void setDefaultBaseUrls(
Optional<String> geminiBaseUrl, Optional<String> vertexBaseUrl) {
ApiClient.geminiBaseUrl = geminiBaseUrl;
ApiClient.vertexBaseUrl = vertexBaseUrl;
}
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/google/genai/ApiResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@

package com.google.genai;

import com.google.api.core.InternalApi;
import okhttp3.Headers;
import okhttp3.ResponseBody;

/** The API response contains a response to a call to the GenAI APIs. */
abstract class ApiResponse implements AutoCloseable {
@InternalApi
public abstract class ApiResponse implements AutoCloseable {
/** Gets the ResponseBody. */
public abstract ResponseBody getBody();

Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/google/genai/AsyncBatches.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ CompletableFuture<BatchJob> privateCreate(
String model, BatchJobSource src, CreateBatchJobConfig config) {
BuiltRequest builtRequest = batches.buildRequestForPrivateCreate(model, src, config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -64,7 +64,7 @@ CompletableFuture<BatchJob> privateCreateEmbeddings(
String model, EmbeddingsBatchJobSource src, CreateEmbeddingsBatchJobConfig config) {
BuiltRequest builtRequest = batches.buildRequestForPrivateCreateEmbeddings(model, src, config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -85,7 +85,7 @@ CompletableFuture<BatchJob> privateCreateEmbeddings(
public CompletableFuture<BatchJob> get(String name, GetBatchJobConfig config) {
BuiltRequest builtRequest = batches.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -105,7 +105,7 @@ public CompletableFuture<BatchJob> get(String name, GetBatchJobConfig config) {
public CompletableFuture<Void> cancel(String name, CancelBatchJobConfig config) {
BuiltRequest builtRequest = batches.buildRequestForCancel(name, config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenAccept(
response -> {
try (ApiResponse res = response) {}
Expand All @@ -115,7 +115,7 @@ public CompletableFuture<Void> cancel(String name, CancelBatchJobConfig config)
CompletableFuture<ListBatchJobsResponse> privateList(ListBatchJobsConfig config) {
BuiltRequest builtRequest = batches.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -135,7 +135,8 @@ CompletableFuture<ListBatchJobsResponse> privateList(ListBatchJobsConfig config)
public CompletableFuture<DeleteResourceJob> delete(String name, DeleteBatchJobConfig config) {
BuiltRequest builtRequest = batches.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest("delete", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest(
"delete", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/google/genai/AsyncCaches.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public AsyncCaches(ApiClient apiClient) {
public CompletableFuture<CachedContent> create(String model, CreateCachedContentConfig config) {
BuiltRequest builtRequest = caches.buildRequestForCreate(model, config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -73,7 +73,7 @@ public CompletableFuture<CachedContent> create(String model, CreateCachedContent
public CompletableFuture<CachedContent> get(String name, GetCachedContentConfig config) {
BuiltRequest builtRequest = caches.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -92,7 +92,8 @@ public CompletableFuture<DeleteCachedContentResponse> delete(
String name, DeleteCachedContentConfig config) {
BuiltRequest builtRequest = caches.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest("delete", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest(
"delete", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -111,7 +112,7 @@ public CompletableFuture<DeleteCachedContentResponse> delete(
public CompletableFuture<CachedContent> update(String name, UpdateCachedContentConfig config) {
BuiltRequest builtRequest = caches.buildRequestForUpdate(name, config);
return this.apiClient
.asyncRequest("patch", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("patch", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -123,7 +124,7 @@ public CompletableFuture<CachedContent> update(String name, UpdateCachedContentC
CompletableFuture<ListCachedContentsResponse> privateList(ListCachedContentsConfig config) {
BuiltRequest builtRequest = caches.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/google/genai/AsyncDocuments.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public AsyncDocuments(ApiClient apiClient) {
public CompletableFuture<Document> get(String name, GetDocumentConfig config) {
BuiltRequest builtRequest = documents.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -56,7 +56,8 @@ public CompletableFuture<Document> get(String name, GetDocumentConfig config) {
public CompletableFuture<Void> delete(String name, DeleteDocumentConfig config) {
BuiltRequest builtRequest = documents.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest("delete", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest(
"delete", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenAccept(
response -> {
try (ApiResponse res = response) {}
Expand All @@ -66,7 +67,7 @@ public CompletableFuture<Void> delete(String name, DeleteDocumentConfig config)
CompletableFuture<ListDocumentsResponse> privateList(String parent, ListDocumentsConfig config) {
BuiltRequest builtRequest = documents.buildRequestForPrivateList(parent, config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/com/google/genai/AsyncFileSearchStores.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public AsyncFileSearchStores(ApiClient apiClient) {
public CompletableFuture<FileSearchStore> create(CreateFileSearchStoreConfig config) {
BuiltRequest builtRequest = fileSearchStores.buildRequestForCreate(config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -73,7 +73,7 @@ public CompletableFuture<FileSearchStore> create(CreateFileSearchStoreConfig con
public CompletableFuture<FileSearchStore> get(String name, GetFileSearchStoreConfig config) {
BuiltRequest builtRequest = fileSearchStores.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -85,7 +85,8 @@ public CompletableFuture<FileSearchStore> get(String name, GetFileSearchStoreCon
public CompletableFuture<Void> delete(String name, DeleteFileSearchStoreConfig config) {
BuiltRequest builtRequest = fileSearchStores.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest("delete", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest(
"delete", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenAccept(
response -> {
try (ApiResponse res = response) {}
Expand All @@ -95,7 +96,7 @@ public CompletableFuture<Void> delete(String name, DeleteFileSearchStoreConfig c
CompletableFuture<ListFileSearchStoresResponse> privateList(ListFileSearchStoresConfig config) {
BuiltRequest builtRequest = fileSearchStores.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -109,7 +110,7 @@ CompletableFuture<UploadToFileSearchStoreResumableResponse> privateUploadToFileS
BuiltRequest builtRequest =
fileSearchStores.buildRequestForPrivateUploadToFileSearchStore(fileSearchStoreName, config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -124,7 +125,7 @@ public CompletableFuture<ImportFileOperation> importFile(
BuiltRequest builtRequest =
fileSearchStores.buildRequestForImportFile(fileSearchStoreName, fileName, config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/google/genai/AsyncFiles.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public AsyncFiles(ApiClient apiClient) {
CompletableFuture<ListFilesResponse> privateList(ListFilesConfig config) {
BuiltRequest builtRequest = files.buildRequestForPrivateList(config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -64,7 +64,7 @@ CompletableFuture<ListFilesResponse> privateList(ListFilesConfig config) {
CompletableFuture<CreateFileResponse> privateCreate(File file, CreateFileConfig config) {
BuiltRequest builtRequest = files.buildRequestForPrivateCreate(file, config);
return this.apiClient
.asyncRequest("post", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("post", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -83,7 +83,7 @@ CompletableFuture<CreateFileResponse> privateCreate(File file, CreateFileConfig
public CompletableFuture<File> get(String name, GetFileConfig config) {
BuiltRequest builtRequest = files.buildRequestForGet(name, config);
return this.apiClient
.asyncRequest("get", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest("get", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand All @@ -102,7 +102,8 @@ public CompletableFuture<File> get(String name, GetFileConfig config) {
public CompletableFuture<DeleteFileResponse> delete(String name, DeleteFileConfig config) {
BuiltRequest builtRequest = files.buildRequestForDelete(name, config);
return this.apiClient
.asyncRequest("delete", builtRequest.path, builtRequest.body, builtRequest.httpOptions)
.asyncRequest(
"delete", builtRequest.path(), builtRequest.body(), builtRequest.httpOptions())
.thenApplyAsync(
response -> {
try (ApiResponse res = response) {
Expand Down
Loading