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.stub; 18 19 import static com.google.cloud.retail.v2.SearchServiceClient.SearchPagedResponse; 20 21 import com.google.api.gax.core.BackgroundResource; 22 import com.google.api.gax.core.BackgroundResourceAggregation; 23 import com.google.api.gax.grpc.GrpcCallSettings; 24 import com.google.api.gax.grpc.GrpcStubCallableFactory; 25 import com.google.api.gax.rpc.ClientContext; 26 import com.google.api.gax.rpc.UnaryCallable; 27 import com.google.cloud.retail.v2.SearchRequest; 28 import com.google.cloud.retail.v2.SearchResponse; 29 import com.google.common.collect.ImmutableMap; 30 import com.google.longrunning.stub.GrpcOperationsStub; 31 import io.grpc.MethodDescriptor; 32 import io.grpc.protobuf.ProtoUtils; 33 import java.io.IOException; 34 import java.util.concurrent.TimeUnit; 35 import javax.annotation.Generated; 36 37 // AUTO-GENERATED DOCUMENTATION AND CLASS. 38 /** 39 * gRPC stub implementation for the SearchService service API. 40 * 41 * <p>This class is for advanced usage and reflects the underlying API directly. 42 */ 43 @Generated("by gapic-generator-java") 44 public class GrpcSearchServiceStub extends SearchServiceStub { 45 private static final MethodDescriptor<SearchRequest, SearchResponse> searchMethodDescriptor = 46 MethodDescriptor.<SearchRequest, SearchResponse>newBuilder() 47 .setType(MethodDescriptor.MethodType.UNARY) 48 .setFullMethodName("google.cloud.retail.v2.SearchService/Search") 49 .setRequestMarshaller(ProtoUtils.marshaller(SearchRequest.getDefaultInstance())) 50 .setResponseMarshaller(ProtoUtils.marshaller(SearchResponse.getDefaultInstance())) 51 .build(); 52 53 private final UnaryCallable<SearchRequest, SearchResponse> searchCallable; 54 private final UnaryCallable<SearchRequest, SearchPagedResponse> searchPagedCallable; 55 56 private final BackgroundResource backgroundResources; 57 private final GrpcOperationsStub operationsStub; 58 private final GrpcStubCallableFactory callableFactory; 59 create(SearchServiceStubSettings settings)60 public static final GrpcSearchServiceStub create(SearchServiceStubSettings settings) 61 throws IOException { 62 return new GrpcSearchServiceStub(settings, ClientContext.create(settings)); 63 } 64 create(ClientContext clientContext)65 public static final GrpcSearchServiceStub create(ClientContext clientContext) throws IOException { 66 return new GrpcSearchServiceStub(SearchServiceStubSettings.newBuilder().build(), clientContext); 67 } 68 create( ClientContext clientContext, GrpcStubCallableFactory callableFactory)69 public static final GrpcSearchServiceStub create( 70 ClientContext clientContext, GrpcStubCallableFactory callableFactory) throws IOException { 71 return new GrpcSearchServiceStub( 72 SearchServiceStubSettings.newBuilder().build(), clientContext, callableFactory); 73 } 74 75 /** 76 * Constructs an instance of GrpcSearchServiceStub, using the given settings. This is protected so 77 * that it is easy to make a subclass, but otherwise, the static factory methods should be 78 * preferred. 79 */ GrpcSearchServiceStub(SearchServiceStubSettings settings, ClientContext clientContext)80 protected GrpcSearchServiceStub(SearchServiceStubSettings settings, ClientContext clientContext) 81 throws IOException { 82 this(settings, clientContext, new GrpcSearchServiceCallableFactory()); 83 } 84 85 /** 86 * Constructs an instance of GrpcSearchServiceStub, using the given settings. This is protected so 87 * that it is easy to make a subclass, but otherwise, the static factory methods should be 88 * preferred. 89 */ GrpcSearchServiceStub( SearchServiceStubSettings settings, ClientContext clientContext, GrpcStubCallableFactory callableFactory)90 protected GrpcSearchServiceStub( 91 SearchServiceStubSettings settings, 92 ClientContext clientContext, 93 GrpcStubCallableFactory callableFactory) 94 throws IOException { 95 this.callableFactory = callableFactory; 96 this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory); 97 98 GrpcCallSettings<SearchRequest, SearchResponse> searchTransportSettings = 99 GrpcCallSettings.<SearchRequest, SearchResponse>newBuilder() 100 .setMethodDescriptor(searchMethodDescriptor) 101 .setParamsExtractor( 102 request -> { 103 ImmutableMap.Builder<String, String> params = ImmutableMap.builder(); 104 params.put("placement", String.valueOf(request.getPlacement())); 105 return params.build(); 106 }) 107 .build(); 108 109 this.searchCallable = 110 callableFactory.createUnaryCallable( 111 searchTransportSettings, settings.searchSettings(), clientContext); 112 this.searchPagedCallable = 113 callableFactory.createPagedCallable( 114 searchTransportSettings, settings.searchSettings(), clientContext); 115 116 this.backgroundResources = 117 new BackgroundResourceAggregation(clientContext.getBackgroundResources()); 118 } 119 getOperationsStub()120 public GrpcOperationsStub getOperationsStub() { 121 return operationsStub; 122 } 123 124 @Override searchCallable()125 public UnaryCallable<SearchRequest, SearchResponse> searchCallable() { 126 return searchCallable; 127 } 128 129 @Override searchPagedCallable()130 public UnaryCallable<SearchRequest, SearchPagedResponse> searchPagedCallable() { 131 return searchPagedCallable; 132 } 133 134 @Override close()135 public final void close() { 136 try { 137 backgroundResources.close(); 138 } catch (RuntimeException e) { 139 throw e; 140 } catch (Exception e) { 141 throw new IllegalStateException("Failed to close resource", e); 142 } 143 } 144 145 @Override shutdown()146 public void shutdown() { 147 backgroundResources.shutdown(); 148 } 149 150 @Override isShutdown()151 public boolean isShutdown() { 152 return backgroundResources.isShutdown(); 153 } 154 155 @Override isTerminated()156 public boolean isTerminated() { 157 return backgroundResources.isTerminated(); 158 } 159 160 @Override shutdownNow()161 public void shutdownNow() { 162 backgroundResources.shutdownNow(); 163 } 164 165 @Override awaitTermination(long duration, TimeUnit unit)166 public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { 167 return backgroundResources.awaitTermination(duration, unit); 168 } 169 } 170