• 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.UnaryCallable;
25 import com.google.cloud.retail.v2.PredictRequest;
26 import com.google.cloud.retail.v2.PredictResponse;
27 import com.google.common.collect.ImmutableMap;
28 import com.google.longrunning.stub.GrpcOperationsStub;
29 import io.grpc.MethodDescriptor;
30 import io.grpc.protobuf.ProtoUtils;
31 import java.io.IOException;
32 import java.util.concurrent.TimeUnit;
33 import javax.annotation.Generated;
34 
35 // AUTO-GENERATED DOCUMENTATION AND CLASS.
36 /**
37  * gRPC stub implementation for the PredictionService service API.
38  *
39  * <p>This class is for advanced usage and reflects the underlying API directly.
40  */
41 @Generated("by gapic-generator-java")
42 public class GrpcPredictionServiceStub extends PredictionServiceStub {
43   private static final MethodDescriptor<PredictRequest, PredictResponse> predictMethodDescriptor =
44       MethodDescriptor.<PredictRequest, PredictResponse>newBuilder()
45           .setType(MethodDescriptor.MethodType.UNARY)
46           .setFullMethodName("google.cloud.retail.v2.PredictionService/Predict")
47           .setRequestMarshaller(ProtoUtils.marshaller(PredictRequest.getDefaultInstance()))
48           .setResponseMarshaller(ProtoUtils.marshaller(PredictResponse.getDefaultInstance()))
49           .build();
50 
51   private final UnaryCallable<PredictRequest, PredictResponse> predictCallable;
52 
53   private final BackgroundResource backgroundResources;
54   private final GrpcOperationsStub operationsStub;
55   private final GrpcStubCallableFactory callableFactory;
56 
create(PredictionServiceStubSettings settings)57   public static final GrpcPredictionServiceStub create(PredictionServiceStubSettings settings)
58       throws IOException {
59     return new GrpcPredictionServiceStub(settings, ClientContext.create(settings));
60   }
61 
create(ClientContext clientContext)62   public static final GrpcPredictionServiceStub create(ClientContext clientContext)
63       throws IOException {
64     return new GrpcPredictionServiceStub(
65         PredictionServiceStubSettings.newBuilder().build(), clientContext);
66   }
67 
create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)68   public static final GrpcPredictionServiceStub create(
69       ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
70     return new GrpcPredictionServiceStub(
71         PredictionServiceStubSettings.newBuilder().build(), clientContext, callableFactory);
72   }
73 
74   /**
75    * Constructs an instance of GrpcPredictionServiceStub, using the given settings. This is
76    * protected so that it is easy to make a subclass, but otherwise, the static factory methods
77    * should be preferred.
78    */
GrpcPredictionServiceStub( PredictionServiceStubSettings settings, ClientContext clientContext)79   protected GrpcPredictionServiceStub(
80       PredictionServiceStubSettings settings, ClientContext clientContext) throws IOException {
81     this(settings, clientContext, new GrpcPredictionServiceCallableFactory());
82   }
83 
84   /**
85    * Constructs an instance of GrpcPredictionServiceStub, using the given settings. This is
86    * protected so that it is easy to make a subclass, but otherwise, the static factory methods
87    * should be preferred.
88    */
GrpcPredictionServiceStub( PredictionServiceStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)89   protected GrpcPredictionServiceStub(
90       PredictionServiceStubSettings settings,
91       ClientContext clientContext,
92       GrpcStubCallableFactory callableFactory)
93       throws IOException {
94     this.callableFactory = callableFactory;
95     this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);
96 
97     GrpcCallSettings<PredictRequest, PredictResponse> predictTransportSettings =
98         GrpcCallSettings.<PredictRequest, PredictResponse>newBuilder()
99             .setMethodDescriptor(predictMethodDescriptor)
100             .setParamsExtractor(
101                 request -> {
102                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
103                   params.put("placement", String.valueOf(request.getPlacement()));
104                   return params.build();
105                 })
106             .build();
107 
108     this.predictCallable =
109         callableFactory.createUnaryCallable(
110             predictTransportSettings, settings.predictSettings(), clientContext);
111 
112     this.backgroundResources =
113         new BackgroundResourceAggregation(clientContext.getBackgroundResources());
114   }
115 
getOperationsStub()116   public GrpcOperationsStub getOperationsStub() {
117     return operationsStub;
118   }
119 
120   @Override
predictCallable()121   public UnaryCallable<PredictRequest, PredictResponse> predictCallable() {
122     return predictCallable;
123   }
124 
125   @Override
close()126   public final void close() {
127     try {
128       backgroundResources.close();
129     } catch (RuntimeException e) {
130       throw e;
131     } catch (Exception e) {
132       throw new IllegalStateException("Failed to close resource", e);
133     }
134   }
135 
136   @Override
shutdown()137   public void shutdown() {
138     backgroundResources.shutdown();
139   }
140 
141   @Override
isShutdown()142   public boolean isShutdown() {
143     return backgroundResources.isShutdown();
144   }
145 
146   @Override
isTerminated()147   public boolean isTerminated() {
148     return backgroundResources.isTerminated();
149   }
150 
151   @Override
shutdownNow()152   public void shutdownNow() {
153     backgroundResources.shutdownNow();
154   }
155 
156   @Override
awaitTermination(long duration, TimeUnit unit)157   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
158     return backgroundResources.awaitTermination(duration, unit);
159   }
160 }
161