Skip to content
This repository has been archived by the owner on Jun 28, 2022. It is now read-only.

Commit

Permalink
java: move CredentialsProvider up to ClientSettings (#1319)
Browse files Browse the repository at this point in the history
redo of #1251.

WIP pending more tests, but all generated code's
unit tests run OK against updated gax.
  • Loading branch information
pongad authored Jun 7, 2017
1 parent 7b390ad commit 79d47ad
Show file tree
Hide file tree
Showing 11 changed files with 133 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -311,24 +311,25 @@ private GapicInterfaceContext createContext(

private void addUnitTestImports(GapicInterfaceContext context) {
ModelTypeTable typeTable = context.getModelTypeTable();
typeTable.saveNicknameFor("com.google.api.gax.core.NoCredentialsProvider");
typeTable.saveNicknameFor("com.google.api.gax.grpc.ApiException");
typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockGrpcService");
typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockServiceHelper");
typeTable.saveNicknameFor("com.google.common.collect.Lists");
typeTable.saveNicknameFor("com.google.protobuf.GeneratedMessageV3");
typeTable.saveNicknameFor("io.grpc.Status");
typeTable.saveNicknameFor("io.grpc.StatusRuntimeException");
typeTable.saveNicknameFor("java.io.IOException");
typeTable.saveNicknameFor("java.util.ArrayList");
typeTable.saveNicknameFor("java.util.Arrays");
typeTable.saveNicknameFor("java.util.concurrent.ExecutionException");
typeTable.saveNicknameFor("java.util.List");
typeTable.saveNicknameFor("org.junit.After");
typeTable.saveNicknameFor("org.junit.AfterClass");
typeTable.saveNicknameFor("org.junit.Assert");
typeTable.saveNicknameFor("org.junit.Before");
typeTable.saveNicknameFor("org.junit.BeforeClass");
typeTable.saveNicknameFor("org.junit.Test");
typeTable.saveNicknameFor("java.io.IOException");
typeTable.saveNicknameFor("java.util.ArrayList");
typeTable.saveNicknameFor("java.util.Arrays");
typeTable.saveNicknameFor("java.util.List");
typeTable.saveNicknameFor("java.util.concurrent.ExecutionException");
typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockServiceHelper");
typeTable.saveNicknameFor("com.google.api.gax.grpc.testing.MockGrpcService");
typeTable.saveNicknameFor("com.google.api.gax.grpc.ApiException");
typeTable.saveNicknameFor("com.google.common.collect.Lists");
typeTable.saveNicknameFor("com.google.protobuf.GeneratedMessageV3");
typeTable.saveNicknameFor("io.grpc.Status");
typeTable.saveNicknameFor("io.grpc.StatusRuntimeException");
if (context.getInterfaceConfig().hasPageStreamingMethods()) {
typeTable.saveNicknameFor("com.google.api.gax.core.PagedListResponse");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -440,20 +440,23 @@ private void addApiImports(GapicInterfaceContext context) {
ModelTypeTable typeTable = context.getModelTypeTable();
typeTable.saveNicknameFor("com.google.api.core.BetaApi");
typeTable.saveNicknameFor("com.google.api.gax.grpc.ChannelAndExecutor");
typeTable.saveNicknameFor("com.google.api.gax.grpc.ClientContext");
typeTable.saveNicknameFor("com.google.api.gax.grpc.UnaryCallable");
typeTable.saveNicknameFor("com.google.api.pathtemplate.PathTemplate");
typeTable.saveNicknameFor("com.google.auth.Credentials");
typeTable.saveNicknameFor("io.grpc.ManagedChannel");
typeTable.saveNicknameFor("java.io.Closeable");
typeTable.saveNicknameFor("java.io.IOException");
typeTable.saveNicknameFor("java.util.ArrayList");
typeTable.saveNicknameFor("java.util.List");
typeTable.saveNicknameFor("java.util.concurrent.ScheduledExecutorService");
typeTable.saveNicknameFor("java.util.List");
typeTable.saveNicknameFor("javax.annotation.Generated");

if (context.getInterfaceConfig().hasLongRunningOperations()) {
typeTable.saveNicknameFor("com.google.api.gax.grpc.OperationFuture");
typeTable.saveNicknameFor("com.google.api.gax.core.FixedCredentialsProvider");
typeTable.saveNicknameFor("com.google.api.gax.grpc.FixedChannelProvider");
typeTable.saveNicknameFor("com.google.api.gax.grpc.FixedExecutorProvider");
typeTable.saveNicknameFor("com.google.api.gax.grpc.OperationFuture");
typeTable.saveNicknameFor("com.google.longrunning.OperationsClient");
typeTable.saveNicknameFor("com.google.longrunning.OperationsSettings");
}
Expand Down
33 changes: 19 additions & 14 deletions src/main/resources/com/google/api/codegen/java/main.snip
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,10 @@
*
* <pre>
* <code>
* InstantiatingChannelProvider channelProvider =
* {@xapiClassDoc.settingsClassName}.defaultChannelProviderBuilder()
* {@xapiClassDoc.settingsClassName} {@xapiClassDoc.settingsVarName} =
* {@xapiClassDoc.settingsClassName}.defaultBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
* {@xapiClassDoc.settingsClassName} {@xapiClassDoc.settingsVarName} =
* {@xapiClassDoc.settingsClassName}.defaultBuilder().setChannelProvider(channelProvider).build();
* {@xapiClassDoc.apiClassName} {@xapiClassDoc.apiVarName} =
* {@xapiClassDoc.apiClassName}.create({@xapiClassDoc.settingsVarName});
* </code>
Expand Down Expand Up @@ -219,34 +217,41 @@
ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor();
this.executor = channelAndExecutor.getExecutor();
this.channel = channelAndExecutor.getChannel();
Credentials credentials = settings.getCredentialsProvider().getCredentials();

ClientContext clientContext =
ClientContext.newBuilder()
.setExecutor(this.executor)
.setChannel(this.channel)
.setCredentials(credentials)
.build();

@if xapiClass.hasLongRunningOperations
FixedExecutorProvider executorProvider = FixedExecutorProvider.create(this.executor);
FixedChannelProvider channelProvider = FixedChannelProvider.create(this.channel);
OperationsSettings operationsSettings =
OperationsSettings.defaultBuilder()
.setExecutorProvider(executorProvider)
.setChannelProvider(channelProvider)
.setExecutorProvider(FixedExecutorProvider.create(this.executor))
.setChannelProvider(FixedChannelProvider.create(this.channel))
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
this.operationsClient = OperationsClient.create(operationsSettings);
@end

@join apiCallable : xapiClass.apiCallableMembers
@switch apiCallable.type
@case "SimpleApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), clientContext);
@case "PagedApiCallable"
this.{@apiCallable.name} =
UnaryCallable.createPagedVariant(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.{@apiCallable.settingsFunctionName}(), clientContext);
@case "BatchingApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel, this.executor);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}(), clientContext);
@case "StreamingApiCallable"
this.{@apiCallable.name} = StreamingCallable.create(settings.{@apiCallable.settingsFunctionName}(), this.channel);
this.{@apiCallable.name} = StreamingCallable.create(settings.{@apiCallable.settingsFunctionName}(), clientContext);
@case "InitialOperationApiCallable"
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}().getInitialCallSettings(), this.channel, this.executor);
this.{@apiCallable.name} = UnaryCallable.create(settings.{@apiCallable.settingsFunctionName}().getInitialCallSettings(), clientContext);
@case "OperationApiCallable"
this.{@apiCallable.name} = OperationCallable.create(settings.{@apiCallable.settingsFunctionName}(),\
this.channel, this.executor, this.operationsClient);
clientContext, this.operationsClient);
@default
$unhandledCase: {@apiCallable.type}$
@end
Expand Down
13 changes: 10 additions & 3 deletions src/main/resources/com/google/api/codegen/java/settings.snip
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@
@if xsettingsClass.hasDefaultServiceAddress
.setEndpoint(getDefaultEndpoint())
@end
.setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion())
.setCredentialsProvider(defaultCredentialsProviderBuilder().build());
.setGeneratorHeader(DEFAULT_GAPIC_NAME, getGapicVersion());
}

private static String getGapicVersion() {
Expand Down Expand Up @@ -256,7 +255,8 @@
@private constructors(xsettingsClass)
private {@xsettingsClass.name}(Builder settingsBuilder) throws IOException {
super(settingsBuilder.getExecutorProvider(),
settingsBuilder.getChannelProvider());
settingsBuilder.getChannelProvider(),
settingsBuilder.getCredentialsProvider());

@join settings : xsettingsClass.callSettings
{@settings.memberName} = settingsBuilder.{@settings.settingsGetFunction}().build();
Expand Down Expand Up @@ -508,6 +508,7 @@
private Builder() {
@if xsettingsClass.hasDefaultInstance
super(defaultChannelProviderBuilder().build());
setCredentialsProvider(defaultCredentialsProviderBuilder().build());
@else
super((InstantiatingChannelProvider) null);
@end
Expand Down Expand Up @@ -612,6 +613,12 @@
return this;
}

@@Override
public Builder setCredentialsProvider(CredentialsProvider credentialsProvider) {
super.setCredentialsProvider(credentialsProvider);
return this;
}

/**
* Applies the given settings to all of the unary API methods in this service. Only
* values that are non-null will be applied, so this method is not capable
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/com/google/api/codegen/java/test.snip
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
serviceHelper.reset();
{@xapiTest.testClass.apiSettingsClassName} settings = {@xapiTest.testClass.apiSettingsClassName}.defaultBuilder()
.setChannelProvider(serviceHelper.createChannelProvider())
.setCredentialsProvider(new NoCredentialsProvider())
.build();
client = {@xapiTest.testClass.apiClassName}.create(settings);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package com.google.gcloud.pubsub.spi;

import com.google.api.core.BetaApi;
import com.google.api.gax.core.FixedCredentialsProvider;
import com.google.api.gax.grpc.ChannelAndExecutor;
import com.google.api.gax.grpc.ClientContext;
import com.google.api.gax.grpc.FixedChannelProvider;
import com.google.api.gax.grpc.FixedExecutorProvider;
import com.google.api.gax.grpc.OperationCallable;
Expand All @@ -26,6 +28,7 @@ import com.google.api.gax.grpc.StreamingCallable;
import com.google.api.gax.grpc.UnaryCallable;
import com.google.api.pathtemplate.PathTemplate;
import com.google.api.resourcenames.ResourceName;
import com.google.auth.Credentials;
import com.google.example.library.v1.AddCommentsRequest;
import com.google.example.library.v1.ArchivedBookName;
import com.google.example.library.v1.Book;
Expand Down Expand Up @@ -151,12 +154,10 @@ import javax.annotation.Generated;
*
* <pre>
* <code>
* InstantiatingChannelProvider channelProvider =
* LibrarySettings.defaultChannelProviderBuilder()
* LibrarySettings librarySettings =
* LibrarySettings.defaultBuilder()
* .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
* .build();
* LibrarySettings librarySettings =
* LibrarySettings.defaultBuilder().setChannelProvider(channelProvider).build();
* LibraryClient libraryClient =
* LibraryClient.create(librarySettings);
* </code>
Expand Down Expand Up @@ -232,53 +233,60 @@ public class LibraryClient implements AutoCloseable {
ChannelAndExecutor channelAndExecutor = settings.getChannelAndExecutor();
this.executor = channelAndExecutor.getExecutor();
this.channel = channelAndExecutor.getChannel();
Credentials credentials = settings.getCredentialsProvider().getCredentials();

ClientContext clientContext =
ClientContext.newBuilder()
.setExecutor(this.executor)
.setChannel(this.channel)
.setCredentials(credentials)
.build();

FixedExecutorProvider executorProvider = FixedExecutorProvider.create(this.executor);
FixedChannelProvider channelProvider = FixedChannelProvider.create(this.channel);
OperationsSettings operationsSettings =
OperationsSettings.defaultBuilder()
.setExecutorProvider(executorProvider)
.setChannelProvider(channelProvider)
.setExecutorProvider(FixedExecutorProvider.create(this.executor))
.setChannelProvider(FixedChannelProvider.create(this.channel))
.setCredentialsProvider(FixedCredentialsProvider.create(credentials))
.build();
this.operationsClient = OperationsClient.create(operationsSettings);

this.createShelfCallable = UnaryCallable.create(settings.createShelfSettings(), this.channel, this.executor);
this.getShelfCallable = UnaryCallable.create(settings.getShelfSettings(), this.channel, this.executor);
this.listShelvesCallable = UnaryCallable.create(settings.listShelvesSettings(), this.channel, this.executor);
this.createShelfCallable = UnaryCallable.create(settings.createShelfSettings(), clientContext);
this.getShelfCallable = UnaryCallable.create(settings.getShelfSettings(), clientContext);
this.listShelvesCallable = UnaryCallable.create(settings.listShelvesSettings(), clientContext);
this.listShelvesPagedCallable =
UnaryCallable.createPagedVariant(settings.listShelvesSettings(), this.channel, this.executor);
this.deleteShelfCallable = UnaryCallable.create(settings.deleteShelfSettings(), this.channel, this.executor);
this.mergeShelvesCallable = UnaryCallable.create(settings.mergeShelvesSettings(), this.channel, this.executor);
this.createBookCallable = UnaryCallable.create(settings.createBookSettings(), this.channel, this.executor);
this.publishSeriesCallable = UnaryCallable.create(settings.publishSeriesSettings(), this.channel, this.executor);
this.getBookCallable = UnaryCallable.create(settings.getBookSettings(), this.channel, this.executor);
this.listBooksCallable = UnaryCallable.create(settings.listBooksSettings(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.listShelvesSettings(), clientContext);
this.deleteShelfCallable = UnaryCallable.create(settings.deleteShelfSettings(), clientContext);
this.mergeShelvesCallable = UnaryCallable.create(settings.mergeShelvesSettings(), clientContext);
this.createBookCallable = UnaryCallable.create(settings.createBookSettings(), clientContext);
this.publishSeriesCallable = UnaryCallable.create(settings.publishSeriesSettings(), clientContext);
this.getBookCallable = UnaryCallable.create(settings.getBookSettings(), clientContext);
this.listBooksCallable = UnaryCallable.create(settings.listBooksSettings(), clientContext);
this.listBooksPagedCallable =
UnaryCallable.createPagedVariant(settings.listBooksSettings(), this.channel, this.executor);
this.deleteBookCallable = UnaryCallable.create(settings.deleteBookSettings(), this.channel, this.executor);
this.updateBookCallable = UnaryCallable.create(settings.updateBookSettings(), this.channel, this.executor);
this.moveBookCallable = UnaryCallable.create(settings.moveBookSettings(), this.channel, this.executor);
this.listStringsCallable = UnaryCallable.create(settings.listStringsSettings(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.listBooksSettings(), clientContext);
this.deleteBookCallable = UnaryCallable.create(settings.deleteBookSettings(), clientContext);
this.updateBookCallable = UnaryCallable.create(settings.updateBookSettings(), clientContext);
this.moveBookCallable = UnaryCallable.create(settings.moveBookSettings(), clientContext);
this.listStringsCallable = UnaryCallable.create(settings.listStringsSettings(), clientContext);
this.listStringsPagedCallable =
UnaryCallable.createPagedVariant(settings.listStringsSettings(), this.channel, this.executor);
this.addCommentsCallable = UnaryCallable.create(settings.addCommentsSettings(), this.channel, this.executor);
this.getBookFromArchiveCallable = UnaryCallable.create(settings.getBookFromArchiveSettings(), this.channel, this.executor);
this.getBookFromAnywhereCallable = UnaryCallable.create(settings.getBookFromAnywhereSettings(), this.channel, this.executor);
this.updateBookIndexCallable = UnaryCallable.create(settings.updateBookIndexSettings(), this.channel, this.executor);
this.streamShelvesCallable = StreamingCallable.create(settings.streamShelvesSettings(), this.channel);
this.streamBooksCallable = StreamingCallable.create(settings.streamBooksSettings(), this.channel);
this.discussBookCallable = StreamingCallable.create(settings.discussBookSettings(), this.channel);
this.monologAboutBookCallable = StreamingCallable.create(settings.monologAboutBookSettings(), this.channel);
this.findRelatedBooksCallable = UnaryCallable.create(settings.findRelatedBooksSettings(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.listStringsSettings(), clientContext);
this.addCommentsCallable = UnaryCallable.create(settings.addCommentsSettings(), clientContext);
this.getBookFromArchiveCallable = UnaryCallable.create(settings.getBookFromArchiveSettings(), clientContext);
this.getBookFromAnywhereCallable = UnaryCallable.create(settings.getBookFromAnywhereSettings(), clientContext);
this.updateBookIndexCallable = UnaryCallable.create(settings.updateBookIndexSettings(), clientContext);
this.streamShelvesCallable = StreamingCallable.create(settings.streamShelvesSettings(), clientContext);
this.streamBooksCallable = StreamingCallable.create(settings.streamBooksSettings(), clientContext);
this.discussBookCallable = StreamingCallable.create(settings.discussBookSettings(), clientContext);
this.monologAboutBookCallable = StreamingCallable.create(settings.monologAboutBookSettings(), clientContext);
this.findRelatedBooksCallable = UnaryCallable.create(settings.findRelatedBooksSettings(), clientContext);
this.findRelatedBooksPagedCallable =
UnaryCallable.createPagedVariant(settings.findRelatedBooksSettings(), this.channel, this.executor);
this.addTagCallable = UnaryCallable.create(settings.addTagSettings(), this.channel, this.executor);
this.addLabelCallable = UnaryCallable.create(settings.addLabelSettings(), this.channel, this.executor);
this.getBigBookCallable = UnaryCallable.create(settings.getBigBookSettings().getInitialCallSettings(), this.channel, this.executor);
this.getBigBookOperationCallable = OperationCallable.create(settings.getBigBookSettings(),this.channel, this.executor, this.operationsClient);
this.getBigNothingCallable = UnaryCallable.create(settings.getBigNothingSettings().getInitialCallSettings(), this.channel, this.executor);
this.getBigNothingOperationCallable = OperationCallable.create(settings.getBigNothingSettings(),this.channel, this.executor, this.operationsClient);
this.testOptionalRequiredFlatteningParamsCallable = UnaryCallable.create(settings.testOptionalRequiredFlatteningParamsSettings(), this.channel, this.executor);
UnaryCallable.createPagedVariant(settings.findRelatedBooksSettings(), clientContext);
this.addTagCallable = UnaryCallable.create(settings.addTagSettings(), clientContext);
this.addLabelCallable = UnaryCallable.create(settings.addLabelSettings(), clientContext);
this.getBigBookCallable = UnaryCallable.create(settings.getBigBookSettings().getInitialCallSettings(), clientContext);
this.getBigBookOperationCallable = OperationCallable.create(settings.getBigBookSettings(),clientContext, this.operationsClient);
this.getBigNothingCallable = UnaryCallable.create(settings.getBigNothingSettings().getInitialCallSettings(), clientContext);
this.getBigNothingOperationCallable = OperationCallable.create(settings.getBigNothingSettings(),clientContext, this.operationsClient);
this.testOptionalRequiredFlatteningParamsCallable = UnaryCallable.create(settings.testOptionalRequiredFlatteningParamsSettings(), clientContext);

if (settings.getChannelProvider().shouldAutoClose()) {
closeables.add(
Expand Down
Loading

0 comments on commit 79d47ad

Please sign in to comment.