3131
3232package 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 ;
4035import com .google .auth .oauth2 .GoogleCredentials ;
4136import com .google .common .collect .Lists ;
4237
4843import io .grpc .netty .NegotiationType ;
4944import 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+
5151public 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 ()
0 commit comments