From 615362d74f17f104a8f5feb1f82796fb95d389f4 Mon Sep 17 00:00:00 2001 From: detmerl Date: Tue, 17 Sep 2024 10:23:31 -0400 Subject: [PATCH] cleanup --- .../com/google/api/gax/rpc/ClientContext.java | 12 ++++++----- .../google/api/gax/rpc/ClientSettings.java | 6 +++--- .../com/google/api/gax/rpc/StubSettings.java | 20 +++++-------------- .../google/api/gax/rpc/ClientContextTest.java | 14 +++++-------- 4 files changed, 20 insertions(+), 32 deletions(-) diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java index 3ff919eb2db..ac756ec567e 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientContext.java @@ -180,7 +180,7 @@ public static ClientContext create(StubSettings settings) throws IOException { Credentials credentials = settings.getCredentialsProvider().getCredentials(); if (apiKey != null && credentials != null) { throw new IllegalArgumentException( - "You can not provide both ApiKey and Credentials for a client."); + "You can not provide both ApiKey and Credentials for a client."); } if (apiKey != null) { // if API key exists it becomes the default credential @@ -234,7 +234,6 @@ public static ClientContext create(StubSettings settings) throws IOException { ApiCallContext defaultCallContext = transportChannel.getEmptyCallContext().withTransportChannel(transportChannel); - if (credentials != null) { defaultCallContext = defaultCallContext.withCredentials(credentials); } @@ -293,7 +292,11 @@ public static ClientContext create(StubSettings settings) throws IOException { .build(); } - private static Credentials getGdchCredentials(String settingsGdchApiAudience, String endpoint, Credentials credentials) throws IOException { + /** + * Constructs a new {@link Credentials} object based on credentials provided with a GDC-H audience + */ + private static Credentials getGdchCredentials( + String settingsGdchApiAudience, String endpoint, Credentials credentials) throws IOException { String audienceString; if (!Strings.isNullOrEmpty(settingsGdchApiAudience)) { audienceString = settingsGdchApiAudience; @@ -307,8 +310,7 @@ private static Credentials getGdchCredentials(String settingsGdchApiAudience, St try { gdchAudienceUri = URI.create(audienceString); } catch (IllegalArgumentException ex) { // thrown when passing a malformed uri string - throw new IllegalArgumentException( - "The GDC-H API audience string is not a valid URI", ex); + throw new IllegalArgumentException("The GDC-H API audience string is not a valid URI", ex); } credentials = ((GdchCredentials) credentials).createWithGdchAudience(gdchAudienceUri); return credentials; diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java index 2dc915d50e8..97f1338f33c 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/ClientSettings.java @@ -65,9 +65,7 @@ public final StubSettings getStubSettings() { return stubSettings; } - /** - * @deprecated Please use {@link #getBackgroundExecutorProvider()} - */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()} */ @Deprecated public final ExecutorProvider getExecutorProvider() { return stubSettings.getExecutorProvider(); @@ -314,6 +312,8 @@ public B setGdchApiAudience(@Nullable String gdchApiAudience) { /** * Sets the API key. The API key will get translated to an [ApiKeyCredentials] and stored in * [CallContext]. + * + * Note: you can not set an API key and credentials object in the same Settings. It will fail when creating the client. */ public B setApiKey(String apiKey) { stubSettings.setApiKey(apiKey); diff --git a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java index 1b88cf552a4..0876f353469 100644 --- a/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java +++ b/gax-java/gax/src/main/java/com/google/api/gax/rpc/StubSettings.java @@ -132,9 +132,7 @@ private EndpointContext buildEndpointContext(Builder builder) { } } - /** - * @deprecated Please use {@link #getBackgroundExecutorProvider()}. - */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ @Deprecated public final ExecutorProvider getExecutorProvider() { return deprecatedExecutorProviderSet ? backgroundExecutorProvider : null; @@ -176,23 +174,17 @@ protected String getServiceName() { return ""; } - /** - * @return the fully resolved universe domain used by the client - */ + /** @return the fully resolved universe domain used by the client */ public final String getUniverseDomain() { return endpointContext.resolvedUniverseDomain(); } - /** - * @return the fully resolved endpoint used by the client - */ + /** @return the fully resolved endpoint used by the client */ public String getEndpoint() { return endpointContext.resolvedEndpoint(); } - /** - * @return the newly created EndpointContext - */ + /** @return the newly created EndpointContext */ @InternalApi final EndpointContext getEndpointContext() { return endpointContext; @@ -605,9 +597,7 @@ public B setApiKey(String apiKey) { return self(); } - /** - * @deprecated Please use {@link #getBackgroundExecutorProvider()}. - */ + /** @deprecated Please use {@link #getBackgroundExecutorProvider()}. */ @Deprecated public ExecutorProvider getExecutorProvider() { return deprecatedExecutorProviderSet ? backgroundExecutorProvider : null; diff --git a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java index 294f0a64324..3101f2c910a 100644 --- a/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java +++ b/gax-java/gax/src/test/java/com/google/api/gax/rpc/ClientContextTest.java @@ -62,8 +62,6 @@ import java.util.concurrent.Executor; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.ScheduledThreadPoolExecutor; - -import org.apache.http.impl.client.SystemDefaultCredentialsProvider; import org.junit.jupiter.api.Test; import org.mockito.Mockito; @@ -1080,7 +1078,6 @@ public void testSetApiKey_createsApiCredentials() throws IOException { new FakeTransportProvider( transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); builder.setTransportChannelProvider(transportProvider); - HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); builder.setHeaderProvider(headerProvider); @@ -1099,10 +1096,9 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce InterceptingExecutor executor = new InterceptingExecutor(1); FakeTransportChannel transportChannel = FakeTransportChannel.create(new FakeChannel()); FakeTransportProvider transportProvider = - new FakeTransportProvider( - transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); + new FakeTransportProvider( + transportChannel, executor, true, ImmutableMap.of(), null, DEFAULT_ENDPOINT); builder.setTransportChannelProvider(transportProvider); - HeaderProvider headerProvider = Mockito.mock(HeaderProvider.class); Mockito.when(headerProvider.getHeaders()).thenReturn(ImmutableMap.of()); builder.setHeaderProvider(headerProvider); @@ -1110,11 +1106,11 @@ void testCreateClient_throwsErrorIfApiKeyAndCredentialsAreProvided() throws Exce builder.setCredentialsProvider(Mockito.mock(CredentialsProvider.class)); try { - ClientContext context = ClientContext.create(builder.build()); + ClientContext.create(builder.build()); fail("No exception raised"); } catch (IllegalArgumentException e) { - assert( - e.getMessage().contains("You can not provide both ApiKey and Credentials for a client.")); + assert (e.getMessage() + .contains("You can not provide both ApiKey and Credentials for a client.")); } } }