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