• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      https://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.cloud.dialogflow.v2.stub;
18 
19 import static com.google.cloud.dialogflow.v2.SessionsClient.ListLocationsPagedResponse;
20 
21 import com.google.api.core.ApiFunction;
22 import com.google.api.core.ApiFuture;
23 import com.google.api.core.BetaApi;
24 import com.google.api.gax.core.GaxProperties;
25 import com.google.api.gax.core.GoogleCredentialsProvider;
26 import com.google.api.gax.core.InstantiatingExecutorProvider;
27 import com.google.api.gax.grpc.GaxGrpcProperties;
28 import com.google.api.gax.grpc.GrpcTransportChannel;
29 import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider;
30 import com.google.api.gax.httpjson.GaxHttpJsonProperties;
31 import com.google.api.gax.httpjson.HttpJsonTransportChannel;
32 import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider;
33 import com.google.api.gax.retrying.RetrySettings;
34 import com.google.api.gax.rpc.ApiCallContext;
35 import com.google.api.gax.rpc.ApiClientHeaderProvider;
36 import com.google.api.gax.rpc.ClientContext;
37 import com.google.api.gax.rpc.PageContext;
38 import com.google.api.gax.rpc.PagedCallSettings;
39 import com.google.api.gax.rpc.PagedListDescriptor;
40 import com.google.api.gax.rpc.PagedListResponseFactory;
41 import com.google.api.gax.rpc.StatusCode;
42 import com.google.api.gax.rpc.StreamingCallSettings;
43 import com.google.api.gax.rpc.StubSettings;
44 import com.google.api.gax.rpc.TransportChannelProvider;
45 import com.google.api.gax.rpc.UnaryCallSettings;
46 import com.google.api.gax.rpc.UnaryCallable;
47 import com.google.cloud.dialogflow.v2.DetectIntentRequest;
48 import com.google.cloud.dialogflow.v2.DetectIntentResponse;
49 import com.google.cloud.dialogflow.v2.StreamingDetectIntentRequest;
50 import com.google.cloud.dialogflow.v2.StreamingDetectIntentResponse;
51 import com.google.cloud.location.GetLocationRequest;
52 import com.google.cloud.location.ListLocationsRequest;
53 import com.google.cloud.location.ListLocationsResponse;
54 import com.google.cloud.location.Location;
55 import com.google.common.collect.ImmutableList;
56 import com.google.common.collect.ImmutableMap;
57 import com.google.common.collect.ImmutableSet;
58 import com.google.common.collect.Lists;
59 import java.io.IOException;
60 import java.util.List;
61 import javax.annotation.Generated;
62 import org.threeten.bp.Duration;
63 
64 // AUTO-GENERATED DOCUMENTATION AND CLASS.
65 /**
66  * Settings class to configure an instance of {@link SessionsStub}.
67  *
68  * <p>The default instance has everything set to sensible defaults:
69  *
70  * <ul>
71  *   <li>The default service address (dialogflow.googleapis.com) and default port (443) are used.
72  *   <li>Credentials are acquired automatically through Application Default Credentials.
73  *   <li>Retries are configured for idempotent methods but not for non-idempotent methods.
74  * </ul>
75  *
76  * <p>The builder of this class is recursive, so contained classes are themselves builders. When
77  * build() is called, the tree of builders is called to create the complete settings object.
78  *
79  * <p>For example, to set the total timeout of detectIntent to 30 seconds:
80  *
81  * <pre>{@code
82  * // This snippet has been automatically generated and should be regarded as a code template only.
83  * // It will require modifications to work:
84  * // - It may require correct/in-range values for request initialization.
85  * // - It may require specifying regional endpoints when creating the service client as shown in
86  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
87  * SessionsStubSettings.Builder sessionsSettingsBuilder = SessionsStubSettings.newBuilder();
88  * sessionsSettingsBuilder
89  *     .detectIntentSettings()
90  *     .setRetrySettings(
91  *         sessionsSettingsBuilder
92  *             .detectIntentSettings()
93  *             .getRetrySettings()
94  *             .toBuilder()
95  *             .setTotalTimeout(Duration.ofSeconds(30))
96  *             .build());
97  * SessionsStubSettings sessionsSettings = sessionsSettingsBuilder.build();
98  * }</pre>
99  */
100 @Generated("by gapic-generator-java")
101 public class SessionsStubSettings extends StubSettings<SessionsStubSettings> {
102   /** The default scopes of the service. */
103   private static final ImmutableList<String> DEFAULT_SERVICE_SCOPES =
104       ImmutableList.<String>builder()
105           .add("https://www.googleapis.com/auth/cloud-platform")
106           .add("https://www.googleapis.com/auth/dialogflow")
107           .build();
108 
109   private final UnaryCallSettings<DetectIntentRequest, DetectIntentResponse> detectIntentSettings;
110   private final StreamingCallSettings<StreamingDetectIntentRequest, StreamingDetectIntentResponse>
111       streamingDetectIntentSettings;
112   private final PagedCallSettings<
113           ListLocationsRequest, ListLocationsResponse, ListLocationsPagedResponse>
114       listLocationsSettings;
115   private final UnaryCallSettings<GetLocationRequest, Location> getLocationSettings;
116 
117   private static final PagedListDescriptor<ListLocationsRequest, ListLocationsResponse, Location>
118       LIST_LOCATIONS_PAGE_STR_DESC =
119           new PagedListDescriptor<ListLocationsRequest, ListLocationsResponse, Location>() {
120             @Override
121             public String emptyToken() {
122               return "";
123             }
124 
125             @Override
126             public ListLocationsRequest injectToken(ListLocationsRequest payload, String token) {
127               return ListLocationsRequest.newBuilder(payload).setPageToken(token).build();
128             }
129 
130             @Override
131             public ListLocationsRequest injectPageSize(ListLocationsRequest payload, int pageSize) {
132               return ListLocationsRequest.newBuilder(payload).setPageSize(pageSize).build();
133             }
134 
135             @Override
136             public Integer extractPageSize(ListLocationsRequest payload) {
137               return payload.getPageSize();
138             }
139 
140             @Override
141             public String extractNextToken(ListLocationsResponse payload) {
142               return payload.getNextPageToken();
143             }
144 
145             @Override
146             public Iterable<Location> extractResources(ListLocationsResponse payload) {
147               return payload.getLocationsList() == null
148                   ? ImmutableList.<Location>of()
149                   : payload.getLocationsList();
150             }
151           };
152 
153   private static final PagedListResponseFactory<
154           ListLocationsRequest, ListLocationsResponse, ListLocationsPagedResponse>
155       LIST_LOCATIONS_PAGE_STR_FACT =
156           new PagedListResponseFactory<
157               ListLocationsRequest, ListLocationsResponse, ListLocationsPagedResponse>() {
158             @Override
159             public ApiFuture<ListLocationsPagedResponse> getFuturePagedResponse(
160                 UnaryCallable<ListLocationsRequest, ListLocationsResponse> callable,
161                 ListLocationsRequest request,
162                 ApiCallContext context,
163                 ApiFuture<ListLocationsResponse> futureResponse) {
164               PageContext<ListLocationsRequest, ListLocationsResponse, Location> pageContext =
165                   PageContext.create(callable, LIST_LOCATIONS_PAGE_STR_DESC, request, context);
166               return ListLocationsPagedResponse.createAsync(pageContext, futureResponse);
167             }
168           };
169 
170   /** Returns the object with the settings used for calls to detectIntent. */
detectIntentSettings()171   public UnaryCallSettings<DetectIntentRequest, DetectIntentResponse> detectIntentSettings() {
172     return detectIntentSettings;
173   }
174 
175   /** Returns the object with the settings used for calls to streamingDetectIntent. */
176   public StreamingCallSettings<StreamingDetectIntentRequest, StreamingDetectIntentResponse>
streamingDetectIntentSettings()177       streamingDetectIntentSettings() {
178     return streamingDetectIntentSettings;
179   }
180 
181   /** Returns the object with the settings used for calls to listLocations. */
182   public PagedCallSettings<ListLocationsRequest, ListLocationsResponse, ListLocationsPagedResponse>
listLocationsSettings()183       listLocationsSettings() {
184     return listLocationsSettings;
185   }
186 
187   /** Returns the object with the settings used for calls to getLocation. */
getLocationSettings()188   public UnaryCallSettings<GetLocationRequest, Location> getLocationSettings() {
189     return getLocationSettings;
190   }
191 
createStub()192   public SessionsStub createStub() throws IOException {
193     if (getTransportChannelProvider()
194         .getTransportName()
195         .equals(GrpcTransportChannel.getGrpcTransportName())) {
196       return GrpcSessionsStub.create(this);
197     }
198     if (getTransportChannelProvider()
199         .getTransportName()
200         .equals(HttpJsonTransportChannel.getHttpJsonTransportName())) {
201       return HttpJsonSessionsStub.create(this);
202     }
203     throw new UnsupportedOperationException(
204         String.format(
205             "Transport not supported: %s", getTransportChannelProvider().getTransportName()));
206   }
207 
208   /** Returns a builder for the default ExecutorProvider for this service. */
defaultExecutorProviderBuilder()209   public static InstantiatingExecutorProvider.Builder defaultExecutorProviderBuilder() {
210     return InstantiatingExecutorProvider.newBuilder();
211   }
212 
213   /** Returns the default service endpoint. */
getDefaultEndpoint()214   public static String getDefaultEndpoint() {
215     return "dialogflow.googleapis.com:443";
216   }
217 
218   /** Returns the default mTLS service endpoint. */
getDefaultMtlsEndpoint()219   public static String getDefaultMtlsEndpoint() {
220     return "dialogflow.mtls.googleapis.com:443";
221   }
222 
223   /** Returns the default service scopes. */
getDefaultServiceScopes()224   public static List<String> getDefaultServiceScopes() {
225     return DEFAULT_SERVICE_SCOPES;
226   }
227 
228   /** Returns a builder for the default credentials for this service. */
defaultCredentialsProviderBuilder()229   public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilder() {
230     return GoogleCredentialsProvider.newBuilder()
231         .setScopesToApply(DEFAULT_SERVICE_SCOPES)
232         .setUseJwtAccessWithScope(true);
233   }
234 
235   /** Returns a builder for the default gRPC ChannelProvider for this service. */
defaultGrpcTransportProviderBuilder()236   public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() {
237     return InstantiatingGrpcChannelProvider.newBuilder()
238         .setMaxInboundMessageSize(Integer.MAX_VALUE);
239   }
240 
241   /** Returns a builder for the default REST ChannelProvider for this service. */
242   @BetaApi
243   public static InstantiatingHttpJsonChannelProvider.Builder
defaultHttpJsonTransportProviderBuilder()244       defaultHttpJsonTransportProviderBuilder() {
245     return InstantiatingHttpJsonChannelProvider.newBuilder();
246   }
247 
defaultTransportChannelProvider()248   public static TransportChannelProvider defaultTransportChannelProvider() {
249     return defaultGrpcTransportProviderBuilder().build();
250   }
251 
252   @BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
defaultGrpcApiClientHeaderProviderBuilder()253   public static ApiClientHeaderProvider.Builder defaultGrpcApiClientHeaderProviderBuilder() {
254     return ApiClientHeaderProvider.newBuilder()
255         .setGeneratedLibToken("gapic", GaxProperties.getLibraryVersion(SessionsStubSettings.class))
256         .setTransportToken(
257             GaxGrpcProperties.getGrpcTokenName(), GaxGrpcProperties.getGrpcVersion());
258   }
259 
260   @BetaApi("The surface for customizing headers is not stable yet and may change in the future.")
defaultHttpJsonApiClientHeaderProviderBuilder()261   public static ApiClientHeaderProvider.Builder defaultHttpJsonApiClientHeaderProviderBuilder() {
262     return ApiClientHeaderProvider.newBuilder()
263         .setGeneratedLibToken("gapic", GaxProperties.getLibraryVersion(SessionsStubSettings.class))
264         .setTransportToken(
265             GaxHttpJsonProperties.getHttpJsonTokenName(),
266             GaxHttpJsonProperties.getHttpJsonVersion());
267   }
268 
defaultApiClientHeaderProviderBuilder()269   public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() {
270     return SessionsStubSettings.defaultGrpcApiClientHeaderProviderBuilder();
271   }
272 
273   /** Returns a new gRPC builder for this class. */
newBuilder()274   public static Builder newBuilder() {
275     return Builder.createDefault();
276   }
277 
278   /** Returns a new REST builder for this class. */
newHttpJsonBuilder()279   public static Builder newHttpJsonBuilder() {
280     return Builder.createHttpJsonDefault();
281   }
282 
283   /** Returns a new builder for this class. */
newBuilder(ClientContext clientContext)284   public static Builder newBuilder(ClientContext clientContext) {
285     return new Builder(clientContext);
286   }
287 
288   /** Returns a builder containing all the values of this settings class. */
toBuilder()289   public Builder toBuilder() {
290     return new Builder(this);
291   }
292 
SessionsStubSettings(Builder settingsBuilder)293   protected SessionsStubSettings(Builder settingsBuilder) throws IOException {
294     super(settingsBuilder);
295 
296     detectIntentSettings = settingsBuilder.detectIntentSettings().build();
297     streamingDetectIntentSettings = settingsBuilder.streamingDetectIntentSettings().build();
298     listLocationsSettings = settingsBuilder.listLocationsSettings().build();
299     getLocationSettings = settingsBuilder.getLocationSettings().build();
300   }
301 
302   /** Builder for SessionsStubSettings. */
303   public static class Builder extends StubSettings.Builder<SessionsStubSettings, Builder> {
304     private final ImmutableList<UnaryCallSettings.Builder<?, ?>> unaryMethodSettingsBuilders;
305     private final UnaryCallSettings.Builder<DetectIntentRequest, DetectIntentResponse>
306         detectIntentSettings;
307     private final StreamingCallSettings.Builder<
308             StreamingDetectIntentRequest, StreamingDetectIntentResponse>
309         streamingDetectIntentSettings;
310     private final PagedCallSettings.Builder<
311             ListLocationsRequest, ListLocationsResponse, ListLocationsPagedResponse>
312         listLocationsSettings;
313     private final UnaryCallSettings.Builder<GetLocationRequest, Location> getLocationSettings;
314     private static final ImmutableMap<String, ImmutableSet<StatusCode.Code>>
315         RETRYABLE_CODE_DEFINITIONS;
316 
317     static {
318       ImmutableMap.Builder<String, ImmutableSet<StatusCode.Code>> definitions =
319           ImmutableMap.builder();
320       definitions.put(
321           "retry_policy_1_codes",
322           ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.UNAVAILABLE)));
323       definitions.put(
324           "no_retry_2_codes", ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList()));
325       definitions.put(
326           "retry_policy_0_codes",
327           ImmutableSet.copyOf(Lists.<StatusCode.Code>newArrayList(StatusCode.Code.UNAVAILABLE)));
328       RETRYABLE_CODE_DEFINITIONS = definitions.build();
329     }
330 
331     private static final ImmutableMap<String, RetrySettings> RETRY_PARAM_DEFINITIONS;
332 
333     static {
334       ImmutableMap.Builder<String, RetrySettings> definitions = ImmutableMap.builder();
335       RetrySettings settings = null;
336       settings =
337           RetrySettings.newBuilder()
338               .setInitialRetryDelay(Duration.ofMillis(100L))
339               .setRetryDelayMultiplier(1.3)
340               .setMaxRetryDelay(Duration.ofMillis(60000L))
341               .setInitialRpcTimeout(Duration.ofMillis(220000L))
342               .setRpcTimeoutMultiplier(1.0)
343               .setMaxRpcTimeout(Duration.ofMillis(220000L))
344               .setTotalTimeout(Duration.ofMillis(220000L))
345               .build();
346       definitions.put("retry_policy_1_params", settings);
347       settings =
348           RetrySettings.newBuilder()
349               .setInitialRpcTimeout(Duration.ofMillis(220000L))
350               .setRpcTimeoutMultiplier(1.0)
351               .setMaxRpcTimeout(Duration.ofMillis(220000L))
352               .setTotalTimeout(Duration.ofMillis(220000L))
353               .build();
354       definitions.put("no_retry_2_params", settings);
355       settings =
356           RetrySettings.newBuilder()
357               .setInitialRetryDelay(Duration.ofMillis(100L))
358               .setRetryDelayMultiplier(1.3)
359               .setMaxRetryDelay(Duration.ofMillis(60000L))
360               .setInitialRpcTimeout(Duration.ofMillis(60000L))
361               .setRpcTimeoutMultiplier(1.0)
362               .setMaxRpcTimeout(Duration.ofMillis(60000L))
363               .setTotalTimeout(Duration.ofMillis(60000L))
364               .build();
365       definitions.put("retry_policy_0_params", settings);
366       RETRY_PARAM_DEFINITIONS = definitions.build();
367     }
368 
Builder()369     protected Builder() {
370       this(((ClientContext) null));
371     }
372 
Builder(ClientContext clientContext)373     protected Builder(ClientContext clientContext) {
374       super(clientContext);
375 
376       detectIntentSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();
377       streamingDetectIntentSettings = StreamingCallSettings.newBuilder();
378       listLocationsSettings = PagedCallSettings.newBuilder(LIST_LOCATIONS_PAGE_STR_FACT);
379       getLocationSettings = UnaryCallSettings.newUnaryCallSettingsBuilder();
380 
381       unaryMethodSettingsBuilders =
382           ImmutableList.<UnaryCallSettings.Builder<?, ?>>of(
383               detectIntentSettings, listLocationsSettings, getLocationSettings);
384       initDefaults(this);
385     }
386 
Builder(SessionsStubSettings settings)387     protected Builder(SessionsStubSettings settings) {
388       super(settings);
389 
390       detectIntentSettings = settings.detectIntentSettings.toBuilder();
391       streamingDetectIntentSettings = settings.streamingDetectIntentSettings.toBuilder();
392       listLocationsSettings = settings.listLocationsSettings.toBuilder();
393       getLocationSettings = settings.getLocationSettings.toBuilder();
394 
395       unaryMethodSettingsBuilders =
396           ImmutableList.<UnaryCallSettings.Builder<?, ?>>of(
397               detectIntentSettings, listLocationsSettings, getLocationSettings);
398     }
399 
createDefault()400     private static Builder createDefault() {
401       Builder builder = new Builder(((ClientContext) null));
402 
403       builder.setTransportChannelProvider(defaultTransportChannelProvider());
404       builder.setCredentialsProvider(defaultCredentialsProviderBuilder().build());
405       builder.setInternalHeaderProvider(defaultApiClientHeaderProviderBuilder().build());
406       builder.setEndpoint(getDefaultEndpoint());
407       builder.setMtlsEndpoint(getDefaultMtlsEndpoint());
408       builder.setSwitchToMtlsEndpointAllowed(true);
409 
410       return initDefaults(builder);
411     }
412 
createHttpJsonDefault()413     private static Builder createHttpJsonDefault() {
414       Builder builder = new Builder(((ClientContext) null));
415 
416       builder.setTransportChannelProvider(defaultHttpJsonTransportProviderBuilder().build());
417       builder.setCredentialsProvider(defaultCredentialsProviderBuilder().build());
418       builder.setInternalHeaderProvider(defaultHttpJsonApiClientHeaderProviderBuilder().build());
419       builder.setEndpoint(getDefaultEndpoint());
420       builder.setMtlsEndpoint(getDefaultMtlsEndpoint());
421       builder.setSwitchToMtlsEndpointAllowed(true);
422 
423       return initDefaults(builder);
424     }
425 
initDefaults(Builder builder)426     private static Builder initDefaults(Builder builder) {
427       builder
428           .detectIntentSettings()
429           .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("retry_policy_1_codes"))
430           .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("retry_policy_1_params"));
431 
432       builder
433           .listLocationsSettings()
434           .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("retry_policy_0_codes"))
435           .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("retry_policy_0_params"));
436 
437       builder
438           .getLocationSettings()
439           .setRetryableCodes(RETRYABLE_CODE_DEFINITIONS.get("retry_policy_0_codes"))
440           .setRetrySettings(RETRY_PARAM_DEFINITIONS.get("retry_policy_0_params"));
441 
442       return builder;
443     }
444 
445     /**
446      * Applies the given settings updater function to all of the unary API methods in this service.
447      *
448      * <p>Note: This method does not support applying settings to streaming methods.
449      */
applyToAllUnaryMethods( ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> settingsUpdater)450     public Builder applyToAllUnaryMethods(
451         ApiFunction<UnaryCallSettings.Builder<?, ?>, Void> settingsUpdater) {
452       super.applyToAllUnaryMethods(unaryMethodSettingsBuilders, settingsUpdater);
453       return this;
454     }
455 
unaryMethodSettingsBuilders()456     public ImmutableList<UnaryCallSettings.Builder<?, ?>> unaryMethodSettingsBuilders() {
457       return unaryMethodSettingsBuilders;
458     }
459 
460     /** Returns the builder for the settings used for calls to detectIntent. */
461     public UnaryCallSettings.Builder<DetectIntentRequest, DetectIntentResponse>
detectIntentSettings()462         detectIntentSettings() {
463       return detectIntentSettings;
464     }
465 
466     /** Returns the builder for the settings used for calls to streamingDetectIntent. */
467     public StreamingCallSettings.Builder<
468             StreamingDetectIntentRequest, StreamingDetectIntentResponse>
streamingDetectIntentSettings()469         streamingDetectIntentSettings() {
470       return streamingDetectIntentSettings;
471     }
472 
473     /** Returns the builder for the settings used for calls to listLocations. */
474     public PagedCallSettings.Builder<
475             ListLocationsRequest, ListLocationsResponse, ListLocationsPagedResponse>
listLocationsSettings()476         listLocationsSettings() {
477       return listLocationsSettings;
478     }
479 
480     /** Returns the builder for the settings used for calls to getLocation. */
getLocationSettings()481     public UnaryCallSettings.Builder<GetLocationRequest, Location> getLocationSettings() {
482       return getLocationSettings;
483     }
484 
485     @Override
build()486     public SessionsStubSettings build() throws IOException {
487       return new SessionsStubSettings(this);
488     }
489   }
490 }
491