1616
1717package com .google .adk .sessions ;
1818
19- import static com .google .common .base .Preconditions .checkNotNull ;
2019import static com .google .common .base .StandardSystemProperty .JAVA_VERSION ;
2120
2221import com .google .auth .oauth2 .GoogleCredentials ;
2322import com .google .common .base .Ascii ;
23+ import com .google .common .base .Strings ;
2424import com .google .common .collect .ImmutableMap ;
2525import com .google .genai .errors .GenAiIOException ;
2626import com .google .genai .types .HttpOptions ;
3535abstract 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
0 commit comments