Skip to content

Commit

Permalink
Merge pull request #345 from garrettjonesgoogle/pubsub-alpha
Browse files Browse the repository at this point in the history
Generated Pub/Sub client classes and unit tests
  • Loading branch information
aozarov committed Nov 20, 2015
2 parents 9e3b1e4 + b9d8555 commit dea33f5
Show file tree
Hide file tree
Showing 12 changed files with 1,735 additions and 53 deletions.
2 changes: 1 addition & 1 deletion gcloud-java-gax/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>generated/src/main</source>
<source>generated/src/main/java</source>
</sources>
</configuration>
</execution>
Expand Down
18 changes: 0 additions & 18 deletions gcloud-java-gax/src/main/java/io/gapi/gax/grpc/ApiCallable.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@
public abstract class ApiCallable<RequestT, ResponseT> {

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

// Subclass Contract
// =================
Expand Down Expand Up @@ -390,20 +388,4 @@ public ApiCallable<RequestT, ResponseT> retrying() {
return new PageStreamingCallable<RequestT, ResponseT, ResourceT>(this, pageDescriptor);
}

/**
* Returns a callable which behaves the same as {@link #pageStreaming(PageDescriptor)}, with
* the page descriptor attempted to derive from the callable descriptor.
*
* @throws IllegalArgumentException if a page descriptor is not derivable.
*/
public <ResourceT> ApiCallable<RequestT, ResourceT>
pageStreaming(Class<ResourceT> resourceType) {
PageDescriptor<RequestT, ResponseT, ResourceT> pageDescriptor =
getDescriptor() != null ? getDescriptor().getPageDescriptor(resourceType) : null;
if (pageDescriptor == null) {
throw new IllegalArgumentException(String.format(
"cannot derive page descriptor for '%s'", this));
}
return pageStreaming(pageDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,23 @@

import io.grpc.ManagedChannel;

/**
* A settings class to configure a service api class.
*/
public class ServiceApiSettings {
private boolean isIdempotentRetrying;

private Credentials credentials;

private String servicePath;
private String serviceAddress;
private int port;

private ManagedChannel channel;

public ServiceApiSettings() {
isIdempotentRetrying = true;
credentials = null;
servicePath = null;
serviceAddress = null;
port = 0;
}

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

/**
* Sets the credentials to use in order to call the service. The default is to acquire
* the credentials using GoogleCredentials.getApplicationDefault().
* the credentials using GoogleCredentials.getApplicationDefault(). These credentials
* will not be used if the channel is set.
*/
public ServiceApiSettings setCredentials(Credentials credentials) {
this.credentials = credentials;
Expand All @@ -81,19 +85,19 @@ public Credentials getCredentials() {
}

/**
* The path used to reach the service.
* The path used to reach the service. This value will not be used if the channel is set.
*/
public ServiceApiSettings setServicePath(String servicePath) {
this.servicePath = servicePath;
public ServiceApiSettings setServiceAddress(String serviceAddress) {
this.serviceAddress = serviceAddress;
return this;
}

public String getServicePath() {
return servicePath;
public String getServiceAddress() {
return serviceAddress;
}

/**
* The port used to reach the service.
* The port used to reach the service. This value will not be used if the channel is set.
*/
public ServiceApiSettings setPort(int port) {
this.port = port;
Expand All @@ -105,8 +109,10 @@ public int getPort() {
}

/**
* An instance of ManagedChannel; shutdown will be called on this channel when
* the instance of LoggingServiceApi is shut down.
* The channel used to send requests to the service. Whichever service api class that
* this instance of ServiceApiSettings is passed to will call shutdown() on this
* channel. This injection mechanism is intended for use by unit tests to override
* the channel that would be created by default for real calls to the service.
*/
public ServiceApiSettings setChannel(ManagedChannel channel) {
this.channel = channel;
Expand Down
61 changes: 39 additions & 22 deletions gcloud-java-gax/src/main/java/io/gapi/gax/internal/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@

package io.gapi.gax.internal;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executors;

import com.google.auth.Credentials;
import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.collect.Lists;

Expand All @@ -48,6 +43,11 @@
import io.grpc.netty.NegotiationType;
import io.grpc.netty.NettyChannelBuilder;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Executors;

public class ApiUtils {

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

/**
* Creates a channel for the given path, address and port.
* Acquires application-default credentials, applying the given scopes if the
* credentials require scopes.
*/
public static Credentials credentialsWithScopes(String scopes[]) throws IOException {
List<String> scopeList = Arrays.asList(scopes);
GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
if (credentials.createScopedRequired()) {
credentials = credentials.createScoped(scopeList);
}
return credentials;
}

/**
* Creates a channel for the given address, port, and credentials.
*/
public static ManagedChannel createChannel(String address, int port, Collection<String> scopes)
public static ManagedChannel createChannel(String address, int port, Credentials credentials)
throws IOException {
List<ClientInterceptor> interceptors = Lists.newArrayList();
//TODO: MIGRATION interceptors.add(ChannelFactory.authorityInterceptor(address));

GoogleCredentials credentials = GoogleCredentials.getApplicationDefault();
if (credentials.createScopedRequired()) {
credentials = credentials.createScoped(scopes);
}
interceptors.add(new ClientAuthInterceptor(credentials,
Executors.newFixedThreadPool(AUTH_THREADS)));

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

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

if (channel == null) {
String servicePath = defaultServicePath;
if (settings.getServicePath() != null) {
servicePath = settings.getServicePath();
String servicePath = settings.getServiceAddress();
if (servicePath == null) {
servicePath = defaultServiceAddress;
}

int port = settings.getPort();
if (port == 0) {
port = defaultServicePort;
}

int port = defaultServicePort;
if (settings.getPort() != 0) {
port = settings.getPort();
Credentials credentials = settings.getCredentials();
if (credentials == null) {
credentials = credentialsWithScopes(scopes);
}

List<String> scopeList = Arrays.asList(scopes);
channel = ApiUtils.createChannel(servicePath, port, scopeList);
channel = ApiUtils.createChannel(servicePath, port, credentials);
}

return new ServiceApiSettings()
Expand Down
8 changes: 7 additions & 1 deletion gcloud-java-pubsub/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
<artifactId>auto-value</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand All @@ -45,7 +51,7 @@
<goals><goal>add-source</goal></goals>
<configuration>
<sources>
<source>generated/src/main</source>
<source>generated/src/main/java</source>
</sources>
</configuration>
</execution>
Expand Down
Loading

0 comments on commit dea33f5

Please sign in to comment.