Skip to content

Commit 910d727

Browse files
tilgalascopybara-github
authored andcommitted
feat!: refactor ApiClient constructors hierarchy to remove Optional parameters
PiperOrigin-RevId: 883114754
1 parent 7cce374 commit 910d727

4 files changed

Lines changed: 59 additions & 77 deletions

File tree

core/src/main/java/com/google/adk/sessions/ApiClient.java

Lines changed: 40 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@
1616

1717
package com.google.adk.sessions;
1818

19-
import static com.google.common.base.Preconditions.checkNotNull;
2019
import static com.google.common.base.StandardSystemProperty.JAVA_VERSION;
2120

2221
import com.google.auth.oauth2.GoogleCredentials;
2322
import com.google.common.base.Ascii;
23+
import com.google.common.base.Strings;
2424
import com.google.common.collect.ImmutableMap;
2525
import com.google.genai.errors.GenAiIOException;
2626
import com.google.genai.types.HttpOptions;
@@ -35,83 +35,69 @@
3535
abstract class ApiClient {
3636
OkHttpClient httpClient;
3737
// For Google AI APIs
38-
final Optional<String> apiKey;
38+
final @Nullable String apiKey;
3939
// For Vertex AI APIs
40-
final Optional<String> project;
41-
final Optional<String> location;
42-
final Optional<GoogleCredentials> credentials;
40+
final @Nullable String project;
41+
final @Nullable String location;
42+
final @Nullable GoogleCredentials credentials;
4343
HttpOptions httpOptions;
4444
final boolean vertexAI;
4545

4646
/** Constructs an ApiClient for Google AI APIs. */
47-
ApiClient(Optional<String> apiKey, Optional<HttpOptions> customHttpOptions) {
48-
checkNotNull(apiKey, "API Key cannot be null");
49-
checkNotNull(customHttpOptions, "customHttpOptions cannot be null");
47+
ApiClient(@Nullable String apiKey, @Nullable HttpOptions customHttpOptions) {
5048

51-
try {
52-
this.apiKey = Optional.of(apiKey.orElseGet(() -> System.getenv("GOOGLE_API_KEY")));
53-
} catch (NullPointerException e) {
49+
this.apiKey = apiKey != null ? apiKey : System.getenv("GOOGLE_API_KEY");
50+
51+
if (Strings.isNullOrEmpty(this.apiKey)) {
5452
throw new IllegalArgumentException(
55-
"API key must either be provided or set in the environment variable" + " GOOGLE_API_KEY.",
56-
e);
53+
"API key must either be provided or set in the environment variable"
54+
+ " GOOGLE_API_KEY.");
5755
}
5856

59-
this.project = Optional.empty();
60-
this.location = Optional.empty();
61-
this.credentials = Optional.empty();
57+
this.project = null;
58+
this.location = null;
59+
this.credentials = null;
6260
this.vertexAI = false;
6361

6462
this.httpOptions = defaultHttpOptions(/* vertexAI= */ false, this.location);
6563

66-
if (customHttpOptions.isPresent()) {
67-
applyHttpOptions(customHttpOptions.get());
64+
if (customHttpOptions != null) {
65+
applyHttpOptions(customHttpOptions);
6866
}
6967

7068
this.httpClient = createHttpClient(httpOptions.timeout().orElse(null));
7169
}
7270

7371
ApiClient(
74-
Optional<String> project,
75-
Optional<String> location,
76-
Optional<GoogleCredentials> credentials,
77-
Optional<HttpOptions> customHttpOptions) {
78-
checkNotNull(project, "project cannot be null");
79-
checkNotNull(location, "location cannot be null");
80-
checkNotNull(credentials, "credentials cannot be null");
81-
checkNotNull(customHttpOptions, "customHttpOptions cannot be null");
72+
@Nullable String project,
73+
@Nullable String location,
74+
@Nullable GoogleCredentials credentials,
75+
@Nullable HttpOptions customHttpOptions) {
8276

83-
try {
84-
this.project = Optional.of(project.orElseGet(() -> System.getenv("GOOGLE_CLOUD_PROJECT")));
85-
} catch (NullPointerException e) {
77+
this.project = project != null ? project : System.getenv("GOOGLE_CLOUD_PROJECT");
78+
79+
if (Strings.isNullOrEmpty(this.project)) {
8680
throw new IllegalArgumentException(
8781
"Project must either be provided or set in the environment variable"
88-
+ " GOOGLE_CLOUD_PROJECT.",
89-
e);
90-
}
91-
if (this.project.get().isEmpty()) {
92-
throw new IllegalArgumentException("Project must not be empty.");
82+
+ " GOOGLE_CLOUD_PROJECT.");
9383
}
9484

95-
try {
96-
this.location = Optional.of(location.orElse(System.getenv("GOOGLE_CLOUD_LOCATION")));
97-
} catch (NullPointerException e) {
85+
this.location = location != null ? location : System.getenv("GOOGLE_CLOUD_LOCATION");
86+
87+
if (Strings.isNullOrEmpty(this.location)) {
9888
throw new IllegalArgumentException(
9989
"Location must either be provided or set in the environment variable"
100-
+ " GOOGLE_CLOUD_LOCATION.",
101-
e);
102-
}
103-
if (this.location.get().isEmpty()) {
104-
throw new IllegalArgumentException("Location must not be empty.");
90+
+ " GOOGLE_CLOUD_LOCATION.");
10591
}
10692

107-
this.credentials = Optional.of(credentials.orElseGet(this::defaultCredentials));
93+
this.credentials = credentials != null ? credentials : defaultCredentials();
10894

10995
this.httpOptions = defaultHttpOptions(/* vertexAI= */ true, this.location);
11096

111-
if (customHttpOptions.isPresent()) {
112-
applyHttpOptions(customHttpOptions.get());
97+
if (customHttpOptions != null) {
98+
applyHttpOptions(customHttpOptions);
11399
}
114-
this.apiKey = Optional.empty();
100+
this.apiKey = null;
115101
this.vertexAI = true;
116102
this.httpClient = createHttpClient(httpOptions.timeout().orElse(null));
117103
}
@@ -142,17 +128,17 @@ public boolean vertexAI() {
142128

143129
/** Returns the project ID for Vertex AI APIs. */
144130
public @Nullable String project() {
145-
return project.orElse(null);
131+
return project;
146132
}
147133

148134
/** Returns the location for Vertex AI APIs. */
149135
public @Nullable String location() {
150-
return location.orElse(null);
136+
return location;
151137
}
152138

153139
/** Returns the API key for Google AI APIs. */
154140
public @Nullable String apiKey() {
155-
return apiKey.orElse(null);
141+
return apiKey;
156142
}
157143

158144
/** Returns the HttpClient for API calls. */
@@ -192,7 +178,7 @@ private void applyHttpOptions(HttpOptions httpOptionsToApply) {
192178
this.httpOptions = mergedHttpOptionsBuilder.build();
193179
}
194180

195-
static HttpOptions defaultHttpOptions(boolean vertexAI, Optional<String> location) {
181+
static HttpOptions defaultHttpOptions(boolean vertexAI, @Nullable String location) {
196182
ImmutableMap.Builder<String, String> defaultHeaders = ImmutableMap.builder();
197183
defaultHeaders
198184
.put("Content-Type", "application/json")
@@ -202,14 +188,14 @@ static HttpOptions defaultHttpOptions(boolean vertexAI, Optional<String> locatio
202188
HttpOptions.Builder defaultHttpOptionsBuilder =
203189
HttpOptions.builder().headers(defaultHeaders.buildOrThrow());
204190

205-
if (vertexAI && location.isPresent()) {
191+
if (vertexAI && location != null) {
206192
defaultHttpOptionsBuilder
207193
.baseUrl(
208-
Ascii.equalsIgnoreCase(location.get(), "global")
194+
Ascii.equalsIgnoreCase(location, "global")
209195
? "https://aiplatform.googleapis.com"
210-
: String.format("https://%s-aiplatform.googleapis.com", location.get()))
196+
: String.format("https://%s-aiplatform.googleapis.com", location))
211197
.apiVersion("v1beta1");
212-
} else if (vertexAI && location.isEmpty()) {
198+
} else if (vertexAI && Strings.isNullOrEmpty(location)) {
213199
throw new IllegalArgumentException("Location must be provided for Vertex AI APIs.");
214200
} else {
215201
defaultHttpOptionsBuilder

core/src/main/java/com/google/adk/sessions/HttpApiClient.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,33 +18,34 @@
1818

1919
import com.google.auth.oauth2.GoogleCredentials;
2020
import com.google.common.base.Ascii;
21+
import com.google.common.base.Preconditions;
2122
import com.google.common.collect.ImmutableMap;
2223
import com.google.genai.errors.GenAiIOException;
2324
import com.google.genai.types.HttpOptions;
2425
import java.io.IOException;
2526
import java.util.Map;
26-
import java.util.Optional;
2727
import okhttp3.MediaType;
2828
import okhttp3.Request;
2929
import okhttp3.RequestBody;
3030
import okhttp3.Response;
31+
import org.jspecify.annotations.Nullable;
3132

3233
/** Base client for the HTTP APIs. */
3334
public class HttpApiClient extends ApiClient {
3435
public static final MediaType MEDIA_TYPE_APPLICATION_JSON =
3536
MediaType.parse("application/json; charset=utf-8");
3637

3738
/** Constructs an ApiClient for Google AI APIs. */
38-
HttpApiClient(Optional<String> apiKey, Optional<HttpOptions> httpOptions) {
39+
HttpApiClient(@Nullable String apiKey, @Nullable HttpOptions httpOptions) {
3940
super(apiKey, httpOptions);
4041
}
4142

4243
/** Constructs an ApiClient for Vertex AI APIs. */
4344
HttpApiClient(
44-
Optional<String> project,
45-
Optional<String> location,
46-
Optional<GoogleCredentials> credentials,
47-
Optional<HttpOptions> httpOptions) {
45+
@Nullable String project,
46+
@Nullable String location,
47+
@Nullable GoogleCredentials credentials,
48+
@Nullable HttpOptions httpOptions) {
4849
super(project, location, credentials, httpOptions);
4950
}
5051

@@ -54,9 +55,7 @@ public ApiResponse request(String httpMethod, String path, String requestJson) {
5455
boolean queryBaseModel =
5556
Ascii.equalsIgnoreCase(httpMethod, "GET") && path.startsWith("publishers/google/models/");
5657
if (this.vertexAI() && !path.startsWith("projects/") && !queryBaseModel) {
57-
path =
58-
String.format("projects/%s/locations/%s/", this.project.get(), this.location.get())
59-
+ path;
58+
path = String.format("projects/%s/locations/%s/", this.project, this.location) + path;
6059
}
6160
String requestUrl =
6261
String.format(
@@ -85,11 +84,11 @@ private void setHeaders(Request.Builder requestBuilder) {
8584
requestBuilder.header(header.getKey(), header.getValue());
8685
}
8786

88-
if (apiKey.isPresent()) {
89-
requestBuilder.header("x-goog-api-key", apiKey.get());
87+
if (apiKey != null) {
88+
requestBuilder.header("x-goog-api-key", apiKey);
9089
} else {
91-
GoogleCredentials cred =
92-
credentials.orElseThrow(() -> new IllegalStateException("credentials is required"));
90+
Preconditions.checkState(credentials != null, "credentials is required");
91+
GoogleCredentials cred = credentials;
9392
try {
9493
cred.refreshIfExpired();
9594
} catch (IOException e) {

core/src/main/java/com/google/adk/sessions/VertexAiClient.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.HashMap;
1818
import java.util.List;
1919
import java.util.Map;
20-
import java.util.Optional;
2120
import java.util.concurrent.TimeoutException;
2221
import javax.annotation.Nullable;
2322
import okhttp3.ResponseBody;
@@ -37,17 +36,15 @@ final class VertexAiClient {
3736
}
3837

3938
VertexAiClient() {
40-
this.apiClient =
41-
new HttpApiClient(Optional.empty(), Optional.empty(), Optional.empty(), Optional.empty());
39+
this.apiClient = new HttpApiClient((String) null, null, null, null);
4240
}
4341

4442
VertexAiClient(
4543
String project,
4644
String location,
47-
Optional<GoogleCredentials> credentials,
48-
Optional<HttpOptions> httpOptions) {
49-
this.apiClient =
50-
new HttpApiClient(Optional.of(project), Optional.of(location), credentials, httpOptions);
45+
@Nullable GoogleCredentials credentials,
46+
@Nullable HttpOptions httpOptions) {
47+
this.apiClient = new HttpApiClient(project, location, credentials, httpOptions);
5148
}
5249

5350
Maybe<JsonNode> createSession(

core/src/main/java/com/google/adk/sessions/VertexAiSessionService.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
import java.util.concurrent.ConcurrentMap;
4141
import java.util.regex.Matcher;
4242
import java.util.regex.Pattern;
43-
import javax.annotation.Nullable;
43+
import org.jspecify.annotations.Nullable;
4444

4545
/** Connects to the managed Vertex AI Session Service. */
4646
// TODO: Use the genai HttpApiClient and ApiResponse methods once they are public.
@@ -65,8 +65,8 @@ public VertexAiSessionService() {
6565
public VertexAiSessionService(
6666
String project,
6767
String location,
68-
Optional<GoogleCredentials> credentials,
69-
Optional<HttpOptions> httpOptions) {
68+
@Nullable GoogleCredentials credentials,
69+
@Nullable HttpOptions httpOptions) {
7070
this.client = new VertexAiClient(project, location, credentials, httpOptions);
7171
}
7272

0 commit comments

Comments
 (0)