• 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.automl.v1.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.automl.v1.BatchPredictRequest;
27 import com.google.cloud.automl.v1.BatchPredictResult;
28 import com.google.cloud.automl.v1.OperationMetadata;
29 import com.google.cloud.automl.v1.PredictRequest;
30 import com.google.cloud.automl.v1.PredictResponse;
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 PredictionService 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 GrpcPredictionServiceStub extends PredictionServiceStub {
48   private static final MethodDescriptor<PredictRequest, PredictResponse> predictMethodDescriptor =
49       MethodDescriptor.<PredictRequest, PredictResponse>newBuilder()
50           .setType(MethodDescriptor.MethodType.UNARY)
51           .setFullMethodName("google.cloud.automl.v1.PredictionService/Predict")
52           .setRequestMarshaller(ProtoUtils.marshaller(PredictRequest.getDefaultInstance()))
53           .setResponseMarshaller(ProtoUtils.marshaller(PredictResponse.getDefaultInstance()))
54           .build();
55 
56   private static final MethodDescriptor<BatchPredictRequest, Operation>
57       batchPredictMethodDescriptor =
58           MethodDescriptor.<BatchPredictRequest, Operation>newBuilder()
59               .setType(MethodDescriptor.MethodType.UNARY)
60               .setFullMethodName("google.cloud.automl.v1.PredictionService/BatchPredict")
61               .setRequestMarshaller(ProtoUtils.marshaller(BatchPredictRequest.getDefaultInstance()))
62               .setResponseMarshaller(ProtoUtils.marshaller(Operation.getDefaultInstance()))
63               .build();
64 
65   private final UnaryCallable<PredictRequest, PredictResponse> predictCallable;
66   private final UnaryCallable<BatchPredictRequest, Operation> batchPredictCallable;
67   private final OperationCallable<BatchPredictRequest, BatchPredictResult, OperationMetadata>
68       batchPredictOperationCallable;
69 
70   private final BackgroundResource backgroundResources;
71   private final GrpcOperationsStub operationsStub;
72   private final GrpcStubCallableFactory callableFactory;
73 
create(PredictionServiceStubSettings settings)74   public static final GrpcPredictionServiceStub create(PredictionServiceStubSettings settings)
75       throws IOException {
76     return new GrpcPredictionServiceStub(settings, ClientContext.create(settings));
77   }
78 
create(ClientContext clientContext)79   public static final GrpcPredictionServiceStub create(ClientContext clientContext)
80       throws IOException {
81     return new GrpcPredictionServiceStub(
82         PredictionServiceStubSettings.newBuilder().build(), clientContext);
83   }
84 
create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)85   public static final GrpcPredictionServiceStub create(
86       ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException {
87     return new GrpcPredictionServiceStub(
88         PredictionServiceStubSettings.newBuilder().build(), clientContext, callableFactory);
89   }
90 
91   /**
92    * Constructs an instance of GrpcPredictionServiceStub, using the given settings. This is
93    * protected so that it is easy to make a subclass, but otherwise, the static factory methods
94    * should be preferred.
95    */
GrpcPredictionServiceStub( PredictionServiceStubSettings settings, ClientContext clientContext)96   protected GrpcPredictionServiceStub(
97       PredictionServiceStubSettings settings, ClientContext clientContext) throws IOException {
98     this(settings, clientContext, new GrpcPredictionServiceCallableFactory());
99   }
100 
101   /**
102    * Constructs an instance of GrpcPredictionServiceStub, using the given settings. This is
103    * protected so that it is easy to make a subclass, but otherwise, the static factory methods
104    * should be preferred.
105    */
GrpcPredictionServiceStub( PredictionServiceStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)106   protected GrpcPredictionServiceStub(
107       PredictionServiceStubSettings settings,
108       ClientContext clientContext,
109       GrpcStubCallableFactory callableFactory)
110       throws IOException {
111     this.callableFactory = callableFactory;
112     this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);
113 
114     GrpcCallSettings<PredictRequest, PredictResponse> predictTransportSettings =
115         GrpcCallSettings.<PredictRequest, PredictResponse>newBuilder()
116             .setMethodDescriptor(predictMethodDescriptor)
117             .setParamsExtractor(
118                 request -> {
119                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
120                   params.put("name", String.valueOf(request.getName()));
121                   return params.build();
122                 })
123             .build();
124     GrpcCallSettings<BatchPredictRequest, Operation> batchPredictTransportSettings =
125         GrpcCallSettings.<BatchPredictRequest, Operation>newBuilder()
126             .setMethodDescriptor(batchPredictMethodDescriptor)
127             .setParamsExtractor(
128                 request -> {
129                   ImmutableMap.Builder<String, String> params = ImmutableMap.builder();
130                   params.put("name", String.valueOf(request.getName()));
131                   return params.build();
132                 })
133             .build();
134 
135     this.predictCallable =
136         callableFactory.createUnaryCallable(
137             predictTransportSettings, settings.predictSettings(), clientContext);
138     this.batchPredictCallable =
139         callableFactory.createUnaryCallable(
140             batchPredictTransportSettings, settings.batchPredictSettings(), clientContext);
141     this.batchPredictOperationCallable =
142         callableFactory.createOperationCallable(
143             batchPredictTransportSettings,
144             settings.batchPredictOperationSettings(),
145             clientContext,
146             operationsStub);
147 
148     this.backgroundResources =
149         new BackgroundResourceAggregation(clientContext.getBackgroundResources());
150   }
151 
getOperationsStub()152   public GrpcOperationsStub getOperationsStub() {
153     return operationsStub;
154   }
155 
156   @Override
predictCallable()157   public UnaryCallable<PredictRequest, PredictResponse> predictCallable() {
158     return predictCallable;
159   }
160 
161   @Override
batchPredictCallable()162   public UnaryCallable<BatchPredictRequest, Operation> batchPredictCallable() {
163     return batchPredictCallable;
164   }
165 
166   @Override
167   public OperationCallable<BatchPredictRequest, BatchPredictResult, OperationMetadata>
batchPredictOperationCallable()168       batchPredictOperationCallable() {
169     return batchPredictOperationCallable;
170   }
171 
172   @Override
close()173   public final void close() {
174     try {
175       backgroundResources.close();
176     } catch (RuntimeException e) {
177       throw e;
178     } catch (Exception e) {
179       throw new IllegalStateException("Failed to close resource", e);
180     }
181   }
182 
183   @Override
shutdown()184   public void shutdown() {
185     backgroundResources.shutdown();
186   }
187 
188   @Override
isShutdown()189   public boolean isShutdown() {
190     return backgroundResources.isShutdown();
191   }
192 
193   @Override
isTerminated()194   public boolean isTerminated() {
195     return backgroundResources.isTerminated();
196   }
197 
198   @Override
shutdownNow()199   public void shutdownNow() {
200     backgroundResources.shutdownNow();
201   }
202 
203   @Override
awaitTermination(long duration, TimeUnit unit)204   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
205     return backgroundResources.awaitTermination(duration, unit);
206   }
207 }
208