• 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.gax.core.BackgroundResource;
22 import com.google.api.gax.core.BackgroundResourceAggregation;
23 import com.google.api.gax.grpc.GrpcCallSettings;
24 import com.google.api.gax.grpc.GrpcStubCallableFactory;
25 import com.google.api.gax.rpc.BidiStreamingCallable;
26 import com.google.api.gax.rpc.ClientContext;
27 import com.google.api.gax.rpc.UnaryCallable;
28 import com.google.cloud.dialogflow.v2.DetectIntentRequest;
29 import com.google.cloud.dialogflow.v2.DetectIntentResponse;
30 import com.google.cloud.dialogflow.v2.StreamingDetectIntentRequest;
31 import com.google.cloud.dialogflow.v2.StreamingDetectIntentResponse;
32 import com.google.cloud.location.GetLocationRequest;
33 import com.google.cloud.location.ListLocationsRequest;
34 import com.google.cloud.location.ListLocationsResponse;
35 import com.google.cloud.location.Location;
36 import com.google.common.collect.ImmutableMap;
37 import com.google.longrunning.stub.GrpcOperationsStub;
38 import io.grpc.MethodDescriptor;
39 import io.grpc.protobuf.ProtoUtils;
40 import java.io.IOException;
41 import java.util.concurrent.TimeUnit;
42 import javax.annotation.Generated;
43 
44 // AUTO-GENERATED DOCUMENTATION AND CLASS.
45 /**
46  * gRPC stub implementation for the Sessions service API.
47  *
48  * <p>This class is for advanced usage and reflects the underlying API directly.
49  */
50 @Generated("by gapic-generator-java")
51 public class GrpcSessionsStub extends SessionsStub {
52   private static final MethodDescriptor<DetectIntentRequest, DetectIntentResponse>
53       detectIntentMethodDescriptor =
54           MethodDescriptor.<DetectIntentRequest, DetectIntentResponse>newBuilder()
55               .setType(MethodDescriptor.MethodType.UNARY)
56               .setFullMethodName("google.cloud.dialogflow.v2.Sessions/DetectIntent")
57               .setRequestMarshaller(ProtoUtils.marshaller(DetectIntentRequest.getDefaultInstance()))
58               .setResponseMarshaller(
59                   ProtoUtils.marshaller(DetectIntentResponse.getDefaultInstance()))
60               .build();
61 
62   private static final MethodDescriptor<StreamingDetectIntentRequest, StreamingDetectIntentResponse>
63       streamingDetectIntentMethodDescriptor =
64           MethodDescriptor.<StreamingDetectIntentRequest, StreamingDetectIntentResponse>newBuilder()
65               .setType(MethodDescriptor.MethodType.BIDI_STREAMING)
66               .setFullMethodName("google.cloud.dialogflow.v2.Sessions/StreamingDetectIntent")
67               .setRequestMarshaller(
68                   ProtoUtils.marshaller(StreamingDetectIntentRequest.getDefaultInstance()))
69               .setResponseMarshaller(
70                   ProtoUtils.marshaller(StreamingDetectIntentResponse.getDefaultInstance()))
71               .build();
72 
73   private static final MethodDescriptor<ListLocationsRequest, ListLocationsResponse>
74       listLocationsMethodDescriptor =
75           MethodDescriptor.<ListLocationsRequest, ListLocationsResponse>newBuilder()
76               .setType(MethodDescriptor.MethodType.UNARY)
77               .setFullMethodName("google.cloud.location.Locations/ListLocations")
78               .setRequestMarshaller(
79                   ProtoUtils.marshaller(ListLocationsRequest.getDefaultInstance()))
80               .setResponseMarshaller(
81                   ProtoUtils.marshaller(ListLocationsResponse.getDefaultInstance()))
82               .build();
83 
84   private static final MethodDescriptor<GetLocationRequest, Location> getLocationMethodDescriptor =
85       MethodDescriptor.<GetLocationRequest, Location>newBuilder()
86           .setType(MethodDescriptor.MethodType.UNARY)
87           .setFullMethodName("google.cloud.location.Locations/GetLocation")
88           .setRequestMarshaller(ProtoUtils.marshaller(GetLocationRequest.getDefaultInstance()))
89           .setResponseMarshaller(ProtoUtils.marshaller(Location.getDefaultInstance()))
90           .build();
91 
92   private final UnaryCallable<DetectIntentRequest, DetectIntentResponse> detectIntentCallable;
93   private final BidiStreamingCallable<StreamingDetectIntentRequest, StreamingDetectIntentResponse>
94       streamingDetectIntentCallable;
95   private final UnaryCallable<ListLocationsRequest, ListLocationsResponse> listLocationsCallable;
96   private final UnaryCallable<ListLocationsRequest, ListLocationsPagedResponse>
97       listLocationsPagedCallable;
98   private final UnaryCallable<GetLocationRequest, Location> getLocationCallable;
99 
100   private final BackgroundResource backgroundResources;
101   private final GrpcOperationsStub operationsStub;
102   private final GrpcStubCallableFactory callableFactory;
103 
create(SessionsStubSettings settings)104   public static final GrpcSessionsStub create(SessionsStubSettings settings) throws IOException {
105     return new GrpcSessionsStub(settings, ClientContext.create(settings));
106   }
107 
create(ClientContext clientContext)108   public static final GrpcSessionsStub create(ClientContext clientContext) throws IOException {
109     return new GrpcSessionsStub(SessionsStubSettings.newBuilder().build(), clientContext);
110   }
111 
create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)112   public static final GrpcSessionsStub create(
113       ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
114     return new GrpcSessionsStub(
115         SessionsStubSettings.newBuilder().build(), clientContext, callableFactory);
116   }
117 
118   /**
119    * Constructs an instance of GrpcSessionsStub, using the given settings. This is protected so that
120    * it is easy to make a subclass, but otherwise, the static factory methods should be preferred.
121    */
GrpcSessionsStub(SessionsStubSettings settings, ClientContext clientContext)122   protected GrpcSessionsStub(SessionsStubSettings settings, ClientContext clientContext)
123       throws IOException {
124     this(settings, clientContext, new GrpcSessionsCallableFactory());
125   }
126 
127   /**
128    * Constructs an instance of GrpcSessionsStub, using the given settings. This is protected so that
129    * it is easy to make a subclass, but otherwise, the static factory methods should be preferred.
130    */
GrpcSessionsStub( SessionsStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)131   protected GrpcSessionsStub(
132       SessionsStubSettings settings,
133       ClientContext clientContext,
134       GrpcStubCallableFactory callableFactory)
135       throws IOException {
136     this.callableFactory = callableFactory;
137     this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);
138 
139     GrpcCallSettings<DetectIntentRequest, DetectIntentResponse> detectIntentTransportSettings =
140         GrpcCallSettings.<DetectIntentRequest, DetectIntentResponse>newBuilder()
141             .setMethodDescriptor(detectIntentMethodDescriptor)
142             .setParamsExtractor(
143                 request -> {
144                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
145                   params.put("session", String.valueOf(request.getSession()));
146                   return params.build();
147                 })
148             .build();
149     GrpcCallSettings<StreamingDetectIntentRequest, StreamingDetectIntentResponse>
150         streamingDetectIntentTransportSettings =
151             GrpcCallSettings
152                 .<StreamingDetectIntentRequest, StreamingDetectIntentResponse>newBuilder()
153                 .setMethodDescriptor(streamingDetectIntentMethodDescriptor)
154                 .build();
155     GrpcCallSettings<ListLocationsRequest, ListLocationsResponse> listLocationsTransportSettings =
156         GrpcCallSettings.<ListLocationsRequest, ListLocationsResponse>newBuilder()
157             .setMethodDescriptor(listLocationsMethodDescriptor)
158             .setParamsExtractor(
159                 request -> {
160                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
161                   params.put("name", String.valueOf(request.getName()));
162                   return params.build();
163                 })
164             .build();
165     GrpcCallSettings<GetLocationRequest, Location> getLocationTransportSettings =
166         GrpcCallSettings.<GetLocationRequest, Location>newBuilder()
167             .setMethodDescriptor(getLocationMethodDescriptor)
168             .setParamsExtractor(
169                 request -> {
170                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
171                   params.put("name", String.valueOf(request.getName()));
172                   return params.build();
173                 })
174             .build();
175 
176     this.detectIntentCallable =
177         callableFactory.createUnaryCallable(
178             detectIntentTransportSettings, settings.detectIntentSettings(), clientContext);
179     this.streamingDetectIntentCallable =
180         callableFactory.createBidiStreamingCallable(
181             streamingDetectIntentTransportSettings,
182             settings.streamingDetectIntentSettings(),
183             clientContext);
184     this.listLocationsCallable =
185         callableFactory.createUnaryCallable(
186             listLocationsTransportSettings, settings.listLocationsSettings(), clientContext);
187     this.listLocationsPagedCallable =
188         callableFactory.createPagedCallable(
189             listLocationsTransportSettings, settings.listLocationsSettings(), clientContext);
190     this.getLocationCallable =
191         callableFactory.createUnaryCallable(
192             getLocationTransportSettings, settings.getLocationSettings(), clientContext);
193 
194     this.backgroundResources =
195         new BackgroundResourceAggregation(clientContext.getBackgroundResources());
196   }
197 
getOperationsStub()198   public GrpcOperationsStub getOperationsStub() {
199     return operationsStub;
200   }
201 
202   @Override
detectIntentCallable()203   public UnaryCallable<DetectIntentRequest, DetectIntentResponse> detectIntentCallable() {
204     return detectIntentCallable;
205   }
206 
207   @Override
208   public BidiStreamingCallable<StreamingDetectIntentRequest, StreamingDetectIntentResponse>
streamingDetectIntentCallable()209       streamingDetectIntentCallable() {
210     return streamingDetectIntentCallable;
211   }
212 
213   @Override
listLocationsCallable()214   public UnaryCallable<ListLocationsRequest, ListLocationsResponse> listLocationsCallable() {
215     return listLocationsCallable;
216   }
217 
218   @Override
219   public UnaryCallable<ListLocationsRequest, ListLocationsPagedResponse>
listLocationsPagedCallable()220       listLocationsPagedCallable() {
221     return listLocationsPagedCallable;
222   }
223 
224   @Override
getLocationCallable()225   public UnaryCallable<GetLocationRequest, Location> getLocationCallable() {
226     return getLocationCallable;
227   }
228 
229   @Override
close()230   public final void close() {
231     try {
232       backgroundResources.close();
233     } catch (RuntimeException e) {
234       throw e;
235     } catch (Exception e) {
236       throw new IllegalStateException("Failed to close resource", e);
237     }
238   }
239 
240   @Override
shutdown()241   public void shutdown() {
242     backgroundResources.shutdown();
243   }
244 
245   @Override
isShutdown()246   public boolean isShutdown() {
247     return backgroundResources.isShutdown();
248   }
249 
250   @Override
isTerminated()251   public boolean isTerminated() {
252     return backgroundResources.isTerminated();
253   }
254 
255   @Override
shutdownNow()256   public void shutdownNow() {
257     backgroundResources.shutdownNow();
258   }
259 
260   @Override
awaitTermination(long duration, TimeUnit unit)261   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
262     return backgroundResources.awaitTermination(duration, unit);
263   }
264 }
265