Skip to content

Commit dea33f5

Browse files
committed
Merge pull request #345 from garrettjonesgoogle/pubsub-alpha
Generated Pub/Sub client classes and unit tests
2 parents 9e3b1e4 + b9d8555 commit dea33f5

File tree

12 files changed

+1735
-53
lines changed

12 files changed

+1735
-53
lines changed

gcloud-java-gax/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
<goals><goal>add-source</goal></goals>
6464
<configuration>
6565
<sources>
66-
<source>generated/src/main</source>
66+
<source>generated/src/main/java</source>
6767
</sources>
6868
</configuration>
6969
</execution>

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: 17 additions & 11 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;
@@ -105,8 +109,10 @@ public int getPort() {
105109
}
106110

107111
/**
108-
* An instance of ManagedChannel; shutdown will be called on this channel when
109-
* the instance of LoggingServiceApi is shut down.
112+
* The channel used to send requests to the service. Whichever service api class that
113+
* this instance of ServiceApiSettings is passed to will call shutdown() on this
114+
* channel. This injection mechanism is intended for use by unit tests to override
115+
* the channel that would be created by default for real calls to the service.
110116
*/
111117
public ServiceApiSettings setChannel(ManagedChannel channel) {
112118
this.channel = channel;

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/pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@
3232
<artifactId>auto-value</artifactId>
3333
<version>1.1</version>
3434
</dependency>
35+
<dependency>
36+
<groupId>junit</groupId>
37+
<artifactId>junit</artifactId>
38+
<version>4.12</version>
39+
<scope>test</scope>
40+
</dependency>
3541
</dependencies>
3642
<build>
3743
<plugins>
@@ -45,7 +51,7 @@
4551
<goals><goal>add-source</goal></goals>
4652
<configuration>
4753
<sources>
48-
<source>generated/src/main</source>
54+
<source>generated/src/main/java</source>
4955
</sources>
5056
</configuration>
5157
</execution>

0 commit comments

Comments
 (0)