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.v1p2beta1; 18 19 import com.google.api.core.BetaApi; 20 import com.google.api.gax.core.BackgroundResource; 21 import com.google.api.gax.httpjson.longrunning.OperationsClient; 22 import com.google.api.gax.longrunning.OperationFuture; 23 import com.google.api.gax.rpc.OperationCallable; 24 import com.google.api.gax.rpc.UnaryCallable; 25 import com.google.cloud.vision.v1p2beta1.stub.ImageAnnotatorStub; 26 import com.google.cloud.vision.v1p2beta1.stub.ImageAnnotatorStubSettings; 27 import com.google.longrunning.Operation; 28 import java.io.IOException; 29 import java.util.List; 30 import java.util.concurrent.TimeUnit; 31 import javax.annotation.Generated; 32 33 // AUTO-GENERATED DOCUMENTATION AND CLASS. 34 /** 35 * Service Description: Service that performs Google Cloud Vision API detection tasks over client 36 * images, such as face, landmark, logo, label, and text detection. The ImageAnnotator service 37 * returns detected entities from the images. 38 * 39 * <p>This class provides the ability to make remote calls to the backing service through method 40 * calls that map to API methods. Sample code to get started: 41 * 42 * <pre>{@code 43 * // This snippet has been automatically generated and should be regarded as a code template only. 44 * // It will require modifications to work: 45 * // - It may require correct/in-range values for request initialization. 46 * // - It may require specifying regional endpoints when creating the service client as shown in 47 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 48 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 49 * List<AnnotateImageRequest> requests = new ArrayList<>(); 50 * BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(requests); 51 * } 52 * }</pre> 53 * 54 * <p>Note: close() needs to be called on the ImageAnnotatorClient object to clean up resources such 55 * as threads. In the example above, try-with-resources is used, which automatically calls close(). 56 * 57 * <p>The surface of this class includes several types of Java methods for each of the API's 58 * methods: 59 * 60 * <ol> 61 * <li>A "flattened" method. With this type of method, the fields of the request type have been 62 * converted into function parameters. It may be the case that not all fields are available as 63 * parameters, and not every API method will have a flattened method entry point. 64 * <li>A "request object" method. This type of method only takes one parameter, a request object, 65 * which must be constructed before the call. Not every API method will have a request object 66 * method. 67 * <li>A "callable" method. This type of method takes no parameters and returns an immutable API 68 * callable object, which can be used to initiate calls to the service. 69 * </ol> 70 * 71 * <p>See the individual methods for example code. 72 * 73 * <p>Many parameters require resource names to be formatted in a particular way. To assist with 74 * these names, this class includes a format method for each type of name, and additionally a parse 75 * method to extract the individual identifiers contained within names that are returned. 76 * 77 * <p>This class can be customized by passing in a custom instance of ImageAnnotatorSettings to 78 * create(). For example: 79 * 80 * <p>To customize credentials: 81 * 82 * <pre>{@code 83 * // This snippet has been automatically generated and should be regarded as a code template only. 84 * // It will require modifications to work: 85 * // - It may require correct/in-range values for request initialization. 86 * // - It may require specifying regional endpoints when creating the service client as shown in 87 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 88 * ImageAnnotatorSettings imageAnnotatorSettings = 89 * ImageAnnotatorSettings.newBuilder() 90 * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) 91 * .build(); 92 * ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings); 93 * }</pre> 94 * 95 * <p>To customize the endpoint: 96 * 97 * <pre>{@code 98 * // This snippet has been automatically generated and should be regarded as a code template only. 99 * // It will require modifications to work: 100 * // - It may require correct/in-range values for request initialization. 101 * // - It may require specifying regional endpoints when creating the service client as shown in 102 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 103 * ImageAnnotatorSettings imageAnnotatorSettings = 104 * ImageAnnotatorSettings.newBuilder().setEndpoint(myEndpoint).build(); 105 * ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings); 106 * }</pre> 107 * 108 * <p>To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over 109 * the wire: 110 * 111 * <pre>{@code 112 * // This snippet has been automatically generated and should be regarded as a code template only. 113 * // It will require modifications to work: 114 * // - It may require correct/in-range values for request initialization. 115 * // - It may require specifying regional endpoints when creating the service client as shown in 116 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 117 * ImageAnnotatorSettings imageAnnotatorSettings = 118 * ImageAnnotatorSettings.newHttpJsonBuilder().build(); 119 * ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create(imageAnnotatorSettings); 120 * }</pre> 121 * 122 * <p>Please refer to the GitHub repository's samples for more quickstart code snippets. 123 */ 124 @BetaApi 125 @Generated("by gapic-generator-java") 126 public class ImageAnnotatorClient implements BackgroundResource { 127 private final ImageAnnotatorSettings settings; 128 private final ImageAnnotatorStub stub; 129 private final OperationsClient httpJsonOperationsClient; 130 private final com.google.longrunning.OperationsClient operationsClient; 131 132 /** Constructs an instance of ImageAnnotatorClient with default settings. */ create()133 public static final ImageAnnotatorClient create() throws IOException { 134 return create(ImageAnnotatorSettings.newBuilder().build()); 135 } 136 137 /** 138 * Constructs an instance of ImageAnnotatorClient, using the given settings. The channels are 139 * created based on the settings passed in, or defaults for any settings that are not set. 140 */ create(ImageAnnotatorSettings settings)141 public static final ImageAnnotatorClient create(ImageAnnotatorSettings settings) 142 throws IOException { 143 return new ImageAnnotatorClient(settings); 144 } 145 146 /** 147 * Constructs an instance of ImageAnnotatorClient, using the given stub for making calls. This is 148 * for advanced usage - prefer using create(ImageAnnotatorSettings). 149 */ create(ImageAnnotatorStub stub)150 public static final ImageAnnotatorClient create(ImageAnnotatorStub stub) { 151 return new ImageAnnotatorClient(stub); 152 } 153 154 /** 155 * Constructs an instance of ImageAnnotatorClient, using the given settings. This is protected so 156 * that it is easy to make a subclass, but otherwise, the static factory methods should be 157 * preferred. 158 */ ImageAnnotatorClient(ImageAnnotatorSettings settings)159 protected ImageAnnotatorClient(ImageAnnotatorSettings settings) throws IOException { 160 this.settings = settings; 161 this.stub = ((ImageAnnotatorStubSettings) settings.getStubSettings()).createStub(); 162 this.operationsClient = 163 com.google.longrunning.OperationsClient.create(this.stub.getOperationsStub()); 164 this.httpJsonOperationsClient = OperationsClient.create(this.stub.getHttpJsonOperationsStub()); 165 } 166 ImageAnnotatorClient(ImageAnnotatorStub stub)167 protected ImageAnnotatorClient(ImageAnnotatorStub stub) { 168 this.settings = null; 169 this.stub = stub; 170 this.operationsClient = 171 com.google.longrunning.OperationsClient.create(this.stub.getOperationsStub()); 172 this.httpJsonOperationsClient = OperationsClient.create(this.stub.getHttpJsonOperationsStub()); 173 } 174 getSettings()175 public final ImageAnnotatorSettings getSettings() { 176 return settings; 177 } 178 getStub()179 public ImageAnnotatorStub getStub() { 180 return stub; 181 } 182 183 /** 184 * Returns the OperationsClient that can be used to query the status of a long-running operation 185 * returned by another API method call. 186 */ getOperationsClient()187 public final com.google.longrunning.OperationsClient getOperationsClient() { 188 return operationsClient; 189 } 190 191 /** 192 * Returns the OperationsClient that can be used to query the status of a long-running operation 193 * returned by another API method call. 194 */ 195 @BetaApi getHttpJsonOperationsClient()196 public final OperationsClient getHttpJsonOperationsClient() { 197 return httpJsonOperationsClient; 198 } 199 200 // AUTO-GENERATED DOCUMENTATION AND METHOD. 201 /** 202 * Run image detection and annotation for a batch of images. 203 * 204 * <p>Sample code: 205 * 206 * <pre>{@code 207 * // This snippet has been automatically generated and should be regarded as a code template only. 208 * // It will require modifications to work: 209 * // - It may require correct/in-range values for request initialization. 210 * // - It may require specifying regional endpoints when creating the service client as shown in 211 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 212 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 213 * List<AnnotateImageRequest> requests = new ArrayList<>(); 214 * BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(requests); 215 * } 216 * }</pre> 217 * 218 * @param requests Required. Individual image annotation requests for this batch. 219 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 220 */ batchAnnotateImages( List<AnnotateImageRequest> requests)221 public final BatchAnnotateImagesResponse batchAnnotateImages( 222 List<AnnotateImageRequest> requests) { 223 BatchAnnotateImagesRequest request = 224 BatchAnnotateImagesRequest.newBuilder().addAllRequests(requests).build(); 225 return batchAnnotateImages(request); 226 } 227 228 // AUTO-GENERATED DOCUMENTATION AND METHOD. 229 /** 230 * Run image detection and annotation for a batch of images. 231 * 232 * <p>Sample code: 233 * 234 * <pre>{@code 235 * // This snippet has been automatically generated and should be regarded as a code template only. 236 * // It will require modifications to work: 237 * // - It may require correct/in-range values for request initialization. 238 * // - It may require specifying regional endpoints when creating the service client as shown in 239 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 240 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 241 * BatchAnnotateImagesRequest request = 242 * BatchAnnotateImagesRequest.newBuilder() 243 * .addAllRequests(new ArrayList<AnnotateImageRequest>()) 244 * .build(); 245 * BatchAnnotateImagesResponse response = imageAnnotatorClient.batchAnnotateImages(request); 246 * } 247 * }</pre> 248 * 249 * @param request The request object containing all of the parameters for the API call. 250 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 251 */ batchAnnotateImages(BatchAnnotateImagesRequest request)252 public final BatchAnnotateImagesResponse batchAnnotateImages(BatchAnnotateImagesRequest request) { 253 return batchAnnotateImagesCallable().call(request); 254 } 255 256 // AUTO-GENERATED DOCUMENTATION AND METHOD. 257 /** 258 * Run image detection and annotation for a batch of images. 259 * 260 * <p>Sample code: 261 * 262 * <pre>{@code 263 * // This snippet has been automatically generated and should be regarded as a code template only. 264 * // It will require modifications to work: 265 * // - It may require correct/in-range values for request initialization. 266 * // - It may require specifying regional endpoints when creating the service client as shown in 267 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 268 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 269 * BatchAnnotateImagesRequest request = 270 * BatchAnnotateImagesRequest.newBuilder() 271 * .addAllRequests(new ArrayList<AnnotateImageRequest>()) 272 * .build(); 273 * ApiFuture<BatchAnnotateImagesResponse> future = 274 * imageAnnotatorClient.batchAnnotateImagesCallable().futureCall(request); 275 * // Do something. 276 * BatchAnnotateImagesResponse response = future.get(); 277 * } 278 * }</pre> 279 */ 280 public final UnaryCallable<BatchAnnotateImagesRequest, BatchAnnotateImagesResponse> batchAnnotateImagesCallable()281 batchAnnotateImagesCallable() { 282 return stub.batchAnnotateImagesCallable(); 283 } 284 285 // AUTO-GENERATED DOCUMENTATION AND METHOD. 286 /** 287 * Run async image detection and annotation for a list of generic files (e.g. PDF) which may 288 * contain multiple pages and multiple images per page. Progress and results can be retrieved 289 * through the `google.longrunning.Operations` interface. `Operation.metadata` contains 290 * `OperationMetadata` (metadata). `Operation.response` contains `AsyncBatchAnnotateFilesResponse` 291 * (results). 292 * 293 * <p>Sample code: 294 * 295 * <pre>{@code 296 * // This snippet has been automatically generated and should be regarded as a code template only. 297 * // It will require modifications to work: 298 * // - It may require correct/in-range values for request initialization. 299 * // - It may require specifying regional endpoints when creating the service client as shown in 300 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 301 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 302 * List<AsyncAnnotateFileRequest> requests = new ArrayList<>(); 303 * AsyncBatchAnnotateFilesResponse response = 304 * imageAnnotatorClient.asyncBatchAnnotateFilesAsync(requests).get(); 305 * } 306 * }</pre> 307 * 308 * @param requests Required. Individual async file annotation requests for this batch. 309 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 310 */ 311 public final OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> asyncBatchAnnotateFilesAsync(List<AsyncAnnotateFileRequest> requests)312 asyncBatchAnnotateFilesAsync(List<AsyncAnnotateFileRequest> requests) { 313 AsyncBatchAnnotateFilesRequest request = 314 AsyncBatchAnnotateFilesRequest.newBuilder().addAllRequests(requests).build(); 315 return asyncBatchAnnotateFilesAsync(request); 316 } 317 318 // AUTO-GENERATED DOCUMENTATION AND METHOD. 319 /** 320 * Run async image detection and annotation for a list of generic files (e.g. PDF) which may 321 * contain multiple pages and multiple images per page. Progress and results can be retrieved 322 * through the `google.longrunning.Operations` interface. `Operation.metadata` contains 323 * `OperationMetadata` (metadata). `Operation.response` contains `AsyncBatchAnnotateFilesResponse` 324 * (results). 325 * 326 * <p>Sample code: 327 * 328 * <pre>{@code 329 * // This snippet has been automatically generated and should be regarded as a code template only. 330 * // It will require modifications to work: 331 * // - It may require correct/in-range values for request initialization. 332 * // - It may require specifying regional endpoints when creating the service client as shown in 333 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 334 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 335 * AsyncBatchAnnotateFilesRequest request = 336 * AsyncBatchAnnotateFilesRequest.newBuilder() 337 * .addAllRequests(new ArrayList<AsyncAnnotateFileRequest>()) 338 * .build(); 339 * AsyncBatchAnnotateFilesResponse response = 340 * imageAnnotatorClient.asyncBatchAnnotateFilesAsync(request).get(); 341 * } 342 * }</pre> 343 * 344 * @param request The request object containing all of the parameters for the API call. 345 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 346 */ 347 public final OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> asyncBatchAnnotateFilesAsync(AsyncBatchAnnotateFilesRequest request)348 asyncBatchAnnotateFilesAsync(AsyncBatchAnnotateFilesRequest request) { 349 return asyncBatchAnnotateFilesOperationCallable().futureCall(request); 350 } 351 352 // AUTO-GENERATED DOCUMENTATION AND METHOD. 353 /** 354 * Run async image detection and annotation for a list of generic files (e.g. PDF) which may 355 * contain multiple pages and multiple images per page. Progress and results can be retrieved 356 * through the `google.longrunning.Operations` interface. `Operation.metadata` contains 357 * `OperationMetadata` (metadata). `Operation.response` contains `AsyncBatchAnnotateFilesResponse` 358 * (results). 359 * 360 * <p>Sample code: 361 * 362 * <pre>{@code 363 * // This snippet has been automatically generated and should be regarded as a code template only. 364 * // It will require modifications to work: 365 * // - It may require correct/in-range values for request initialization. 366 * // - It may require specifying regional endpoints when creating the service client as shown in 367 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 368 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 369 * AsyncBatchAnnotateFilesRequest request = 370 * AsyncBatchAnnotateFilesRequest.newBuilder() 371 * .addAllRequests(new ArrayList<AsyncAnnotateFileRequest>()) 372 * .build(); 373 * OperationFuture<AsyncBatchAnnotateFilesResponse, OperationMetadata> future = 374 * imageAnnotatorClient.asyncBatchAnnotateFilesOperationCallable().futureCall(request); 375 * // Do something. 376 * AsyncBatchAnnotateFilesResponse response = future.get(); 377 * } 378 * }</pre> 379 */ 380 public final OperationCallable< 381 AsyncBatchAnnotateFilesRequest, AsyncBatchAnnotateFilesResponse, OperationMetadata> asyncBatchAnnotateFilesOperationCallable()382 asyncBatchAnnotateFilesOperationCallable() { 383 return stub.asyncBatchAnnotateFilesOperationCallable(); 384 } 385 386 // AUTO-GENERATED DOCUMENTATION AND METHOD. 387 /** 388 * Run async image detection and annotation for a list of generic files (e.g. PDF) which may 389 * contain multiple pages and multiple images per page. Progress and results can be retrieved 390 * through the `google.longrunning.Operations` interface. `Operation.metadata` contains 391 * `OperationMetadata` (metadata). `Operation.response` contains `AsyncBatchAnnotateFilesResponse` 392 * (results). 393 * 394 * <p>Sample code: 395 * 396 * <pre>{@code 397 * // This snippet has been automatically generated and should be regarded as a code template only. 398 * // It will require modifications to work: 399 * // - It may require correct/in-range values for request initialization. 400 * // - It may require specifying regional endpoints when creating the service client as shown in 401 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 402 * try (ImageAnnotatorClient imageAnnotatorClient = ImageAnnotatorClient.create()) { 403 * AsyncBatchAnnotateFilesRequest request = 404 * AsyncBatchAnnotateFilesRequest.newBuilder() 405 * .addAllRequests(new ArrayList<AsyncAnnotateFileRequest>()) 406 * .build(); 407 * ApiFuture<Operation> future = 408 * imageAnnotatorClient.asyncBatchAnnotateFilesCallable().futureCall(request); 409 * // Do something. 410 * Operation response = future.get(); 411 * } 412 * }</pre> 413 */ 414 public final UnaryCallable<AsyncBatchAnnotateFilesRequest, Operation> asyncBatchAnnotateFilesCallable()415 asyncBatchAnnotateFilesCallable() { 416 return stub.asyncBatchAnnotateFilesCallable(); 417 } 418 419 @Override close()420 public final void close() { 421 stub.close(); 422 } 423 424 @Override shutdown()425 public void shutdown() { 426 stub.shutdown(); 427 } 428 429 @Override isShutdown()430 public boolean isShutdown() { 431 return stub.isShutdown(); 432 } 433 434 @Override isTerminated()435 public boolean isTerminated() { 436 return stub.isTerminated(); 437 } 438 439 @Override shutdownNow()440 public void shutdownNow() { 441 stub.shutdownNow(); 442 } 443 444 @Override awaitTermination(long duration, TimeUnit unit)445 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 446 return stub.awaitTermination(duration, unit); 447 } 448 } 449