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; 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.retail.v2.stub.CompletionServiceStub; 26 import com.google.cloud.retail.v2.stub.CompletionServiceStubSettings; 27 import com.google.longrunning.Operation; 28 import java.io.IOException; 29 import java.util.concurrent.TimeUnit; 30 import javax.annotation.Generated; 31 32 // AUTO-GENERATED DOCUMENTATION AND CLASS. 33 /** 34 * Service Description: Autocomplete service for retail. 35 * 36 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail Search 37 * on Cloud Console before using this feature. 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 (CompletionServiceClient completionServiceClient = CompletionServiceClient.create()) { 49 * CompleteQueryRequest request = 50 * CompleteQueryRequest.newBuilder() 51 * .setCatalog(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString()) 52 * .setQuery("query107944136") 53 * .setVisitorId("visitorId1880545833") 54 * .addAllLanguageCodes(new ArrayList<String>()) 55 * .setDeviceType("deviceType781190832") 56 * .setDataset("dataset1443214456") 57 * .setMaxSuggestions(618824852) 58 * .setEntity("entity-1298275357") 59 * .build(); 60 * CompleteQueryResponse response = completionServiceClient.completeQuery(request); 61 * } 62 * }</pre> 63 * 64 * <p>Note: close() needs to be called on the CompletionServiceClient object to clean up resources 65 * such as threads. In the example above, try-with-resources is used, which automatically calls 66 * close(). 67 * 68 * <p>The surface of this class includes several types of Java methods for each of the API's 69 * methods: 70 * 71 * <ol> 72 * <li>A "flattened" method. With this type of method, the fields of the request type have been 73 * converted into function parameters. It may be the case that not all fields are available as 74 * parameters, and not every API method will have a flattened method entry point. 75 * <li>A "request object" method. This type of method only takes one parameter, a request object, 76 * which must be constructed before the call. Not every API method will have a request object 77 * method. 78 * <li>A "callable" method. This type of method takes no parameters and returns an immutable API 79 * callable object, which can be used to initiate calls to the service. 80 * </ol> 81 * 82 * <p>See the individual methods for example code. 83 * 84 * <p>Many parameters require resource names to be formatted in a particular way. To assist with 85 * these names, this class includes a format method for each type of name, and additionally a parse 86 * method to extract the individual identifiers contained within names that are returned. 87 * 88 * <p>This class can be customized by passing in a custom instance of CompletionServiceSettings to 89 * create(). For example: 90 * 91 * <p>To customize credentials: 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 * CompletionServiceSettings completionServiceSettings = 100 * CompletionServiceSettings.newBuilder() 101 * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) 102 * .build(); 103 * CompletionServiceClient completionServiceClient = 104 * CompletionServiceClient.create(completionServiceSettings); 105 * }</pre> 106 * 107 * <p>To customize the endpoint: 108 * 109 * <pre>{@code 110 * // This snippet has been automatically generated and should be regarded as a code template only. 111 * // It will require modifications to work: 112 * // - It may require correct/in-range values for request initialization. 113 * // - It may require specifying regional endpoints when creating the service client as shown in 114 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 115 * CompletionServiceSettings completionServiceSettings = 116 * CompletionServiceSettings.newBuilder().setEndpoint(myEndpoint).build(); 117 * CompletionServiceClient completionServiceClient = 118 * CompletionServiceClient.create(completionServiceSettings); 119 * }</pre> 120 * 121 * <p>To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over 122 * the wire: 123 * 124 * <pre>{@code 125 * // This snippet has been automatically generated and should be regarded as a code template only. 126 * // It will require modifications to work: 127 * // - It may require correct/in-range values for request initialization. 128 * // - It may require specifying regional endpoints when creating the service client as shown in 129 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 130 * CompletionServiceSettings completionServiceSettings = 131 * CompletionServiceSettings.newHttpJsonBuilder().build(); 132 * CompletionServiceClient completionServiceClient = 133 * CompletionServiceClient.create(completionServiceSettings); 134 * }</pre> 135 * 136 * <p>Please refer to the GitHub repository's samples for more quickstart code snippets. 137 */ 138 @Generated("by gapic-generator-java") 139 public class CompletionServiceClient implements BackgroundResource { 140 private final CompletionServiceSettings settings; 141 private final CompletionServiceStub stub; 142 private final OperationsClient httpJsonOperationsClient; 143 private final com.google.longrunning.OperationsClient operationsClient; 144 145 /** Constructs an instance of CompletionServiceClient with default settings. */ create()146 public static final CompletionServiceClient create() throws IOException { 147 return create(CompletionServiceSettings.newBuilder().build()); 148 } 149 150 /** 151 * Constructs an instance of CompletionServiceClient, using the given settings. The channels are 152 * created based on the settings passed in, or defaults for any settings that are not set. 153 */ create(CompletionServiceSettings settings)154 public static final CompletionServiceClient create(CompletionServiceSettings settings) 155 throws IOException { 156 return new CompletionServiceClient(settings); 157 } 158 159 /** 160 * Constructs an instance of CompletionServiceClient, using the given stub for making calls. This 161 * is for advanced usage - prefer using create(CompletionServiceSettings). 162 */ create(CompletionServiceStub stub)163 public static final CompletionServiceClient create(CompletionServiceStub stub) { 164 return new CompletionServiceClient(stub); 165 } 166 167 /** 168 * Constructs an instance of CompletionServiceClient, using the given settings. This is protected 169 * so that it is easy to make a subclass, but otherwise, the static factory methods should be 170 * preferred. 171 */ CompletionServiceClient(CompletionServiceSettings settings)172 protected CompletionServiceClient(CompletionServiceSettings settings) throws IOException { 173 this.settings = settings; 174 this.stub = ((CompletionServiceStubSettings) settings.getStubSettings()).createStub(); 175 this.operationsClient = 176 com.google.longrunning.OperationsClient.create(this.stub.getOperationsStub()); 177 this.httpJsonOperationsClient = OperationsClient.create(this.stub.getHttpJsonOperationsStub()); 178 } 179 CompletionServiceClient(CompletionServiceStub stub)180 protected CompletionServiceClient(CompletionServiceStub stub) { 181 this.settings = null; 182 this.stub = stub; 183 this.operationsClient = 184 com.google.longrunning.OperationsClient.create(this.stub.getOperationsStub()); 185 this.httpJsonOperationsClient = OperationsClient.create(this.stub.getHttpJsonOperationsStub()); 186 } 187 getSettings()188 public final CompletionServiceSettings getSettings() { 189 return settings; 190 } 191 getStub()192 public CompletionServiceStub getStub() { 193 return stub; 194 } 195 196 /** 197 * Returns the OperationsClient that can be used to query the status of a long-running operation 198 * returned by another API method call. 199 */ getOperationsClient()200 public final com.google.longrunning.OperationsClient getOperationsClient() { 201 return operationsClient; 202 } 203 204 /** 205 * Returns the OperationsClient that can be used to query the status of a long-running operation 206 * returned by another API method call. 207 */ 208 @BetaApi getHttpJsonOperationsClient()209 public final OperationsClient getHttpJsonOperationsClient() { 210 return httpJsonOperationsClient; 211 } 212 213 // AUTO-GENERATED DOCUMENTATION AND METHOD. 214 /** 215 * Completes the specified prefix with keyword suggestions. 216 * 217 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 218 * Search on Cloud Console before using this feature. 219 * 220 * <p>Sample code: 221 * 222 * <pre>{@code 223 * // This snippet has been automatically generated and should be regarded as a code template only. 224 * // It will require modifications to work: 225 * // - It may require correct/in-range values for request initialization. 226 * // - It may require specifying regional endpoints when creating the service client as shown in 227 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 228 * try (CompletionServiceClient completionServiceClient = CompletionServiceClient.create()) { 229 * CompleteQueryRequest request = 230 * CompleteQueryRequest.newBuilder() 231 * .setCatalog(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString()) 232 * .setQuery("query107944136") 233 * .setVisitorId("visitorId1880545833") 234 * .addAllLanguageCodes(new ArrayList<String>()) 235 * .setDeviceType("deviceType781190832") 236 * .setDataset("dataset1443214456") 237 * .setMaxSuggestions(618824852) 238 * .setEntity("entity-1298275357") 239 * .build(); 240 * CompleteQueryResponse response = completionServiceClient.completeQuery(request); 241 * } 242 * }</pre> 243 * 244 * @param request The request object containing all of the parameters for the API call. 245 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 246 */ completeQuery(CompleteQueryRequest request)247 public final CompleteQueryResponse completeQuery(CompleteQueryRequest request) { 248 return completeQueryCallable().call(request); 249 } 250 251 // AUTO-GENERATED DOCUMENTATION AND METHOD. 252 /** 253 * Completes the specified prefix with keyword suggestions. 254 * 255 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 256 * Search on Cloud Console before using this feature. 257 * 258 * <p>Sample code: 259 * 260 * <pre>{@code 261 * // This snippet has been automatically generated and should be regarded as a code template only. 262 * // It will require modifications to work: 263 * // - It may require correct/in-range values for request initialization. 264 * // - It may require specifying regional endpoints when creating the service client as shown in 265 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 266 * try (CompletionServiceClient completionServiceClient = CompletionServiceClient.create()) { 267 * CompleteQueryRequest request = 268 * CompleteQueryRequest.newBuilder() 269 * .setCatalog(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString()) 270 * .setQuery("query107944136") 271 * .setVisitorId("visitorId1880545833") 272 * .addAllLanguageCodes(new ArrayList<String>()) 273 * .setDeviceType("deviceType781190832") 274 * .setDataset("dataset1443214456") 275 * .setMaxSuggestions(618824852) 276 * .setEntity("entity-1298275357") 277 * .build(); 278 * ApiFuture<CompleteQueryResponse> future = 279 * completionServiceClient.completeQueryCallable().futureCall(request); 280 * // Do something. 281 * CompleteQueryResponse response = future.get(); 282 * } 283 * }</pre> 284 */ completeQueryCallable()285 public final UnaryCallable<CompleteQueryRequest, CompleteQueryResponse> completeQueryCallable() { 286 return stub.completeQueryCallable(); 287 } 288 289 // AUTO-GENERATED DOCUMENTATION AND METHOD. 290 /** 291 * Bulk import of processed completion dataset. 292 * 293 * <p>Request processing is asynchronous. Partial updating is not supported. 294 * 295 * <p>The operation is successfully finished only after the imported suggestions are indexed 296 * successfully and ready for serving. The process takes hours. 297 * 298 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 299 * Search on Cloud Console before using this feature. 300 * 301 * <p>Sample code: 302 * 303 * <pre>{@code 304 * // This snippet has been automatically generated and should be regarded as a code template only. 305 * // It will require modifications to work: 306 * // - It may require correct/in-range values for request initialization. 307 * // - It may require specifying regional endpoints when creating the service client as shown in 308 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 309 * try (CompletionServiceClient completionServiceClient = CompletionServiceClient.create()) { 310 * ImportCompletionDataRequest request = 311 * ImportCompletionDataRequest.newBuilder() 312 * .setParent(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString()) 313 * .setInputConfig(CompletionDataInputConfig.newBuilder().build()) 314 * .setNotificationPubsubTopic("notificationPubsubTopic-1361224991") 315 * .build(); 316 * ImportCompletionDataResponse response = 317 * completionServiceClient.importCompletionDataAsync(request).get(); 318 * } 319 * }</pre> 320 * 321 * @param request The request object containing all of the parameters for the API call. 322 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 323 */ 324 public final OperationFuture<ImportCompletionDataResponse, ImportMetadata> importCompletionDataAsync(ImportCompletionDataRequest request)325 importCompletionDataAsync(ImportCompletionDataRequest request) { 326 return importCompletionDataOperationCallable().futureCall(request); 327 } 328 329 // AUTO-GENERATED DOCUMENTATION AND METHOD. 330 /** 331 * Bulk import of processed completion dataset. 332 * 333 * <p>Request processing is asynchronous. Partial updating is not supported. 334 * 335 * <p>The operation is successfully finished only after the imported suggestions are indexed 336 * successfully and ready for serving. The process takes hours. 337 * 338 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 339 * Search on Cloud Console before using this feature. 340 * 341 * <p>Sample code: 342 * 343 * <pre>{@code 344 * // This snippet has been automatically generated and should be regarded as a code template only. 345 * // It will require modifications to work: 346 * // - It may require correct/in-range values for request initialization. 347 * // - It may require specifying regional endpoints when creating the service client as shown in 348 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 349 * try (CompletionServiceClient completionServiceClient = CompletionServiceClient.create()) { 350 * ImportCompletionDataRequest request = 351 * ImportCompletionDataRequest.newBuilder() 352 * .setParent(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString()) 353 * .setInputConfig(CompletionDataInputConfig.newBuilder().build()) 354 * .setNotificationPubsubTopic("notificationPubsubTopic-1361224991") 355 * .build(); 356 * OperationFuture<ImportCompletionDataResponse, ImportMetadata> future = 357 * completionServiceClient.importCompletionDataOperationCallable().futureCall(request); 358 * // Do something. 359 * ImportCompletionDataResponse response = future.get(); 360 * } 361 * }</pre> 362 */ 363 public final OperationCallable< 364 ImportCompletionDataRequest, ImportCompletionDataResponse, ImportMetadata> importCompletionDataOperationCallable()365 importCompletionDataOperationCallable() { 366 return stub.importCompletionDataOperationCallable(); 367 } 368 369 // AUTO-GENERATED DOCUMENTATION AND METHOD. 370 /** 371 * Bulk import of processed completion dataset. 372 * 373 * <p>Request processing is asynchronous. Partial updating is not supported. 374 * 375 * <p>The operation is successfully finished only after the imported suggestions are indexed 376 * successfully and ready for serving. The process takes hours. 377 * 378 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 379 * Search on Cloud Console before using this feature. 380 * 381 * <p>Sample code: 382 * 383 * <pre>{@code 384 * // This snippet has been automatically generated and should be regarded as a code template only. 385 * // It will require modifications to work: 386 * // - It may require correct/in-range values for request initialization. 387 * // - It may require specifying regional endpoints when creating the service client as shown in 388 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 389 * try (CompletionServiceClient completionServiceClient = CompletionServiceClient.create()) { 390 * ImportCompletionDataRequest request = 391 * ImportCompletionDataRequest.newBuilder() 392 * .setParent(CatalogName.of("[PROJECT]", "[LOCATION]", "[CATALOG]").toString()) 393 * .setInputConfig(CompletionDataInputConfig.newBuilder().build()) 394 * .setNotificationPubsubTopic("notificationPubsubTopic-1361224991") 395 * .build(); 396 * ApiFuture<Operation> future = 397 * completionServiceClient.importCompletionDataCallable().futureCall(request); 398 * // Do something. 399 * Operation response = future.get(); 400 * } 401 * }</pre> 402 */ 403 public final UnaryCallable<ImportCompletionDataRequest, Operation> importCompletionDataCallable()404 importCompletionDataCallable() { 405 return stub.importCompletionDataCallable(); 406 } 407 408 @Override close()409 public final void close() { 410 stub.close(); 411 } 412 413 @Override shutdown()414 public void shutdown() { 415 stub.shutdown(); 416 } 417 418 @Override isShutdown()419 public boolean isShutdown() { 420 return stub.isShutdown(); 421 } 422 423 @Override isTerminated()424 public boolean isTerminated() { 425 return stub.isTerminated(); 426 } 427 428 @Override shutdownNow()429 public void shutdownNow() { 430 stub.shutdownNow(); 431 } 432 433 @Override awaitTermination(long duration, TimeUnit unit)434 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 435 return stub.awaitTermination(duration, unit); 436 } 437 } 438