• 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.retail.v2.stub;
18 
19 import com.google.api.gax.core.BackgroundResource;
20 import com.google.api.gax.core.BackgroundResourceAggregation;
21 import com.google.api.gax.grpc.GrpcCallSettings;
22 import com.google.api.gax.grpc.GrpcStubCallableFactory;
23 import com.google.api.gax.rpc.ClientContext;
24 import com.google.api.gax.rpc.OperationCallable;
25 import com.google.api.gax.rpc.UnaryCallable;
26 import com.google.cloud.retail.v2.CompleteQueryRequest;
27 import com.google.cloud.retail.v2.CompleteQueryResponse;
28 import com.google.cloud.retail.v2.ImportCompletionDataRequest;
29 import com.google.cloud.retail.v2.ImportCompletionDataResponse;
30 import com.google.cloud.retail.v2.ImportMetadata;
31 import com.google.common.collect.ImmutableMap;
32 import com.google.longrunning.Operation;
33 import com.google.longrunning.stub.GrpcOperationsStub;
34 import io.grpc.MethodDescriptor;
35 import io.grpc.protobuf.ProtoUtils;
36 import java.io.IOException;
37 import java.util.concurrent.TimeUnit;
38 import javax.annotation.Generated;
39 
40 // AUTO-GENERATED DOCUMENTATION AND CLASS.
41 /**
42  * gRPC stub implementation for the CompletionService service API.
43  *
44  * <p>This class is for advanced usage and reflects the underlying API directly.
45  */
46 @Generated("by gapic-generator-java")
47 public class GrpcCompletionServiceStub extends CompletionServiceStub {
48   private static final MethodDescriptor<CompleteQueryRequest, CompleteQueryResponse>
49       completeQueryMethodDescriptor =
50           MethodDescriptor.<CompleteQueryRequest, CompleteQueryResponse>newBuilder()
51               .setType(MethodDescriptor.MethodType.UNARY)
52               .setFullMethodName("google.cloud.retail.v2.CompletionService/CompleteQuery")
53               .setRequestMarshaller(
54                   ProtoUtils.marshaller(CompleteQueryRequest.getDefaultInstance()))
55               .setResponseMarshaller(
56                   ProtoUtils.marshaller(CompleteQueryResponse.getDefaultInstance()))
57               .build();
58 
59   private static final MethodDescriptor<ImportCompletionDataRequest, Operation>
60       importCompletionDataMethodDescriptor =
61           MethodDescriptor.<ImportCompletionDataRequest, Operation>newBuilder()
62               .setType(MethodDescriptor.MethodType.UNARY)
63               .setFullMethodName("google.cloud.retail.v2.CompletionService/ImportCompletionData")
64               .setRequestMarshaller(
65                   ProtoUtils.marshaller(ImportCompletionDataRequest.getDefaultInstance()))
66               .setResponseMarshaller(ProtoUtils.marshaller(Operation.getDefaultInstance()))
67               .build();
68 
69   private final UnaryCallable<CompleteQueryRequest, CompleteQueryResponse> completeQueryCallable;
70   private final UnaryCallable<ImportCompletionDataRequest, Operation> importCompletionDataCallable;
71   private final OperationCallable<
72           ImportCompletionDataRequest, ImportCompletionDataResponse, ImportMetadata>
73       importCompletionDataOperationCallable;
74 
75   private final BackgroundResource backgroundResources;
76   private final GrpcOperationsStub operationsStub;
77   private final GrpcStubCallableFactory callableFactory;
78 
create(CompletionServiceStubSettings settings)79   public static final GrpcCompletionServiceStub create(CompletionServiceStubSettings settings)
80       throws IOException {
81     return new GrpcCompletionServiceStub(settings, ClientContext.create(settings));
82   }
83 
create(ClientContext clientContext)84   public static final GrpcCompletionServiceStub create(ClientContext clientContext)
85       throws IOException {
86     return new GrpcCompletionServiceStub(
87         CompletionServiceStubSettings.newBuilder().build(), clientContext);
88   }
89 
create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)90   public static final GrpcCompletionServiceStub create(
91       ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
92     return new GrpcCompletionServiceStub(
93         CompletionServiceStubSettings.newBuilder().build(), clientContext, callableFactory);
94   }
95 
96   /**
97    * Constructs an instance of GrpcCompletionServiceStub, using the given settings. This is
98    * protected so that it is easy to make a subclass, but otherwise, the static factory methods
99    * should be preferred.
100    */
GrpcCompletionServiceStub( CompletionServiceStubSettings settings, ClientContext clientContext)101   protected GrpcCompletionServiceStub(
102       CompletionServiceStubSettings settings, ClientContext clientContext) throws IOException {
103     this(settings, clientContext, new GrpcCompletionServiceCallableFactory());
104   }
105 
106   /**
107    * Constructs an instance of GrpcCompletionServiceStub, using the given settings. This is
108    * protected so that it is easy to make a subclass, but otherwise, the static factory methods
109    * should be preferred.
110    */
GrpcCompletionServiceStub( CompletionServiceStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)111   protected GrpcCompletionServiceStub(
112       CompletionServiceStubSettings settings,
113       ClientContext clientContext,
114       GrpcStubCallableFactory callableFactory)
115       throws IOException {
116     this.callableFactory = callableFactory;
117     this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);
118 
119     GrpcCallSettings<CompleteQueryRequest, CompleteQueryResponse> completeQueryTransportSettings =
120         GrpcCallSettings.<CompleteQueryRequest, CompleteQueryResponse>newBuilder()
121             .setMethodDescriptor(completeQueryMethodDescriptor)
122             .setParamsExtractor(
123                 request -> {
124                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
125                   params.put("catalog", String.valueOf(request.getCatalog()));
126                   return params.build();
127                 })
128             .build();
129     GrpcCallSettings<ImportCompletionDataRequest, Operation> importCompletionDataTransportSettings =
130         GrpcCallSettings.<ImportCompletionDataRequest, Operation>newBuilder()
131             .setMethodDescriptor(importCompletionDataMethodDescriptor)
132             .setParamsExtractor(
133                 request -> {
134                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
135                   params.put("parent", String.valueOf(request.getParent()));
136                   return params.build();
137                 })
138             .build();
139 
140     this.completeQueryCallable =
141         callableFactory.createUnaryCallable(
142             completeQueryTransportSettings, settings.completeQuerySettings(), clientContext);
143     this.importCompletionDataCallable =
144         callableFactory.createUnaryCallable(
145             importCompletionDataTransportSettings,
146             settings.importCompletionDataSettings(),
147             clientContext);
148     this.importCompletionDataOperationCallable =
149         callableFactory.createOperationCallable(
150             importCompletionDataTransportSettings,
151             settings.importCompletionDataOperationSettings(),
152             clientContext,
153             operationsStub);
154 
155     this.backgroundResources =
156         new BackgroundResourceAggregation(clientContext.getBackgroundResources());
157   }
158 
getOperationsStub()159   public GrpcOperationsStub getOperationsStub() {
160     return operationsStub;
161   }
162 
163   @Override
completeQueryCallable()164   public UnaryCallable<CompleteQueryRequest, CompleteQueryResponse> completeQueryCallable() {
165     return completeQueryCallable;
166   }
167 
168   @Override
importCompletionDataCallable()169   public UnaryCallable<ImportCompletionDataRequest, Operation> importCompletionDataCallable() {
170     return importCompletionDataCallable;
171   }
172 
173   @Override
174   public OperationCallable<
175           ImportCompletionDataRequest, ImportCompletionDataResponse, ImportMetadata>
importCompletionDataOperationCallable()176       importCompletionDataOperationCallable() {
177     return importCompletionDataOperationCallable;
178   }
179 
180   @Override
close()181   public final void close() {
182     try {
183       backgroundResources.close();
184     } catch (RuntimeException e) {
185       throw e;
186     } catch (Exception e) {
187       throw new IllegalStateException("Failed to close resource", e);
188     }
189   }
190 
191   @Override
shutdown()192   public void shutdown() {
193     backgroundResources.shutdown();
194   }
195 
196   @Override
isShutdown()197   public boolean isShutdown() {
198     return backgroundResources.isShutdown();
199   }
200 
201   @Override
isTerminated()202   public boolean isTerminated() {
203     return backgroundResources.isTerminated();
204   }
205 
206   @Override
shutdownNow()207   public void shutdownNow() {
208     backgroundResources.shutdownNow();
209   }
210 
211   @Override
awaitTermination(long duration, TimeUnit unit)212   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
213     return backgroundResources.awaitTermination(duration, unit);
214   }
215 }
216