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