@@ -183,40 +183,25 @@ public static ClientContext create(StubSettings settings) throws IOException {
183
183
"You can not provide both ApiKey and Credentials for a client." );
184
184
}
185
185
if (apiKey != null ) {
186
+ // if API key exists it becomes the default credential
186
187
credentials = ApiKeyCredentials .create (settings .getApiKey ());
187
- } else {
188
- // check if need to adjust credentials for Gdch audience
189
- String settingsGdchApiAudience = settings .getGdchApiAudience ();
190
- boolean usingGDCH = credentials instanceof GdchCredentials ;
191
- if (usingGDCH ) {
192
- // Can only determine if the GDC-H is being used via the Credentials. The Credentials object
193
- // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the
194
- // endpointContext only on GDC-H flows.
195
- endpointContext = endpointContext .withGDCH ();
196
- // Resolve the new endpoint with the GDC-H flow
197
- endpoint = endpointContext .resolvedEndpoint ();
198
- // We recompute the GdchCredentials with the audience
199
- String audienceString ;
200
- if (!Strings .isNullOrEmpty (settingsGdchApiAudience )) {
201
- audienceString = settingsGdchApiAudience ;
202
- } else if (!Strings .isNullOrEmpty (endpoint )) {
203
- audienceString = endpoint ;
204
- } else {
205
- throw new IllegalArgumentException ("Could not infer GDCH api audience from settings" );
206
- }
207
-
208
- URI gdchAudienceUri ;
209
- try {
210
- gdchAudienceUri = URI .create (audienceString );
211
- } catch (IllegalArgumentException ex ) { // thrown when passing a malformed uri string
212
- throw new IllegalArgumentException (
213
- "The GDC-H API audience string is not a valid URI" , ex );
214
- }
215
- credentials = ((GdchCredentials ) credentials ).createWithGdchAudience (gdchAudienceUri );
216
- } else if (!Strings .isNullOrEmpty (settingsGdchApiAudience )) {
217
- throw new IllegalArgumentException (
218
- "GDC-H API audience can only be set when using GdchCredentials" );
219
- }
188
+ }
189
+
190
+ // check if need to adjust credentials/endpoint/endpointContext for GDC-H
191
+ String settingsGdchApiAudience = settings .getGdchApiAudience ();
192
+ boolean usingGDCH = credentials instanceof GdchCredentials ;
193
+ if (usingGDCH ) {
194
+ // Can only determine if the GDC-H is being used via the Credentials. The Credentials object
195
+ // is resolved in the ClientContext and must be passed to the EndpointContext. Rebuild the
196
+ // endpointContext only on GDC-H flows.
197
+ endpointContext = endpointContext .withGDCH ();
198
+ // Resolve the new endpoint with the GDC-H flow
199
+ endpoint = endpointContext .resolvedEndpoint ();
200
+ // We recompute the GdchCredentials with the audience
201
+ credentials = getGdchCredentials (settingsGdchApiAudience , endpoint , credentials );
202
+ } else if (!Strings .isNullOrEmpty (settingsGdchApiAudience )) {
203
+ throw new IllegalArgumentException (
204
+ "GDC-H API audience can only be set when using GdchCredentials" );
220
205
}
221
206
222
207
if (settings .getQuotaProjectId () != null && credentials != null ) {
@@ -304,6 +289,27 @@ public static ClientContext create(StubSettings settings) throws IOException {
304
289
.build ();
305
290
}
306
291
292
+ private static Credentials getGdchCredentials (String settingsGdchApiAudience , String endpoint , Credentials credentials ) throws IOException {
293
+ String audienceString ;
294
+ if (!Strings .isNullOrEmpty (settingsGdchApiAudience )) {
295
+ audienceString = settingsGdchApiAudience ;
296
+ } else if (!Strings .isNullOrEmpty (endpoint )) {
297
+ audienceString = endpoint ;
298
+ } else {
299
+ throw new IllegalArgumentException ("Could not infer GDCH api audience from settings" );
300
+ }
301
+
302
+ URI gdchAudienceUri ;
303
+ try {
304
+ gdchAudienceUri = URI .create (audienceString );
305
+ } catch (IllegalArgumentException ex ) { // thrown when passing a malformed uri string
306
+ throw new IllegalArgumentException (
307
+ "The GDC-H API audience string is not a valid URI" , ex );
308
+ }
309
+ credentials = ((GdchCredentials ) credentials ).createWithGdchAudience (gdchAudienceUri );
310
+ return credentials ;
311
+ }
312
+
307
313
/**
308
314
* Getting a header map from HeaderProvider and InternalHeaderProvider from settings with Quota
309
315
* Project Id.
0 commit comments