• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.compute.v1.stub;
18 
19 import static com.google.cloud.compute.v1.ZonesClient.ListPagedResponse;
20 
21 import com.google.api.core.BetaApi;
22 import com.google.api.core.InternalApi;
23 import com.google.api.gax.core.BackgroundResource;
24 import com.google.api.gax.core.BackgroundResourceAggregation;
25 import com.google.api.gax.httpjson.ApiMethodDescriptor;
26 import com.google.api.gax.httpjson.HttpJsonCallSettings;
27 import com.google.api.gax.httpjson.HttpJsonStubCallableFactory;
28 import com.google.api.gax.httpjson.ProtoMessageRequestFormatter;
29 import com.google.api.gax.httpjson.ProtoMessageResponseParser;
30 import com.google.api.gax.httpjson.ProtoRestSerializer;
31 import com.google.api.gax.rpc.ClientContext;
32 import com.google.api.gax.rpc.UnaryCallable;
33 import com.google.cloud.compute.v1.GetZoneRequest;
34 import com.google.cloud.compute.v1.ListZonesRequest;
35 import com.google.cloud.compute.v1.Zone;
36 import com.google.cloud.compute.v1.ZoneList;
37 import com.google.protobuf.TypeRegistry;
38 import java.io.IOException;
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.concurrent.TimeUnit;
44 import javax.annotation.Generated;
45 
46 // AUTO-GENERATED DOCUMENTATION AND CLASS.
47 /**
48  * REST stub implementation for the Zones service API.
49  *
50  * <p>This class is for advanced usage and reflects the underlying API directly.
51  */
52 @Generated("by gapic-generator-java")
53 @BetaApi
54 public class HttpJsonZonesStub extends ZonesStub {
55   private static final TypeRegistry typeRegistry = TypeRegistry.newBuilder().build();
56 
57   private static final ApiMethodDescriptor<GetZoneRequest, Zone> getMethodDescriptor =
58       ApiMethodDescriptor.<GetZoneRequest, Zone>newBuilder()
59           .setFullMethodName("google.cloud.compute.v1.Zones/Get")
60           .setHttpMethod("GET")
61           .setType(ApiMethodDescriptor.MethodType.UNARY)
62           .setRequestFormatter(
63               ProtoMessageRequestFormatter.<GetZoneRequest>newBuilder()
64                   .setPath(
65                       "/compute/v1/projects/{project}/zones/{zone}",
66                       request -> {
67                         Map<String, String> fields = new HashMap<>();
68                         ProtoRestSerializer<GetZoneRequest> serializer =
69                             ProtoRestSerializer.create();
70                         serializer.putPathParam(fields, "project", request.getProject());
71                         serializer.putPathParam(fields, "zone", request.getZone());
72                         return fields;
73                       })
74                   .setQueryParamsExtractor(
75                       request -> {
76                         Map<String, List<String>> fields = new HashMap<>();
77                         ProtoRestSerializer<GetZoneRequest> serializer =
78                             ProtoRestSerializer.create();
79                         return fields;
80                       })
81                   .setRequestBodyExtractor(request -> null)
82                   .build())
83           .setResponseParser(
84               ProtoMessageResponseParser.<Zone>newBuilder()
85                   .setDefaultInstance(Zone.getDefaultInstance())
86                   .setDefaultTypeRegistry(typeRegistry)
87                   .build())
88           .build();
89 
90   private static final ApiMethodDescriptor<ListZonesRequest, ZoneList> listMethodDescriptor =
91       ApiMethodDescriptor.<ListZonesRequest, ZoneList>newBuilder()
92           .setFullMethodName("google.cloud.compute.v1.Zones/List")
93           .setHttpMethod("GET")
94           .setType(ApiMethodDescriptor.MethodType.UNARY)
95           .setRequestFormatter(
96               ProtoMessageRequestFormatter.<ListZonesRequest>newBuilder()
97                   .setPath(
98                       "/compute/v1/projects/{project}/zones",
99                       request -> {
100                         Map<String, String> fields = new HashMap<>();
101                         ProtoRestSerializer<ListZonesRequest> serializer =
102                             ProtoRestSerializer.create();
103                         serializer.putPathParam(fields, "project", request.getProject());
104                         return fields;
105                       })
106                   .setQueryParamsExtractor(
107                       request -> {
108                         Map<String, List<String>> fields = new HashMap<>();
109                         ProtoRestSerializer<ListZonesRequest> serializer =
110                             ProtoRestSerializer.create();
111                         if (request.hasFilter()) {
112                           serializer.putQueryParam(fields, "filter", request.getFilter());
113                         }
114                         if (request.hasMaxResults()) {
115                           serializer.putQueryParam(fields, "maxResults", request.getMaxResults());
116                         }
117                         if (request.hasOrderBy()) {
118                           serializer.putQueryParam(fields, "orderBy", request.getOrderBy());
119                         }
120                         if (request.hasPageToken()) {
121                           serializer.putQueryParam(fields, "pageToken", request.getPageToken());
122                         }
123                         if (request.hasReturnPartialSuccess()) {
124                           serializer.putQueryParam(
125                               fields, "returnPartialSuccess", request.getReturnPartialSuccess());
126                         }
127                         return fields;
128                       })
129                   .setRequestBodyExtractor(request -> null)
130                   .build())
131           .setResponseParser(
132               ProtoMessageResponseParser.<ZoneList>newBuilder()
133                   .setDefaultInstance(ZoneList.getDefaultInstance())
134                   .setDefaultTypeRegistry(typeRegistry)
135                   .build())
136           .build();
137 
138   private final UnaryCallable<GetZoneRequest, Zone> getCallable;
139   private final UnaryCallable<ListZonesRequest, ZoneList> listCallable;
140   private final UnaryCallable<ListZonesRequest, ListPagedResponse> listPagedCallable;
141 
142   private final BackgroundResource backgroundResources;
143   private final HttpJsonStubCallableFactory callableFactory;
144 
create(ZonesStubSettings settings)145   public static final HttpJsonZonesStub create(ZonesStubSettings settings) throws IOException {
146     return new HttpJsonZonesStub(settings, ClientContext.create(settings));
147   }
148 
create(ClientContext clientContext)149   public static final HttpJsonZonesStub create(ClientContext clientContext) throws IOException {
150     return new HttpJsonZonesStub(ZonesStubSettings.newBuilder().build(), clientContext);
151   }
152 
create( ClientContext clientContext, HttpJsonStubCallableFactory callableFactory)153   public static final HttpJsonZonesStub create(
154       ClientContext clientContext, HttpJsonStubCallableFactory callableFactory) throws IOException {
155     return new HttpJsonZonesStub(
156         ZonesStubSettings.newBuilder().build(), clientContext, callableFactory);
157   }
158 
159   /**
160    * Constructs an instance of HttpJsonZonesStub, using the given settings. This is protected so
161    * that it is easy to make a subclass, but otherwise, the static factory methods should be
162    * preferred.
163    */
HttpJsonZonesStub(ZonesStubSettings settings, ClientContext clientContext)164   protected HttpJsonZonesStub(ZonesStubSettings settings, ClientContext clientContext)
165       throws IOException {
166     this(settings, clientContext, new HttpJsonZonesCallableFactory());
167   }
168 
169   /**
170    * Constructs an instance of HttpJsonZonesStub, using the given settings. This is protected so
171    * that it is easy to make a subclass, but otherwise, the static factory methods should be
172    * preferred.
173    */
HttpJsonZonesStub( ZonesStubSettings settings, ClientContext clientContext, HttpJsonStubCallableFactory callableFactory)174   protected HttpJsonZonesStub(
175       ZonesStubSettings settings,
176       ClientContext clientContext,
177       HttpJsonStubCallableFactory callableFactory)
178       throws IOException {
179     this.callableFactory = callableFactory;
180 
181     HttpJsonCallSettings<GetZoneRequest, Zone> getTransportSettings =
182         HttpJsonCallSettings.<GetZoneRequest, Zone>newBuilder()
183             .setMethodDescriptor(getMethodDescriptor)
184             .setTypeRegistry(typeRegistry)
185             .build();
186     HttpJsonCallSettings<ListZonesRequest, ZoneList> listTransportSettings =
187         HttpJsonCallSettings.<ListZonesRequest, ZoneList>newBuilder()
188             .setMethodDescriptor(listMethodDescriptor)
189             .setTypeRegistry(typeRegistry)
190             .build();
191 
192     this.getCallable =
193         callableFactory.createUnaryCallable(
194             getTransportSettings, settings.getSettings(), clientContext);
195     this.listCallable =
196         callableFactory.createUnaryCallable(
197             listTransportSettings, settings.listSettings(), clientContext);
198     this.listPagedCallable =
199         callableFactory.createPagedCallable(
200             listTransportSettings, settings.listSettings(), clientContext);
201 
202     this.backgroundResources =
203         new BackgroundResourceAggregation(clientContext.getBackgroundResources());
204   }
205 
206   @InternalApi
getMethodDescriptors()207   public static List<ApiMethodDescriptor> getMethodDescriptors() {
208     List<ApiMethodDescriptor> methodDescriptors = new ArrayList<>();
209     methodDescriptors.add(getMethodDescriptor);
210     methodDescriptors.add(listMethodDescriptor);
211     return methodDescriptors;
212   }
213 
214   @Override
getCallable()215   public UnaryCallable<GetZoneRequest, Zone> getCallable() {
216     return getCallable;
217   }
218 
219   @Override
listCallable()220   public UnaryCallable<ListZonesRequest, ZoneList> listCallable() {
221     return listCallable;
222   }
223 
224   @Override
listPagedCallable()225   public UnaryCallable<ListZonesRequest, ListPagedResponse> listPagedCallable() {
226     return listPagedCallable;
227   }
228 
229   @Override
close()230   public final void close() {
231     try {
232       backgroundResources.close();
233     } catch (RuntimeException e) {
234       throw e;
235     } catch (Exception e) {
236       throw new IllegalStateException("Failed to close resource", e);
237     }
238   }
239 
240   @Override
shutdown()241   public void shutdown() {
242     backgroundResources.shutdown();
243   }
244 
245   @Override
isShutdown()246   public boolean isShutdown() {
247     return backgroundResources.isShutdown();
248   }
249 
250   @Override
isTerminated()251   public boolean isTerminated() {
252     return backgroundResources.isTerminated();
253   }
254 
255   @Override
shutdownNow()256   public void shutdownNow() {
257     backgroundResources.shutdownNow();
258   }
259 
260   @Override
awaitTermination(long duration, TimeUnit unit)261   public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
262     return backgroundResources.awaitTermination(duration, unit);
263   }
264 }
265