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.ContextsClient.ListContextsPagedResponse; 20 import static com.google.cloud.dialogflow.v2.ContextsClient.ListLocationsPagedResponse; 21 22 import com.google.api.gax.core.BackgroundResource; 23 import com.google.api.gax.core.BackgroundResourceAggregation; 24 import com.google.api.gax.grpc.GrpcCallSettings; 25 import com.google.api.gax.grpc.GrpcStubCallableFactory; 26 import com.google.api.gax.rpc.ClientContext; 27 import com.google.api.gax.rpc.UnaryCallable; 28 import com.google.cloud.dialogflow.v2.Context; 29 import com.google.cloud.dialogflow.v2.CreateContextRequest; 30 import com.google.cloud.dialogflow.v2.DeleteAllContextsRequest; 31 import com.google.cloud.dialogflow.v2.DeleteContextRequest; 32 import com.google.cloud.dialogflow.v2.GetContextRequest; 33 import com.google.cloud.dialogflow.v2.ListContextsRequest; 34 import com.google.cloud.dialogflow.v2.ListContextsResponse; 35 import com.google.cloud.dialogflow.v2.UpdateContextRequest; 36 import com.google.cloud.location.GetLocationRequest; 37 import com.google.cloud.location.ListLocationsRequest; 38 import com.google.cloud.location.ListLocationsResponse; 39 import com.google.cloud.location.Location; 40 import com.google.common.collect.ImmutableMap; 41 import com.google.longrunning.stub.GrpcOperationsStub; 42 import com.google.protobuf.Empty; 43 import io.grpc.MethodDescriptor; 44 import io.grpc.protobuf.ProtoUtils; 45 import java.io.IOException; 46 import java.util.concurrent.TimeUnit; 47 import javax.annotation.Generated; 48 49 // AUTO-GENERATED DOCUMENTATION AND CLASS. 50 /** 51 * gRPC stub implementation for the Contexts service API. 52 * 53 * <p>This class is for advanced usage and reflects the underlying API directly. 54 */ 55 @Generated("by gapic-generator-java") 56 public class GrpcContextsStub extends ContextsStub { 57 private static final MethodDescriptor<ListContextsRequest, ListContextsResponse> 58 listContextsMethodDescriptor = 59 MethodDescriptor.<ListContextsRequest, ListContextsResponse>newBuilder() 60 .setType(MethodDescriptor.MethodType.UNARY) 61 .setFullMethodName("google.cloud.dialogflow.v2.Contexts/ListContexts") 62 .setRequestMarshaller(ProtoUtils.marshaller(ListContextsRequest.getDefaultInstance())) 63 .setResponseMarshaller( 64 ProtoUtils.marshaller(ListContextsResponse.getDefaultInstance())) 65 .build(); 66 67 private static final MethodDescriptor<GetContextRequest, Context> getContextMethodDescriptor = 68 MethodDescriptor.<GetContextRequest, Context>newBuilder() 69 .setType(MethodDescriptor.MethodType.UNARY) 70 .setFullMethodName("google.cloud.dialogflow.v2.Contexts/GetContext") 71 .setRequestMarshaller(ProtoUtils.marshaller(GetContextRequest.getDefaultInstance())) 72 .setResponseMarshaller(ProtoUtils.marshaller(Context.getDefaultInstance())) 73 .build(); 74 75 private static final MethodDescriptor<CreateContextRequest, Context> 76 createContextMethodDescriptor = 77 MethodDescriptor.<CreateContextRequest, Context>newBuilder() 78 .setType(MethodDescriptor.MethodType.UNARY) 79 .setFullMethodName("google.cloud.dialogflow.v2.Contexts/CreateContext") 80 .setRequestMarshaller( 81 ProtoUtils.marshaller(CreateContextRequest.getDefaultInstance())) 82 .setResponseMarshaller(ProtoUtils.marshaller(Context.getDefaultInstance())) 83 .build(); 84 85 private static final MethodDescriptor<UpdateContextRequest, Context> 86 updateContextMethodDescriptor = 87 MethodDescriptor.<UpdateContextRequest, Context>newBuilder() 88 .setType(MethodDescriptor.MethodType.UNARY) 89 .setFullMethodName("google.cloud.dialogflow.v2.Contexts/UpdateContext") 90 .setRequestMarshaller( 91 ProtoUtils.marshaller(UpdateContextRequest.getDefaultInstance())) 92 .setResponseMarshaller(ProtoUtils.marshaller(Context.getDefaultInstance())) 93 .build(); 94 95 private static final MethodDescriptor<DeleteContextRequest, Empty> deleteContextMethodDescriptor = 96 MethodDescriptor.<DeleteContextRequest, Empty>newBuilder() 97 .setType(MethodDescriptor.MethodType.UNARY) 98 .setFullMethodName("google.cloud.dialogflow.v2.Contexts/DeleteContext") 99 .setRequestMarshaller(ProtoUtils.marshaller(DeleteContextRequest.getDefaultInstance())) 100 .setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance())) 101 .build(); 102 103 private static final MethodDescriptor<DeleteAllContextsRequest, Empty> 104 deleteAllContextsMethodDescriptor = 105 MethodDescriptor.<DeleteAllContextsRequest, Empty>newBuilder() 106 .setType(MethodDescriptor.MethodType.UNARY) 107 .setFullMethodName("google.cloud.dialogflow.v2.Contexts/DeleteAllContexts") 108 .setRequestMarshaller( 109 ProtoUtils.marshaller(DeleteAllContextsRequest.getDefaultInstance())) 110 .setResponseMarshaller(ProtoUtils.marshaller(Empty.getDefaultInstance())) 111 .build(); 112 113 private static final MethodDescriptor<ListLocationsRequest, ListLocationsResponse> 114 listLocationsMethodDescriptor = 115 MethodDescriptor.<ListLocationsRequest, ListLocationsResponse>newBuilder() 116 .setType(MethodDescriptor.MethodType.UNARY) 117 .setFullMethodName("google.cloud.location.Locations/ListLocations") 118 .setRequestMarshaller( 119 ProtoUtils.marshaller(ListLocationsRequest.getDefaultInstance())) 120 .setResponseMarshaller( 121 ProtoUtils.marshaller(ListLocationsResponse.getDefaultInstance())) 122 .build(); 123 124 private static final MethodDescriptor<GetLocationRequest, Location> getLocationMethodDescriptor = 125 MethodDescriptor.<GetLocationRequest, Location>newBuilder() 126 .setType(MethodDescriptor.MethodType.UNARY) 127 .setFullMethodName("google.cloud.location.Locations/GetLocation") 128 .setRequestMarshaller(ProtoUtils.marshaller(GetLocationRequest.getDefaultInstance())) 129 .setResponseMarshaller(ProtoUtils.marshaller(Location.getDefaultInstance())) 130 .build(); 131 132 private final UnaryCallable<ListContextsRequest, ListContextsResponse> listContextsCallable; 133 private final UnaryCallable<ListContextsRequest, ListContextsPagedResponse> 134 listContextsPagedCallable; 135 private final UnaryCallable<GetContextRequest, Context> getContextCallable; 136 private final UnaryCallable<CreateContextRequest, Context> createContextCallable; 137 private final UnaryCallable<UpdateContextRequest, Context> updateContextCallable; 138 private final UnaryCallable<DeleteContextRequest, Empty> deleteContextCallable; 139 private final UnaryCallable<DeleteAllContextsRequest, Empty> deleteAllContextsCallable; 140 private final UnaryCallable<ListLocationsRequest, ListLocationsResponse> listLocationsCallable; 141 private final UnaryCallable<ListLocationsRequest, ListLocationsPagedResponse> 142 listLocationsPagedCallable; 143 private final UnaryCallable<GetLocationRequest, Location> getLocationCallable; 144 145 private final BackgroundResource backgroundResources; 146 private final GrpcOperationsStub operationsStub; 147 private final GrpcStubCallableFactory callableFactory; 148 create(ContextsStubSettings settings)149 public static final GrpcContextsStub create(ContextsStubSettings settings) throws IOException { 150 return new GrpcContextsStub(settings, ClientContext.create(settings)); 151 } 152 create(ClientContext clientContext)153 public static final GrpcContextsStub create(ClientContext clientContext) throws IOException { 154 return new GrpcContextsStub(ContextsStubSettings.newBuilder().build(), clientContext); 155 } 156 create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)157 public static final GrpcContextsStub create( 158 ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException { 159 return new GrpcContextsStub( 160 ContextsStubSettings.newBuilder().build(), clientContext, callableFactory); 161 } 162 163 /** 164 * Constructs an instance of GrpcContextsStub, using the given settings. This is protected so that 165 * it is easy to make a subclass, but otherwise, the static factory methods should be preferred. 166 */ GrpcContextsStub(ContextsStubSettings settings, ClientContext clientContext)167 protected GrpcContextsStub(ContextsStubSettings settings, ClientContext clientContext) 168 throws IOException { 169 this(settings, clientContext, new GrpcContextsCallableFactory()); 170 } 171 172 /** 173 * Constructs an instance of GrpcContextsStub, using the given settings. This is protected so that 174 * it is easy to make a subclass, but otherwise, the static factory methods should be preferred. 175 */ GrpcContextsStub( ContextsStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)176 protected GrpcContextsStub( 177 ContextsStubSettings settings, 178 ClientContext clientContext, 179 GrpcStubCallableFactory callableFactory) 180 throws IOException { 181 this.callableFactory = callableFactory; 182 this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory); 183 184 GrpcCallSettings<ListContextsRequest, ListContextsResponse> listContextsTransportSettings = 185 GrpcCallSettings.<ListContextsRequest, ListContextsResponse>newBuilder() 186 .setMethodDescriptor(listContextsMethodDescriptor) 187 .setParamsExtractor( 188 request -> { 189 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 190 params.put("parent", String.valueOf(request.getParent())); 191 return params.build(); 192 }) 193 .build(); 194 GrpcCallSettings<GetContextRequest, Context> getContextTransportSettings = 195 GrpcCallSettings.<GetContextRequest, Context>newBuilder() 196 .setMethodDescriptor(getContextMethodDescriptor) 197 .setParamsExtractor( 198 request -> { 199 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 200 params.put("name", String.valueOf(request.getName())); 201 return params.build(); 202 }) 203 .build(); 204 GrpcCallSettings<CreateContextRequest, Context> createContextTransportSettings = 205 GrpcCallSettings.<CreateContextRequest, Context>newBuilder() 206 .setMethodDescriptor(createContextMethodDescriptor) 207 .setParamsExtractor( 208 request -> { 209 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 210 params.put("parent", String.valueOf(request.getParent())); 211 return params.build(); 212 }) 213 .build(); 214 GrpcCallSettings<UpdateContextRequest, Context> updateContextTransportSettings = 215 GrpcCallSettings.<UpdateContextRequest, Context>newBuilder() 216 .setMethodDescriptor(updateContextMethodDescriptor) 217 .setParamsExtractor( 218 request -> { 219 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 220 params.put("context.name", String.valueOf(request.getContext().getName())); 221 return params.build(); 222 }) 223 .build(); 224 GrpcCallSettings<DeleteContextRequest, Empty> deleteContextTransportSettings = 225 GrpcCallSettings.<DeleteContextRequest, Empty>newBuilder() 226 .setMethodDescriptor(deleteContextMethodDescriptor) 227 .setParamsExtractor( 228 request -> { 229 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 230 params.put("name", String.valueOf(request.getName())); 231 return params.build(); 232 }) 233 .build(); 234 GrpcCallSettings<DeleteAllContextsRequest, Empty> deleteAllContextsTransportSettings = 235 GrpcCallSettings.<DeleteAllContextsRequest, Empty>newBuilder() 236 .setMethodDescriptor(deleteAllContextsMethodDescriptor) 237 .setParamsExtractor( 238 request -> { 239 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 240 params.put("parent", String.valueOf(request.getParent())); 241 return params.build(); 242 }) 243 .build(); 244 GrpcCallSettings<ListLocationsRequest, ListLocationsResponse> listLocationsTransportSettings = 245 GrpcCallSettings.<ListLocationsRequest, ListLocationsResponse>newBuilder() 246 .setMethodDescriptor(listLocationsMethodDescriptor) 247 .setParamsExtractor( 248 request -> { 249 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 250 params.put("name", String.valueOf(request.getName())); 251 return params.build(); 252 }) 253 .build(); 254 GrpcCallSettings<GetLocationRequest, Location> getLocationTransportSettings = 255 GrpcCallSettings.<GetLocationRequest, Location>newBuilder() 256 .setMethodDescriptor(getLocationMethodDescriptor) 257 .setParamsExtractor( 258 request -> { 259 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 260 params.put("name", String.valueOf(request.getName())); 261 return params.build(); 262 }) 263 .build(); 264 265 this.listContextsCallable = 266 callableFactory.createUnaryCallable( 267 listContextsTransportSettings, settings.listContextsSettings(), clientContext); 268 this.listContextsPagedCallable = 269 callableFactory.createPagedCallable( 270 listContextsTransportSettings, settings.listContextsSettings(), clientContext); 271 this.getContextCallable = 272 callableFactory.createUnaryCallable( 273 getContextTransportSettings, settings.getContextSettings(), clientContext); 274 this.createContextCallable = 275 callableFactory.createUnaryCallable( 276 createContextTransportSettings, settings.createContextSettings(), clientContext); 277 this.updateContextCallable = 278 callableFactory.createUnaryCallable( 279 updateContextTransportSettings, settings.updateContextSettings(), clientContext); 280 this.deleteContextCallable = 281 callableFactory.createUnaryCallable( 282 deleteContextTransportSettings, settings.deleteContextSettings(), clientContext); 283 this.deleteAllContextsCallable = 284 callableFactory.createUnaryCallable( 285 deleteAllContextsTransportSettings, 286 settings.deleteAllContextsSettings(), 287 clientContext); 288 this.listLocationsCallable = 289 callableFactory.createUnaryCallable( 290 listLocationsTransportSettings, settings.listLocationsSettings(), clientContext); 291 this.listLocationsPagedCallable = 292 callableFactory.createPagedCallable( 293 listLocationsTransportSettings, settings.listLocationsSettings(), clientContext); 294 this.getLocationCallable = 295 callableFactory.createUnaryCallable( 296 getLocationTransportSettings, settings.getLocationSettings(), clientContext); 297 298 this.backgroundResources = 299 new BackgroundResourceAggregation(clientContext.getBackgroundResources()); 300 } 301 getOperationsStub()302 public GrpcOperationsStub getOperationsStub() { 303 return operationsStub; 304 } 305 306 @Override listContextsCallable()307 public UnaryCallable<ListContextsRequest, ListContextsResponse> listContextsCallable() { 308 return listContextsCallable; 309 } 310 311 @Override listContextsPagedCallable()312 public UnaryCallable<ListContextsRequest, ListContextsPagedResponse> listContextsPagedCallable() { 313 return listContextsPagedCallable; 314 } 315 316 @Override getContextCallable()317 public UnaryCallable<GetContextRequest, Context> getContextCallable() { 318 return getContextCallable; 319 } 320 321 @Override createContextCallable()322 public UnaryCallable<CreateContextRequest, Context> createContextCallable() { 323 return createContextCallable; 324 } 325 326 @Override updateContextCallable()327 public UnaryCallable<UpdateContextRequest, Context> updateContextCallable() { 328 return updateContextCallable; 329 } 330 331 @Override deleteContextCallable()332 public UnaryCallable<DeleteContextRequest, Empty> deleteContextCallable() { 333 return deleteContextCallable; 334 } 335 336 @Override deleteAllContextsCallable()337 public UnaryCallable<DeleteAllContextsRequest, Empty> deleteAllContextsCallable() { 338 return deleteAllContextsCallable; 339 } 340 341 @Override listLocationsCallable()342 public UnaryCallable<ListLocationsRequest, ListLocationsResponse> listLocationsCallable() { 343 return listLocationsCallable; 344 } 345 346 @Override 347 public UnaryCallable<ListLocationsRequest, ListLocationsPagedResponse> listLocationsPagedCallable()348 listLocationsPagedCallable() { 349 return listLocationsPagedCallable; 350 } 351 352 @Override getLocationCallable()353 public UnaryCallable<GetLocationRequest, Location> getLocationCallable() { 354 return getLocationCallable; 355 } 356 357 @Override close()358 public final void close() { 359 try { 360 backgroundResources.close(); 361 } catch (RuntimeException e) { 362 throw e; 363 } catch (Exception e) { 364 throw new IllegalStateException("Failed to close resource", e); 365 } 366 } 367 368 @Override shutdown()369 public void shutdown() { 370 backgroundResources.shutdown(); 371 } 372 373 @Override isShutdown()374 public boolean isShutdown() { 375 return backgroundResources.isShutdown(); 376 } 377 378 @Override isTerminated()379 public boolean isTerminated() { 380 return backgroundResources.isTerminated(); 381 } 382 383 @Override shutdownNow()384 public void shutdownNow() { 385 backgroundResources.shutdownNow(); 386 } 387 388 @Override awaitTermination(long duration, TimeUnit unit)389 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 390 return backgroundResources.awaitTermination(duration, unit); 391 } 392 } 393