• 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.vision.v1p1beta1;
18 
19 import com.google.api.core.BetaApi;
20 import com.google.api.gax.core.BackgroundResource;
21 import com.google.api.gax.rpc.UnaryCallable;
22 import com.google.cloud.vision.v1p1beta1.stub.ImageAnnotatorStub;
23 import com.google.cloud.vision.v1p1beta1.stub.ImageAnnotatorStubSettings;
24 import java.io.IOException;
25 import java.util.List;
26 import java.util.concurrent.TimeUnit;
27 import javax.annotation.Generated;
28 
29 // AUTO-GENERATED DOCUMENTATION AND CLASS.
30 /**
31  * Service Description: Service that performs Google Cloud Vision API detection tasks over client
32  * images, such as face, landmark, logo, label, and text detection. The ImageAnnotator service
33  * returns detected entities from the images.
34  *
35  * <p>This class provides the ability to make remote calls to the backing service through method
36  * calls that map to API methods. Sample code to get started:
37  *
38  * <pre>{@code
39  * // This snippet has been automatically generated and should be regarded as a code template only.
40  * // It will require modifications to work:
41  * // - It may require correct/in-range values for request initialization.
42  * // - It may require specifying regional endpoints when creating the service client as shown in
43  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
44  * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
45  *   List<AnnotateImageRequest> requests = new ArrayList<>();
46  *   BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(requests);
47  * }
48  * }</pre>
49  *
50  * <p>Note: close() needs to be called on the ImageAnnotatorClient object to clean up resources such
51  * as threads. In the example above, try-with-resources is used, which automatically calls close().
52  *
53  * <p>The surface of this class includes several types of Java methods for each of the API's
54  * methods:
55  *
56  * <ol>
57  *   <li>A "flattened" method. With this type of method, the fields of the request type have been
58  *       converted into function parameters. It may be the case that not all fields are available as
59  *       parameters, and not every API method will have a flattened method entry point.
60  *   <li>A "request object" method. This type of method only takes one parameter, a request object,
61  *       which must be constructed before the call. Not every API method will have a request object
62  *       method.
63  *   <li>A "callable" method. This type of method takes no parameters and returns an immutable API
64  *       callable object, which can be used to initiate calls to the service.
65  * </ol>
66  *
67  * <p>See the individual methods for example code.
68  *
69  * <p>Many parameters require resource names to be formatted in a particular way. To assist with
70  * these names, this class includes a format method for each type of name, and additionally a parse
71  * method to extract the individual identifiers contained within names that are returned.
72  *
73  * <p>This class can be customized by passing in a custom instance of ImageAnnotatorSettings to
74  * create(). For example:
75  *
76  * <p>To customize credentials:
77  *
78  * <pre>{@code
79  * // This snippet has been automatically generated and should be regarded as a code template only.
80  * // It will require modifications to work:
81  * // - It may require correct/in-range values for request initialization.
82  * // - It may require specifying regional endpoints when creating the service client as shown in
83  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
84  * ImageAnnotatorSettings imageAnnotatorSettings =
85  *     ImageAnnotatorSettings.newBuilder()
86  *         .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials))
87  *         .build();
88  * ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings);
89  * }</pre>
90  *
91  * <p>To customize the endpoint:
92  *
93  * <pre>{@code
94  * // This snippet has been automatically generated and should be regarded as a code template only.
95  * // It will require modifications to work:
96  * // - It may require correct/in-range values for request initialization.
97  * // - It may require specifying regional endpoints when creating the service client as shown in
98  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
99  * ImageAnnotatorSettings imageAnnotatorSettings =
100  *     ImageAnnotatorSettings.newBuilder().setEndpoint(myEndpoint).build();
101  * ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings);
102  * }</pre>
103  *
104  * <p>To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over
105  * the wire:
106  *
107  * <pre>{@code
108  * // This snippet has been automatically generated and should be regarded as a code template only.
109  * // It will require modifications to work:
110  * // - It may require correct/in-range values for request initialization.
111  * // - It may require specifying regional endpoints when creating the service client as shown in
112  * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
113  * ImageAnnotatorSettings imageAnnotatorSettings =
114  *     ImageAnnotatorSettings.newHttpJsonBuilder().build();
115  * ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings);
116  * }</pre>
117  *
118  * <p>Please refer to the GitHub repository's samples for more quickstart code snippets.
119  */
120 @BetaApi
121 @Generated("by gapic-generator-java")
122 public class ImageAnnotatorClient implements BackgroundResource {
123   private final ImageAnnotatorSettings settings;
124   private final ImageAnnotatorStub stub;
125 
126   /** Constructs an instance of ImageAnnotatorClient with default settings. */
create()127   public static final ImageAnnotatorClient create() throws IOException {
128     return create(ImageAnnotatorSettings.newBuilder().build());
129   }
130 
131   /**
132    * Constructs an instance of ImageAnnotatorClient, using the given settings. The channels are
133    * created based on the settings passed in, or defaults for any settings that are not set.
134    */
create(ImageAnnotatorSettings settings)135   public static final ImageAnnotatorClient create(ImageAnnotatorSettings settings)
136       throws IOException {
137     return new ImageAnnotatorClient(settings);
138   }
139 
140   /**
141    * Constructs an instance of ImageAnnotatorClient, using the given stub for making calls. This is
142    * for advanced usage - prefer using create(ImageAnnotatorSettings).
143    */
create(ImageAnnotatorStub stub)144   public static final ImageAnnotatorClient create(ImageAnnotatorStub stub) {
145     return new ImageAnnotatorClient(stub);
146   }
147 
148   /**
149    * Constructs an instance of ImageAnnotatorClient, using the given settings. This is protected so
150    * that it is easy to make a subclass, but otherwise, the static factory methods should be
151    * preferred.
152    */
ImageAnnotatorClient(ImageAnnotatorSettings settings)153   protected ImageAnnotatorClient(ImageAnnotatorSettings settings) throws IOException {
154     this.settings = settings;
155     this.stub = ((ImageAnnotatorStubSettings) settings.getStubSettings()).createStub();
156   }
157 
ImageAnnotatorClient(ImageAnnotatorStub stub)158   protected ImageAnnotatorClient(ImageAnnotatorStub stub) {
159     this.settings = null;
160     this.stub = stub;
161   }
162 
getSettings()163   public final ImageAnnotatorSettings getSettings() {
164     return settings;
165   }
166 
getStub()167   public ImageAnnotatorStub getStub() {
168     return stub;
169   }
170 
171   // AUTO-GENERATED DOCUMENTATION AND METHOD.
172   /**
173    * Run image detection and annotation for a batch of images.
174    *
175    * <p>Sample code:
176    *
177    * <pre>{@code
178    * // This snippet has been automatically generated and should be regarded as a code template only.
179    * // It will require modifications to work:
180    * // - It may require correct/in-range values for request initialization.
181    * // - It may require specifying regional endpoints when creating the service client as shown in
182    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
183    * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
184    *   List<AnnotateImageRequest> requests = new ArrayList<>();
185    *   BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(requests);
186    * }
187    * }</pre>
188    *
189    * @param requests Required. Individual image annotation requests for this batch.
190    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
191    */
batchAnnotateImages( List<AnnotateImageRequest> requests)192   public final BatchAnnotateImagesResponse batchAnnotateImages(
193       List<AnnotateImageRequest> requests) {
194     BatchAnnotateImagesRequest request =
195         BatchAnnotateImagesRequest.newBuilder().addAllRequests(requests).build();
196     return batchAnnotateImages(request);
197   }
198 
199   // AUTO-GENERATED DOCUMENTATION AND METHOD.
200   /**
201    * Run image detection and annotation for a batch of images.
202    *
203    * <p>Sample code:
204    *
205    * <pre>{@code
206    * // This snippet has been automatically generated and should be regarded as a code template only.
207    * // It will require modifications to work:
208    * // - It may require correct/in-range values for request initialization.
209    * // - It may require specifying regional endpoints when creating the service client as shown in
210    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
211    * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
212    *   BatchAnnotateImagesRequest request =
213    *       BatchAnnotateImagesRequest.newBuilder()
214    *           .addAllRequests(new ArrayList<AnnotateImageRequest>())
215    *           .build();
216    *   BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(request);
217    * }
218    * }</pre>
219    *
220    * @param request The request object containing all of the parameters for the API call.
221    * @throws com.google.api.gax.rpc.ApiException if the remote call fails
222    */
batchAnnotateImages(BatchAnnotateImagesRequest request)223   public final BatchAnnotateImagesResponse batchAnnotateImages(BatchAnnotateImagesRequest request) {
224     return batchAnnotateImagesCallable().call(request);
225   }
226 
227   // AUTO-GENERATED DOCUMENTATION AND METHOD.
228   /**
229    * Run image detection and annotation for a batch of images.
230    *
231    * <p>Sample code:
232    *
233    * <pre>{@code
234    * // This snippet has been automatically generated and should be regarded as a code template only.
235    * // It will require modifications to work:
236    * // - It may require correct/in-range values for request initialization.
237    * // - It may require specifying regional endpoints when creating the service client as shown in
238    * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
239    * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) {
240    *   BatchAnnotateImagesRequest request =
241    *       BatchAnnotateImagesRequest.newBuilder()
242    *           .addAllRequests(new ArrayList<AnnotateImageRequest>())
243    *           .build();
244    *   ApiFuture<BatchAnnotateImagesResponse> future =
245    *       imageAnnotatorClient.batchAnnotateImagesCallable().futureCall(request);
246    *   // Do something.
247    *   BatchAnnotateImagesResponse response = future.get();
248    * }
249    * }</pre>
250    */
251   public final UnaryCallable<BatchAnnotateImagesRequest, BatchAnnotateImagesResponse>
batchAnnotateImagesCallable()252       batchAnnotateImagesCallable() {
253     return stub.batchAnnotateImagesCallable();
254   }
255 
256   @Override
close()257   public final void close() {
258     stub.close();
259   }
260 
261   @Override
shutdown()262   public void shutdown() {
263     stub.shutdown();
264   }
265 
266   @Override
isShutdown()267   public boolean isShutdown() {
268     return stub.isShutdown();
269   }
270 
271   @Override
isTerminated()272   public boolean isTerminated() {
273     return stub.isTerminated();
274   }
275 
276   @Override
shutdownNow()277   public void shutdownNow() {
278     stub.shutdownNow();
279   }
280 
281   @Override
awaitTermination(long duration, TimeUnit unit)282   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
283     return stub.awaitTermination(duration, unit);
284   }
285 }
286