18
18
19
19
import static com .google .cloud .datastore .Validator .validateNamespace ;
20
20
21
+ import com .google .api .core .BetaApi ;
21
22
import com .google .api .gax .core .CredentialsProvider ;
22
23
import com .google .api .gax .grpc .InstantiatingGrpcChannelProvider ;
23
24
import com .google .api .gax .rpc .TransportChannelProvider ;
@@ -74,15 +75,8 @@ public ServiceRpc create(DatastoreOptions options) {
74
75
try {
75
76
if (options .getTransportOptions () instanceof GrpcTransportOptions ) {
76
77
return new GrpcDatastoreRpc (options );
77
- } else if (options .getTransportOptions () instanceof HttpTransportOptions ) {
78
- // todo see if we can remove this check
79
- if (DatastoreUtils .isEmulator (options )) {
80
- throw new IllegalArgumentException ("Only GRPC channels are allowed for emulator." );
81
- }
82
- return new HttpDatastoreRpc (options );
83
78
} else {
84
- throw new IllegalArgumentException (
85
- "unknown transport type: " + options .getTransportOptions ());
79
+ return new HttpDatastoreRpc (options );
86
80
}
87
81
} catch (IOException e ) {
88
82
throw new RuntimeException (e );
@@ -96,6 +90,8 @@ public static class Builder extends ServiceOptions.Builder<Datastore, DatastoreO
96
90
private String databaseId ;
97
91
private TransportChannelProvider channelProvider = null ;
98
92
private CredentialsProvider credentialsProvider = null ;
93
+ private String host ;
94
+ private TransportOptions transportOptions ;
99
95
100
96
private Builder () {}
101
97
@@ -109,15 +105,33 @@ private Builder(DatastoreOptions options) {
109
105
110
106
@ Override
111
107
public Builder setTransportOptions (TransportOptions transportOptions ) {
108
+ if (!(transportOptions instanceof HttpTransportOptions )) {
109
+ throw new IllegalArgumentException (
110
+ "Only http transport is allowed for " + API_SHORT_NAME + "." );
111
+ }
112
+ this .transportOptions = transportOptions ;
113
+ return super .setTransportOptions (transportOptions );
114
+ }
115
+
116
+ @ BetaApi
117
+ public Builder setTransportOptions (GrpcTransportOptions transportOptions ) {
118
+ this .transportOptions = transportOptions ;
112
119
return super .setTransportOptions (transportOptions );
113
120
}
114
121
122
+ @ Override
123
+ public Builder setHost (String host ) {
124
+ this .host = host ;
125
+ return super .setHost (host );
126
+ }
127
+
115
128
/**
116
129
* Sets the {@link TransportChannelProvider} to use with this Datastore client.
117
130
*
118
131
* @param channelProvider A InstantiatingGrpcChannelProvider object that defines the transport
119
132
* provider for this client.
120
133
*/
134
+ @ BetaApi
121
135
public Builder setChannelProvider (TransportChannelProvider channelProvider ) {
122
136
this .channelProvider = validateChannelProvider (channelProvider );
123
137
return this ;
@@ -129,13 +143,17 @@ public Builder setChannelProvider(TransportChannelProvider channelProvider) {
129
143
* @param credentialsProvider A CredentialsProvider object that defines the credential provider
130
144
* for this client.
131
145
*/
146
+ @ BetaApi
132
147
public Builder setCredentialsProvider (CredentialsProvider credentialsProvider ) {
133
148
this .credentialsProvider = credentialsProvider ;
134
149
return this ;
135
150
}
136
151
137
152
@ Override
138
153
public DatastoreOptions build () {
154
+ if (this .host == null && this .transportOptions instanceof GrpcTransportOptions ) {
155
+ this .setHost (DatastoreSettings .getDefaultEndpoint ());
156
+ }
139
157
return new DatastoreOptions (this );
140
158
}
141
159
@@ -153,7 +171,7 @@ public Builder setDatabaseId(String databaseId) {
153
171
154
172
private static TransportChannelProvider validateChannelProvider (
155
173
TransportChannelProvider channelProvider ) {
156
- if (!(channelProvider instanceof InstantiatingGrpcChannelProvider )) {
174
+ if (channelProvider != null && !(channelProvider instanceof InstantiatingGrpcChannelProvider )) {
157
175
throw new IllegalArgumentException (
158
176
"Only GRPC channels are allowed for " + API_SHORT_NAME + "." );
159
177
}
@@ -165,7 +183,6 @@ private DatastoreOptions(Builder builder) {
165
183
namespace = MoreObjects .firstNonNull (builder .namespace , defaultNamespace ());
166
184
databaseId = MoreObjects .firstNonNull (builder .databaseId , DEFAULT_DATABASE_ID );
167
185
168
- // todo see if we can update this
169
186
if (getTransportOptions () instanceof HttpTransportOptions
170
187
&& (builder .channelProvider != null || builder .credentialsProvider != null )) {
171
188
throw new IllegalArgumentException (
@@ -195,7 +212,7 @@ public TransportChannelProvider getTransportChannelProvider() {
195
212
@ Override
196
213
protected String getDefaultHost () {
197
214
String host = System .getProperty (LOCAL_HOST_ENV_VAR , System .getenv (LOCAL_HOST_ENV_VAR ));
198
- return host != null ? host : DatastoreSettings . getDefaultEndpoint () ;
215
+ return host != null ? host : com . google . datastore . v1 . client . DatastoreFactory . DEFAULT_HOST ;
199
216
}
200
217
201
218
@ Override
@@ -222,8 +239,8 @@ public TransportOptions getDefaultTransportOptions() {
222
239
return TRANSPORT_OPTIONS ;
223
240
}
224
241
225
- public static GrpcTransportOptions .Builder getDefaultTransportOptionsBuilder () {
226
- return GrpcTransportOptions .newBuilder ();
242
+ public static HttpTransportOptions .Builder getDefaultTransportOptionsBuilder () {
243
+ return HttpTransportOptions .newBuilder ();
227
244
}
228
245
}
229
246
0 commit comments