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.ApiFuture; 20 import com.google.api.core.ApiFutures; 21 import com.google.api.gax.core.BackgroundResource; 22 import com.google.api.gax.paging.AbstractFixedSizeCollection; 23 import com.google.api.gax.paging.AbstractPage; 24 import com.google.api.gax.paging.AbstractPagedListResponse; 25 import com.google.api.gax.rpc.PageContext; 26 import com.google.api.gax.rpc.UnaryCallable; 27 import com.google.cloud.retail.v2.stub.SearchServiceStub; 28 import com.google.cloud.retail.v2.stub.SearchServiceStubSettings; 29 import com.google.common.util.concurrent.MoreExecutors; 30 import java.io.IOException; 31 import java.util.List; 32 import java.util.concurrent.TimeUnit; 33 import javax.annotation.Generated; 34 35 // AUTO-GENERATED DOCUMENTATION AND CLASS. 36 /** 37 * Service Description: Service for search. 38 * 39 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail Search 40 * on Cloud Console before using this feature. 41 * 42 * <p>This class provides the ability to make remote calls to the backing service through method 43 * calls that map to API methods. Sample code to get started: 44 * 45 * <pre>{@code 46 * // This snippet has been automatically generated and should be regarded as a code template only. 47 * // It will require modifications to work: 48 * // - It may require correct/in-range values for request initialization. 49 * // - It may require specifying regional endpoints when creating the service client as shown in 50 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 51 * try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) { 52 * SearchRequest request = 53 * SearchRequest.newBuilder() 54 * .setPlacement("placement1792938725") 55 * .setBranch( 56 * BranchName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[BRANCH]").toString()) 57 * .setQuery("query107944136") 58 * .setVisitorId("visitorId1880545833") 59 * .setUserInfo(UserInfo.newBuilder().build()) 60 * .setPageSize(883849137) 61 * .setPageToken("pageToken873572522") 62 * .setOffset(-1019779949) 63 * .setFilter("filter-1274492040") 64 * .setCanonicalFilter("canonicalFilter-722283124") 65 * .setOrderBy("orderBy-1207110587") 66 * .addAllFacetSpecs(new ArrayList<SearchRequest.FacetSpec>()) 67 * .setDynamicFacetSpec(SearchRequest.DynamicFacetSpec.newBuilder().build()) 68 * .setBoostSpec(SearchRequest.BoostSpec.newBuilder().build()) 69 * .setQueryExpansionSpec(SearchRequest.QueryExpansionSpec.newBuilder().build()) 70 * .addAllVariantRollupKeys(new ArrayList<String>()) 71 * .addAllPageCategories(new ArrayList<String>()) 72 * .setPersonalizationSpec(SearchRequest.PersonalizationSpec.newBuilder().build()) 73 * .putAllLabels(new HashMap<String, String>()) 74 * .setSpellCorrectionSpec(SearchRequest.SpellCorrectionSpec.newBuilder().build()) 75 * .setEntity("entity-1298275357") 76 * .build(); 77 * for (SearchResponse.SearchResult element : searchServiceClient.search(request).iterateAll()) { 78 * // doThingsWith(element); 79 * } 80 * } 81 * }</pre> 82 * 83 * <p>Note: close() needs to be called on the SearchServiceClient object to clean up resources such 84 * as threads. In the example above, try-with-resources is used, which automatically calls close(). 85 * 86 * <p>The surface of this class includes several types of Java methods for each of the API's 87 * methods: 88 * 89 * <ol> 90 * <li>A "flattened" method. With this type of method, the fields of the request type have been 91 * converted into function parameters. It may be the case that not all fields are available as 92 * parameters, and not every API method will have a flattened method entry point. 93 * <li>A "request object" method. This type of method only takes one parameter, a request object, 94 * which must be constructed before the call. Not every API method will have a request object 95 * method. 96 * <li>A "callable" method. This type of method takes no parameters and returns an immutable API 97 * callable object, which can be used to initiate calls to the service. 98 * </ol> 99 * 100 * <p>See the individual methods for example code. 101 * 102 * <p>Many parameters require resource names to be formatted in a particular way. To assist with 103 * these names, this class includes a format method for each type of name, and additionally a parse 104 * method to extract the individual identifiers contained within names that are returned. 105 * 106 * <p>This class can be customized by passing in a custom instance of SearchServiceSettings to 107 * create(). For example: 108 * 109 * <p>To customize credentials: 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 * SearchServiceSettings searchServiceSettings = 118 * SearchServiceSettings.newBuilder() 119 * .setCredentialsProvider(FixedCredentialsProvider.create(myCredentials)) 120 * .build(); 121 * SearchServiceClient searchServiceClient = SearchServiceClient.create(searchServiceSettings); 122 * }</pre> 123 * 124 * <p>To customize the endpoint: 125 * 126 * <pre>{@code 127 * // This snippet has been automatically generated and should be regarded as a code template only. 128 * // It will require modifications to work: 129 * // - It may require correct/in-range values for request initialization. 130 * // - It may require specifying regional endpoints when creating the service client as shown in 131 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 132 * SearchServiceSettings searchServiceSettings = 133 * SearchServiceSettings.newBuilder().setEndpoint(myEndpoint).build(); 134 * SearchServiceClient searchServiceClient = SearchServiceClient.create(searchServiceSettings); 135 * }</pre> 136 * 137 * <p>To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over 138 * the wire: 139 * 140 * <pre>{@code 141 * // This snippet has been automatically generated and should be regarded as a code template only. 142 * // It will require modifications to work: 143 * // - It may require correct/in-range values for request initialization. 144 * // - It may require specifying regional endpoints when creating the service client as shown in 145 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 146 * SearchServiceSettings searchServiceSettings = 147 * SearchServiceSettings.newHttpJsonBuilder().build(); 148 * SearchServiceClient searchServiceClient = SearchServiceClient.create(searchServiceSettings); 149 * }</pre> 150 * 151 * <p>Please refer to the GitHub repository's samples for more quickstart code snippets. 152 */ 153 @Generated("by gapic-generator-java") 154 public class SearchServiceClient implements BackgroundResource { 155 private final SearchServiceSettings settings; 156 private final SearchServiceStub stub; 157 158 /** Constructs an instance of SearchServiceClient with default settings. */ create()159 public static final SearchServiceClient create() throws IOException { 160 return create(SearchServiceSettings.newBuilder().build()); 161 } 162 163 /** 164 * Constructs an instance of SearchServiceClient, using the given settings. The channels are 165 * created based on the settings passed in, or defaults for any settings that are not set. 166 */ create(SearchServiceSettings settings)167 public static final SearchServiceClient create(SearchServiceSettings settings) 168 throws IOException { 169 return new SearchServiceClient(settings); 170 } 171 172 /** 173 * Constructs an instance of SearchServiceClient, using the given stub for making calls. This is 174 * for advanced usage - prefer using create(SearchServiceSettings). 175 */ create(SearchServiceStub stub)176 public static final SearchServiceClient create(SearchServiceStub stub) { 177 return new SearchServiceClient(stub); 178 } 179 180 /** 181 * Constructs an instance of SearchServiceClient, using the given settings. This is protected so 182 * that it is easy to make a subclass, but otherwise, the static factory methods should be 183 * preferred. 184 */ SearchServiceClient(SearchServiceSettings settings)185 protected SearchServiceClient(SearchServiceSettings settings) throws IOException { 186 this.settings = settings; 187 this.stub = ((SearchServiceStubSettings) settings.getStubSettings()).createStub(); 188 } 189 SearchServiceClient(SearchServiceStub stub)190 protected SearchServiceClient(SearchServiceStub stub) { 191 this.settings = null; 192 this.stub = stub; 193 } 194 getSettings()195 public final SearchServiceSettings getSettings() { 196 return settings; 197 } 198 getStub()199 public SearchServiceStub getStub() { 200 return stub; 201 } 202 203 // AUTO-GENERATED DOCUMENTATION AND METHOD. 204 /** 205 * Performs a search. 206 * 207 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 208 * Search on Cloud Console before using this feature. 209 * 210 * <p>Sample code: 211 * 212 * <pre>{@code 213 * // This snippet has been automatically generated and should be regarded as a code template only. 214 * // It will require modifications to work: 215 * // - It may require correct/in-range values for request initialization. 216 * // - It may require specifying regional endpoints when creating the service client as shown in 217 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 218 * try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) { 219 * SearchRequest request = 220 * SearchRequest.newBuilder() 221 * .setPlacement("placement1792938725") 222 * .setBranch( 223 * BranchName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[BRANCH]").toString()) 224 * .setQuery("query107944136") 225 * .setVisitorId("visitorId1880545833") 226 * .setUserInfo(UserInfo.newBuilder().build()) 227 * .setPageSize(883849137) 228 * .setPageToken("pageToken873572522") 229 * .setOffset(-1019779949) 230 * .setFilter("filter-1274492040") 231 * .setCanonicalFilter("canonicalFilter-722283124") 232 * .setOrderBy("orderBy-1207110587") 233 * .addAllFacetSpecs(new ArrayList<SearchRequest.FacetSpec>()) 234 * .setDynamicFacetSpec(SearchRequest.DynamicFacetSpec.newBuilder().build()) 235 * .setBoostSpec(SearchRequest.BoostSpec.newBuilder().build()) 236 * .setQueryExpansionSpec(SearchRequest.QueryExpansionSpec.newBuilder().build()) 237 * .addAllVariantRollupKeys(new ArrayList<String>()) 238 * .addAllPageCategories(new ArrayList<String>()) 239 * .setPersonalizationSpec(SearchRequest.PersonalizationSpec.newBuilder().build()) 240 * .putAllLabels(new HashMap<String, String>()) 241 * .setSpellCorrectionSpec(SearchRequest.SpellCorrectionSpec.newBuilder().build()) 242 * .setEntity("entity-1298275357") 243 * .build(); 244 * for (SearchResponse.SearchResult element : searchServiceClient.search(request).iterateAll()) { 245 * // doThingsWith(element); 246 * } 247 * } 248 * }</pre> 249 * 250 * @param request The request object containing all of the parameters for the API call. 251 * @throws com.google.api.gax.rpc.ApiException if the remote call fails 252 */ search(SearchRequest request)253 public final SearchPagedResponse search(SearchRequest request) { 254 return searchPagedCallable().call(request); 255 } 256 257 // AUTO-GENERATED DOCUMENTATION AND METHOD. 258 /** 259 * Performs a search. 260 * 261 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 262 * Search on Cloud Console before using this feature. 263 * 264 * <p>Sample code: 265 * 266 * <pre>{@code 267 * // This snippet has been automatically generated and should be regarded as a code template only. 268 * // It will require modifications to work: 269 * // - It may require correct/in-range values for request initialization. 270 * // - It may require specifying regional endpoints when creating the service client as shown in 271 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 272 * try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) { 273 * SearchRequest request = 274 * SearchRequest.newBuilder() 275 * .setPlacement("placement1792938725") 276 * .setBranch( 277 * BranchName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[BRANCH]").toString()) 278 * .setQuery("query107944136") 279 * .setVisitorId("visitorId1880545833") 280 * .setUserInfo(UserInfo.newBuilder().build()) 281 * .setPageSize(883849137) 282 * .setPageToken("pageToken873572522") 283 * .setOffset(-1019779949) 284 * .setFilter("filter-1274492040") 285 * .setCanonicalFilter("canonicalFilter-722283124") 286 * .setOrderBy("orderBy-1207110587") 287 * .addAllFacetSpecs(new ArrayList<SearchRequest.FacetSpec>()) 288 * .setDynamicFacetSpec(SearchRequest.DynamicFacetSpec.newBuilder().build()) 289 * .setBoostSpec(SearchRequest.BoostSpec.newBuilder().build()) 290 * .setQueryExpansionSpec(SearchRequest.QueryExpansionSpec.newBuilder().build()) 291 * .addAllVariantRollupKeys(new ArrayList<String>()) 292 * .addAllPageCategories(new ArrayList<String>()) 293 * .setPersonalizationSpec(SearchRequest.PersonalizationSpec.newBuilder().build()) 294 * .putAllLabels(new HashMap<String, String>()) 295 * .setSpellCorrectionSpec(SearchRequest.SpellCorrectionSpec.newBuilder().build()) 296 * .setEntity("entity-1298275357") 297 * .build(); 298 * ApiFuture<SearchResponse.SearchResult> future = 299 * searchServiceClient.searchPagedCallable().futureCall(request); 300 * // Do something. 301 * for (SearchResponse.SearchResult element : future.get().iterateAll()) { 302 * // doThingsWith(element); 303 * } 304 * } 305 * }</pre> 306 */ searchPagedCallable()307 public final UnaryCallable<SearchRequest, SearchPagedResponse> searchPagedCallable() { 308 return stub.searchPagedCallable(); 309 } 310 311 // AUTO-GENERATED DOCUMENTATION AND METHOD. 312 /** 313 * Performs a search. 314 * 315 * <p>This feature is only available for users who have Retail Search enabled. Enable Retail 316 * Search on Cloud Console before using this feature. 317 * 318 * <p>Sample code: 319 * 320 * <pre>{@code 321 * // This snippet has been automatically generated and should be regarded as a code template only. 322 * // It will require modifications to work: 323 * // - It may require correct/in-range values for request initialization. 324 * // - It may require specifying regional endpoints when creating the service client as shown in 325 * // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library 326 * try (SearchServiceClient searchServiceClient = SearchServiceClient.create()) { 327 * SearchRequest request = 328 * SearchRequest.newBuilder() 329 * .setPlacement("placement1792938725") 330 * .setBranch( 331 * BranchName.of("[PROJECT]", "[LOCATION]", "[CATALOG]", "[BRANCH]").toString()) 332 * .setQuery("query107944136") 333 * .setVisitorId("visitorId1880545833") 334 * .setUserInfo(UserInfo.newBuilder().build()) 335 * .setPageSize(883849137) 336 * .setPageToken("pageToken873572522") 337 * .setOffset(-1019779949) 338 * .setFilter("filter-1274492040") 339 * .setCanonicalFilter("canonicalFilter-722283124") 340 * .setOrderBy("orderBy-1207110587") 341 * .addAllFacetSpecs(new ArrayList<SearchRequest.FacetSpec>()) 342 * .setDynamicFacetSpec(SearchRequest.DynamicFacetSpec.newBuilder().build()) 343 * .setBoostSpec(SearchRequest.BoostSpec.newBuilder().build()) 344 * .setQueryExpansionSpec(SearchRequest.QueryExpansionSpec.newBuilder().build()) 345 * .addAllVariantRollupKeys(new ArrayList<String>()) 346 * .addAllPageCategories(new ArrayList<String>()) 347 * .setPersonalizationSpec(SearchRequest.PersonalizationSpec.newBuilder().build()) 348 * .putAllLabels(new HashMap<String, String>()) 349 * .setSpellCorrectionSpec(SearchRequest.SpellCorrectionSpec.newBuilder().build()) 350 * .setEntity("entity-1298275357") 351 * .build(); 352 * while (true) { 353 * SearchResponse response = searchServiceClient.searchCallable().call(request); 354 * for (SearchResponse.SearchResult element : response.getResultsList()) { 355 * // doThingsWith(element); 356 * } 357 * String nextPageToken = response.getNextPageToken(); 358 * if (!Strings.isNullOrEmpty(nextPageToken)) { 359 * request = request.toBuilder().setPageToken(nextPageToken).build(); 360 * } else { 361 * break; 362 * } 363 * } 364 * } 365 * }</pre> 366 */ searchCallable()367 public final UnaryCallable<SearchRequest, SearchResponse> searchCallable() { 368 return stub.searchCallable(); 369 } 370 371 @Override close()372 public final void close() { 373 stub.close(); 374 } 375 376 @Override shutdown()377 public void shutdown() { 378 stub.shutdown(); 379 } 380 381 @Override isShutdown()382 public boolean isShutdown() { 383 return stub.isShutdown(); 384 } 385 386 @Override isTerminated()387 public boolean isTerminated() { 388 return stub.isTerminated(); 389 } 390 391 @Override shutdownNow()392 public void shutdownNow() { 393 stub.shutdownNow(); 394 } 395 396 @Override awaitTermination(long duration, TimeUnit unit)397 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 398 return stub.awaitTermination(duration, unit); 399 } 400 401 public static class SearchPagedResponse 402 extends AbstractPagedListResponse< 403 SearchRequest, 404 SearchResponse, 405 SearchResponse.SearchResult, 406 SearchPage, 407 SearchFixedSizeCollection> { 408 createAsync( PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, ApiFuture<SearchResponse> futureResponse)409 public static ApiFuture<SearchPagedResponse> createAsync( 410 PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, 411 ApiFuture<SearchResponse> futureResponse) { 412 ApiFuture<SearchPage> futurePage = 413 SearchPage.createEmptyPage().createPageAsync(context, futureResponse); 414 return ApiFutures.transform( 415 futurePage, input -> new SearchPagedResponse(input), MoreExecutors.directExecutor()); 416 } 417 SearchPagedResponse(SearchPage page)418 private SearchPagedResponse(SearchPage page) { 419 super(page, SearchFixedSizeCollection.createEmptyCollection()); 420 } 421 } 422 423 public static class SearchPage 424 extends AbstractPage<SearchRequest, SearchResponse, SearchResponse.SearchResult, SearchPage> { 425 SearchPage( PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, SearchResponse response)426 private SearchPage( 427 PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, 428 SearchResponse response) { 429 super(context, response); 430 } 431 createEmptyPage()432 private static SearchPage createEmptyPage() { 433 return new SearchPage(null, null); 434 } 435 436 @Override createPage( PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, SearchResponse response)437 protected SearchPage createPage( 438 PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, 439 SearchResponse response) { 440 return new SearchPage(context, response); 441 } 442 443 @Override createPageAsync( PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, ApiFuture<SearchResponse> futureResponse)444 public ApiFuture<SearchPage> createPageAsync( 445 PageContext<SearchRequest, SearchResponse, SearchResponse.SearchResult> context, 446 ApiFuture<SearchResponse> futureResponse) { 447 return super.createPageAsync(context, futureResponse); 448 } 449 } 450 451 public static class SearchFixedSizeCollection 452 extends AbstractFixedSizeCollection< 453 SearchRequest, 454 SearchResponse, 455 SearchResponse.SearchResult, 456 SearchPage, 457 SearchFixedSizeCollection> { 458 SearchFixedSizeCollection(List<SearchPage> pages, int collectionSize)459 private SearchFixedSizeCollection(List<SearchPage> pages, int collectionSize) { 460 super(pages, collectionSize); 461 } 462 createEmptyCollection()463 private static SearchFixedSizeCollection createEmptyCollection() { 464 return new SearchFixedSizeCollection(null, 0); 465 } 466 467 @Override createCollection( List<SearchPage> pages, int collectionSize)468 protected SearchFixedSizeCollection createCollection( 469 List<SearchPage> pages, int collectionSize) { 470 return new SearchFixedSizeCollection(pages, collectionSize); 471 } 472 } 473 } 474