Skip to content

Commit b9d8555

Browse files
Third round of updates to address PR comments
* ApiUtils: Fixing usage of credentials when channel is null * ApiUtils: Function rename for clarity * ApiUtils, ServiceApiSettings: improving comments * ApiCallable: Removing obsolete stuff * not returning 'this' from LocalPubsubHelper.start()
1 parent add0cc3 commit b9d8555

File tree

7 files changed

+57
-54
lines changed

7 files changed

+57
-54
lines changed

gcloud-java-gax/src/main/java/io/gapi/gax/grpc/ApiCallable.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,6 @@
6262
public abstract class ApiCallable<RequestT, ResponseT> {
6363

6464
// TODO(wrwg): Support interceptors and method/call option configurations.
65-
// TODO(wrwg): gather more feedback whether the overload with java.util.Concurrent hurts that
66-
// much that we want to rename this into ClientCallable or such.
6765

6866
// Subclass Contract
6967
// =================
@@ -390,20 +388,4 @@ public ApiCallable<RequestT, ResponseT> retrying() {
390388
return new PageStreamingCallable<RequestT, ResponseT, ResourceT>(this, pageDescriptor);
391389
}
392390

393-
/**
394-
* Returns a callable which behaves the same as {@link #pageStreaming(PageDescriptor)}, with
395-
* the page descriptor attempted to derive from the callable descriptor.
396-
*
397-
* @throws IllegalArgumentException if a page descriptor is not derivable.
398-
*/
399-
public <ResourceT> ApiCallable<RequestT, ResourceT>
400-
pageStreaming(Class<ResourceT> resourceType) {
401-
PageDescriptor<RequestT, ResponseT, ResourceT> pageDescriptor =
402-
getDescriptor() != null ? getDescriptor().getPageDescriptor(resourceType) : null;
403-
if (pageDescriptor == null) {
404-
throw new IllegalArgumentException(String.format(
405-
"cannot derive page descriptor for '%s'", this));
406-
}
407-
return pageStreaming(pageDescriptor);
408-
}
409391
}

gcloud-java-gax/src/main/java/io/gapi/gax/grpc/ServiceApiSettings.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,20 +35,23 @@
3535

3636
import io.grpc.ManagedChannel;
3737

38+
/**
39+
* A settings class to configure a service api class.
40+
*/
3841
public class ServiceApiSettings {
3942
private boolean isIdempotentRetrying;
4043

4144
private Credentials credentials;
4245

43-
private String servicePath;
46+
private String serviceAddress;
4447
private int port;
4548

4649
private ManagedChannel channel;
4750

4851
public ServiceApiSettings() {
4952
isIdempotentRetrying = true;
5053
credentials = null;
51-
servicePath = null;
54+
serviceAddress = null;
5255
port = 0;
5356
}
5457

@@ -69,7 +72,8 @@ public boolean getIsIdempotentRetrying() {
6972

7073
/**
7174
* Sets the credentials to use in order to call the service. The default is to acquire
72-
* the credentials using GoogleCredentials.getApplicationDefault().
75+
* the credentials using GoogleCredentials.getApplicationDefault(). These credentials
76+
* will not be used if the channel is set.
7377
*/
7478
public ServiceApiSettings setCredentials(Credentials credentials) {
7579
this.credentials = credentials;
@@ -81,19 +85,19 @@ public Credentials getCredentials() {
8185
}
8286

8387
/**
84-
* The path used to reach the service.
88+
* The path used to reach the service. This value will not be used if the channel is set.
8589
*/
86-
public ServiceApiSettings setServicePath(String servicePath) {
87-
this.servicePath = servicePath;
90+
public ServiceApiSettings setServiceAddress(String serviceAddress) {
91+
this.serviceAddress = serviceAddress;
8892
return this;
8993
}
9094

91-
public String getServicePath() {
92-
return servicePath;
95+
public String getServiceAddress() {
96+
return serviceAddress;
9397
}
9498

9599
/**
96-
* The port used to reach the service.
100+
* The port used to reach the service. This value will not be used if the channel is set.
97101
*/
98102
public ServiceApiSettings setPort(int port) {
99103
this.port = port;

gcloud-java-gax/src/main/java/io/gapi/gax/internal/ApiUtils.java

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,7 @@
3131

3232
package io.gapi.gax.internal;
3333

34-
import java.io.IOException;
35-
import java.util.Arrays;
36-
import java.util.Collection;
37-
import java.util.List;
38-
import java.util.concurrent.Executors;
39-
34+
import com.google.auth.Credentials;
4035
import com.google.auth.oauth2.GoogleCredentials;
4136
import com.google.common.collect.Lists;
4237

@@ -48,6 +43,11 @@
4843
import io.grpc.netty.NegotiationType;
4944
import io.grpc.netty.NettyChannelBuilder;
5045

46+
import java.io.IOException;
47+
import java.util.Arrays;
48+
import java.util.List;
49+
import java.util.concurrent.Executors;
50+
5151
public class ApiUtils {
5252

5353
// TODO(wgg): make this configurable
@@ -63,17 +63,26 @@ public static <RequestT, ResponseT> ApiCallable<RequestT, ResponseT> prepareIdem
6363
}
6464

6565
/**
66-
* Creates a channel for the given path, address and port.
66+
* Acquires application-default credentials, applying the given scopes if the
67+
* credentials require scopes.
68+
*/
69+
public static Credentials credentialsWithScopes(String scopes[]) throws IOException {
70+
List<String> scopeList = Arrays.asList(scopes);
71+
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
72+
if (credentials.createScopedRequired()) {
73+
credentials = credentials.createScoped(scopeList);
74+
}
75+
return credentials;
76+
}
77+
78+
/**
79+
* Creates a channel for the given address, port, and credentials.
6780
*/
68-
public static ManagedChannel createChannel(String address, int port, Collection<String> scopes)
81+
public static ManagedChannel createChannel(String address, int port, Credentials credentials)
6982
throws IOException {
7083
List<ClientInterceptor> interceptors = Lists.newArrayList();
7184
//TODO: MIGRATION interceptors.add(ChannelFactory.authorityInterceptor(address));
7285

73-
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
74-
if (credentials.createScopedRequired()) {
75-
credentials = credentials.createScoped(scopes);
76-
}
7786
interceptors.add(new ClientAuthInterceptor(credentials,
7887
Executors.newFixedThreadPool(AUTH_THREADS)));
7988

@@ -84,23 +93,31 @@ public static ManagedChannel createChannel(String address, int port, Collection<
8493
.build();
8594
}
8695

87-
public static ServiceApiSettings settingsWithChannels(ServiceApiSettings settings,
88-
String defaultServicePath, int defaultServicePort, String scopes[]) throws IOException {
96+
/**
97+
* Creates a new instance of ServiceApiSettings with all fields populated, using
98+
* the given defaults if the corresponding values are not set on ServiceApiSettings.
99+
*/
100+
public static ServiceApiSettings populateSettings(ServiceApiSettings settings,
101+
String defaultServiceAddress, int defaultServicePort, String scopes[]) throws IOException {
89102
ManagedChannel channel = settings.getChannel();
90103

91104
if (channel == null) {
92-
String servicePath = defaultServicePath;
93-
if (settings.getServicePath() != null) {
94-
servicePath = settings.getServicePath();
105+
String servicePath = settings.getServiceAddress();
106+
if (servicePath == null) {
107+
servicePath = defaultServiceAddress;
108+
}
109+
110+
int port = settings.getPort();
111+
if (port == 0) {
112+
port = defaultServicePort;
95113
}
96114

97-
int port = defaultServicePort;
98-
if (settings.getPort() != 0) {
99-
port = settings.getPort();
115+
Credentials credentials = settings.getCredentials();
116+
if (credentials == null) {
117+
credentials = credentialsWithScopes(scopes);
100118
}
101119

102-
List<String> scopeList = Arrays.asList(scopes);
103-
channel = ApiUtils.createChannel(servicePath, port, scopeList);
120+
channel = ApiUtils.createChannel(servicePath, port, credentials);
104121
}
105122

106123
return new ServiceApiSettings()

gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/PublisherApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public static PublisherApi create(ServiceApiSettings settings) throws IOExceptio
191191
* easy to make a subclass, but otherwise, the static factory methods should be preferred.
192192
*/
193193
protected PublisherApi(ServiceApiSettings settings) throws IOException {
194-
ServiceApiSettings internalSettings = ApiUtils.settingsWithChannels(settings,
194+
ServiceApiSettings internalSettings = ApiUtils.populateSettings(settings,
195195
SERVICE_ADDRESS, DEFAULT_SERVICE_PORT, ALL_SCOPES);
196196
this.settings = internalSettings;
197197
this.channel = internalSettings.getChannel();

gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/spi/SubscriberApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ public static SubscriberApi create(ServiceApiSettings settings) throws IOExcepti
172172
* easy to make a subclass, but otherwise, the static factory methods should be preferred.
173173
*/
174174
protected SubscriberApi(ServiceApiSettings settings) throws IOException {
175-
ServiceApiSettings internalSettings = ApiUtils.settingsWithChannels(settings,
175+
ServiceApiSettings internalSettings = ApiUtils.populateSettings(settings,
176176
SERVICE_ADDRESS, DEFAULT_SERVICE_PORT, ALL_SCOPES);
177177
this.settings = internalSettings;
178178
this.channel = internalSettings.getChannel();

gcloud-java-pubsub/src/main/java/com/google/gcloud/pubsub/testing/LocalPubsubHelper.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ public LocalPubsubHelper(String addressString) {
4343
/**
4444
* Starts the in-memory service.
4545
*/
46-
public LocalPubsubHelper start() {
46+
public void start() {
4747
try {
4848
server.start();
4949
} catch (IOException ex) {
5050
throw new RuntimeException(ex);
5151
}
52-
return this;
5352
}
5453

5554
/**

gcloud-java-pubsub/src/test/java/com/google/gcloud/pubsub/spi/PublisherApiTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class PublisherApiTest {
3838

3939
@BeforeClass
4040
public static void startStaticServer() {
41-
pubsubHelper = new LocalPubsubHelper("in-process-1").start();
41+
pubsubHelper = new LocalPubsubHelper("in-process-1");
42+
pubsubHelper.start();
4243
}
4344

4445
@AfterClass

0 commit comments

Comments
 (0)