Skip to content

Commit fd728ea

Browse files
committed
feat(generator): activate resumable upload allowlist and regenerate showcase client
1 parent 9a35087 commit fd728ea

17 files changed

Lines changed: 350 additions & 340 deletions

File tree

java-showcase/gapic-showcase/src/main/java/com/google/showcase/v1beta1/ResumableUploadServiceClient.java

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
import com.google.api.gax.paging.AbstractFixedSizeCollection;
2424
import com.google.api.gax.paging.AbstractPage;
2525
import com.google.api.gax.paging.AbstractPagedListResponse;
26+
import com.google.api.gax.rpc.ApiExceptions;
2627
import com.google.api.gax.rpc.PageContext;
28+
import com.google.api.gax.rpc.ResumableUploadCallable;
2729
import com.google.api.gax.rpc.UnaryCallable;
2830
import com.google.cloud.location.GetLocationRequest;
2931
import com.google.cloud.location.ListLocationsRequest;
@@ -38,6 +40,7 @@
3840
import com.google.showcase.v1beta1.stub.ResumableUploadServiceStub;
3941
import com.google.showcase.v1beta1.stub.ResumableUploadServiceStubSettings;
4042
import java.io.IOException;
43+
import java.io.InputStream;
4144
import java.util.List;
4245
import java.util.concurrent.TimeUnit;
4346
import javax.annotation.Generated;
@@ -59,8 +62,8 @@
5962
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
6063
* try (ResumableUploadServiceClient resumableUploadServiceClient =
6164
* ResumableUploadServiceClient.create()) {
62-
* UploadMediaRequest request = UploadMediaRequest.newBuilder().setName("name3373707").build();
63-
* UploadMediaResponse response = resumableUploadServiceClient.uploadMedia(request);
65+
* GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
66+
* Location response = resumableUploadServiceClient.getLocation(request);
6467
* }
6568
* }</pre>
6669
*
@@ -81,7 +84,7 @@
8184
* <td>
8285
* <p>Request object method variants only take one parameter, a request object, which must be constructed before the call.</p>
8386
* <ul>
84-
* <li><p> uploadMedia(UploadMediaRequest request)
87+
* <li><p> uploadMedia(UploadMediaRequest request, InputStream payload)
8588
* </ul>
8689
* <p>Callable method variants take no parameters and return an immutable API callable object, which can be used to initiate calls to the service.</p>
8790
* <ul>
@@ -277,51 +280,29 @@ public ResumableUploadServiceStub getStub() {
277280
/**
278281
* A method with media_upload annotation enabled.
279282
*
280-
* <p>Sample code:
281-
*
282-
* <pre>{@code
283-
* // This snippet has been automatically generated and should be regarded as a code template only.
284-
* // It will require modifications to work:
285-
* // - It may require correct/in-range values for request initialization.
286-
* // - It may require specifying regional endpoints when creating the service client as shown in
287-
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
288-
* try (ResumableUploadServiceClient resumableUploadServiceClient =
289-
* ResumableUploadServiceClient.create()) {
290-
* UploadMediaRequest request = UploadMediaRequest.newBuilder().setName("name3373707").build();
291-
* UploadMediaResponse response = resumableUploadServiceClient.uploadMedia(request);
292-
* }
293-
* }</pre>
283+
* <p>Call context overrides (such as withTimeout, withRetrySettings, or credentials) apply
284+
* strictly to the start request (session initiation). Per-chunk PUT calls rely on the configured
285+
* timeout and retry settings from ResumableUploadCallSettings.
294286
*
295287
* @param request The request object containing all of the parameters for the API call.
288+
* @param payload The payload data stream to upload.
296289
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
297290
*/
298-
public final UploadMediaResponse uploadMedia(UploadMediaRequest request) {
299-
return uploadMediaCallable().call(request);
291+
public final UploadMediaResponse uploadMedia(UploadMediaRequest request, InputStream payload) {
292+
return ApiExceptions.callAndTranslateApiException(
293+
uploadMediaCallable().futureCall(request, payload));
300294
}
301295

302296
// AUTO-GENERATED DOCUMENTATION AND METHOD.
303297
/**
304298
* A method with media_upload annotation enabled.
305299
*
306-
* <p>Sample code:
307-
*
308-
* <pre>{@code
309-
* // This snippet has been automatically generated and should be regarded as a code template only.
310-
* // It will require modifications to work:
311-
* // - It may require correct/in-range values for request initialization.
312-
* // - It may require specifying regional endpoints when creating the service client as shown in
313-
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
314-
* try (ResumableUploadServiceClient resumableUploadServiceClient =
315-
* ResumableUploadServiceClient.create()) {
316-
* UploadMediaRequest request = UploadMediaRequest.newBuilder().setName("name3373707").build();
317-
* ApiFuture<UploadMediaResponse> future =
318-
* resumableUploadServiceClient.uploadMediaCallable().futureCall(request);
319-
* // Do something.
320-
* UploadMediaResponse response = future.get();
321-
* }
322-
* }</pre>
300+
* <p>Call context overrides (such as withTimeout, withRetrySettings, or credentials) apply
301+
* strictly to the start request (session initiation). Per-chunk PUT calls rely on the configured
302+
* timeout and retry settings from ResumableUploadCallSettings.
323303
*/
324-
public final UnaryCallable<UploadMediaRequest, UploadMediaResponse> uploadMediaCallable() {
304+
public final ResumableUploadCallable<UploadMediaRequest, UploadMediaResponse>
305+
uploadMediaCallable() {
325306
return stub.uploadMediaCallable();
326307
}
327308

java-showcase/gapic-showcase/src/main/java/com/google/showcase/v1beta1/ResumableUploadServiceSettings.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.google.api.gax.rpc.ClientContext;
2929
import com.google.api.gax.rpc.ClientSettings;
3030
import com.google.api.gax.rpc.PagedCallSettings;
31+
import com.google.api.gax.rpc.ResumableUploadCallSettings;
3132
import com.google.api.gax.rpc.TransportChannelProvider;
3233
import com.google.api.gax.rpc.UnaryCallSettings;
3334
import com.google.cloud.location.GetLocationRequest;
@@ -63,7 +64,7 @@
6364
*
6465
* <p>For example, to set the
6566
* [RetrySettings](https://cloud.google.com/java/docs/reference/gax/latest/com.google.api.gax.retrying.RetrySettings)
66-
* of uploadMedia:
67+
* of getLocation:
6768
*
6869
* <pre>{@code
6970
* // This snippet has been automatically generated and should be regarded as a code template only.
@@ -74,10 +75,10 @@
7475
* ResumableUploadServiceSettings.Builder resumableUploadServiceSettingsBuilder =
7576
* ResumableUploadServiceSettings.newBuilder();
7677
* resumableUploadServiceSettingsBuilder
77-
* .uploadMediaSettings()
78+
* .getLocationSettings()
7879
* .setRetrySettings(
7980
* resumableUploadServiceSettingsBuilder
80-
* .uploadMediaSettings()
81+
* .getLocationSettings()
8182
* .getRetrySettings()
8283
* .toBuilder()
8384
* .setInitialRetryDelayDuration(Duration.ofSeconds(1))
@@ -102,8 +103,13 @@
102103
@Generated("by gapic-generator-java")
103104
public class ResumableUploadServiceSettings extends ClientSettings<ResumableUploadServiceSettings> {
104105

105-
/** Returns the object with the settings used for calls to uploadMedia. */
106-
public UnaryCallSettings<UploadMediaRequest, UploadMediaResponse> uploadMediaSettings() {
106+
/**
107+
* Returns the object with the settings used for calls to uploadMedia.
108+
*
109+
* <p>Note that custom retry settings and headers configured via ApiCallContext apply strictly to
110+
* the initial session initiation request.
111+
*/
112+
public ResumableUploadCallSettings uploadMediaSettings() {
107113
return ((ResumableUploadServiceStubSettings) getStubSettings()).uploadMediaSettings();
108114
}
109115

@@ -247,9 +253,13 @@ public Builder applyToAllUnaryMethods(
247253
return this;
248254
}
249255

250-
/** Returns the builder for the settings used for calls to uploadMedia. */
251-
public UnaryCallSettings.Builder<UploadMediaRequest, UploadMediaResponse>
252-
uploadMediaSettings() {
256+
/**
257+
* Returns the builder for the settings used for calls to uploadMedia.
258+
*
259+
* <p>Note that custom retry settings and headers configured via ApiCallContext apply strictly
260+
* to the initial session initiation request.
261+
*/
262+
public ResumableUploadCallSettings.Builder uploadMediaSettings() {
253263
return getStubSettingsBuilder().uploadMediaSettings();
254264
}
255265

java-showcase/gapic-showcase/src/main/java/com/google/showcase/v1beta1/package-info.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,8 @@
137137
* // https://cloud.google.com/java/docs/setup#configure_endpoints_for_the_client_library
138138
* try (ResumableUploadServiceClient resumableUploadServiceClient =
139139
* ResumableUploadServiceClient.create()) {
140-
* UploadMediaRequest request = UploadMediaRequest.newBuilder().setName("name3373707").build();
141-
* UploadMediaResponse response = resumableUploadServiceClient.uploadMedia(request);
140+
* GetLocationRequest request = GetLocationRequest.newBuilder().setName("name3373707").build();
141+
* Location response = resumableUploadServiceClient.getLocation(request);
142142
* }
143143
* }</pre>
144144
*

java-showcase/gapic-showcase/src/main/java/com/google/showcase/v1beta1/stub/GrpcResumableUploadServiceStub.java

Lines changed: 64 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,21 @@
2222
import com.google.api.gax.core.BackgroundResource;
2323
import com.google.api.gax.core.BackgroundResourceAggregation;
2424
import com.google.api.gax.grpc.GrpcCallSettings;
25+
import com.google.api.gax.grpc.GrpcStatusCode;
2526
import com.google.api.gax.grpc.GrpcStubCallableFactory;
27+
import com.google.api.gax.httpjson.HttpJsonCallContext;
28+
import com.google.api.gax.httpjson.HttpJsonTransportChannel;
29+
import com.google.api.gax.httpjson.ManagedHttpJsonChannel;
2630
import com.google.api.gax.rpc.ClientContext;
31+
import com.google.api.gax.rpc.FailedPreconditionException;
2732
import com.google.api.gax.rpc.RequestParamsBuilder;
33+
import com.google.api.gax.rpc.ResumableUploadCallable;
2834
import com.google.api.gax.rpc.UnaryCallable;
2935
import com.google.cloud.location.GetLocationRequest;
3036
import com.google.cloud.location.ListLocationsRequest;
3137
import com.google.cloud.location.ListLocationsResponse;
3238
import com.google.cloud.location.Location;
39+
import com.google.common.collect.ImmutableList;
3340
import com.google.iam.v1.GetIamPolicyRequest;
3441
import com.google.iam.v1.Policy;
3542
import com.google.iam.v1.SetIamPolicyRequest;
@@ -39,11 +46,14 @@
3946
import com.google.showcase.v1beta1.UploadMediaRequest;
4047
import com.google.showcase.v1beta1.UploadMediaResponse;
4148
import io.grpc.MethodDescriptor;
49+
import io.grpc.Status;
4250
import io.grpc.protobuf.ProtoUtils;
4351
import java.io.IOException;
52+
import java.util.Arrays;
4453
import java.util.concurrent.TimeUnit;
4554
import javax.annotation.Generated;
4655
import org.jspecify.annotations.NullMarked;
56+
import org.jspecify.annotations.Nullable;
4757

4858
// AUTO-GENERATED DOCUMENTATION AND CLASS.
4959
/**
@@ -55,17 +65,6 @@
5565
@BetaApi
5666
@Generated("by gapic-generator-java")
5767
public class GrpcResumableUploadServiceStub extends ResumableUploadServiceStub {
58-
private static final MethodDescriptor<UploadMediaRequest, UploadMediaResponse>
59-
uploadMediaMethodDescriptor =
60-
MethodDescriptor.<UploadMediaRequest, UploadMediaResponse>newBuilder()
61-
.setType(MethodDescriptor.MethodType.UNARY)
62-
.setFullMethodName("google.showcase.v1beta1.ResumableUploadService/UploadMedia")
63-
.setRequestMarshaller(ProtoUtils.marshaller(UploadMediaRequest.getDefaultInstance()))
64-
.setResponseMarshaller(
65-
ProtoUtils.marshaller(UploadMediaResponse.getDefaultInstance()))
66-
.setSampledToLocalTracing(true)
67-
.build();
68-
6968
private static final MethodDescriptor<ListLocationsRequest, ListLocationsResponse>
7069
listLocationsMethodDescriptor =
7170
MethodDescriptor.<ListLocationsRequest, ListLocationsResponse>newBuilder()
@@ -117,7 +116,6 @@ public class GrpcResumableUploadServiceStub extends ResumableUploadServiceStub {
117116
.setSampledToLocalTracing(true)
118117
.build();
119118

120-
private final UnaryCallable<UploadMediaRequest, UploadMediaResponse> uploadMediaCallable;
121119
private final UnaryCallable<ListLocationsRequest, ListLocationsResponse> listLocationsCallable;
122120
private final UnaryCallable<ListLocationsRequest, ListLocationsPagedResponse>
123121
listLocationsPagedCallable;
@@ -129,6 +127,7 @@ public class GrpcResumableUploadServiceStub extends ResumableUploadServiceStub {
129127

130128
private final BackgroundResource backgroundResources;
131129
private final GrpcOperationsStub operationsStub;
130+
private final @Nullable HttpJsonResumableUploadServiceResumableUploadStub resumableUploadStub;
132131
private final GrpcStubCallableFactory callableFactory;
133132

134133
public static final GrpcResumableUploadServiceStub create(
@@ -171,10 +170,6 @@ protected GrpcResumableUploadServiceStub(
171170
this.callableFactory = callableFactory;
172171
this.operationsStub = GrpcOperationsStub.create(clientContext, callableFactory);
173172

174-
GrpcCallSettings<UploadMediaRequest, UploadMediaResponse> uploadMediaTransportSettings =
175-
GrpcCallSettings.<UploadMediaRequest, UploadMediaResponse>newBuilder()
176-
.setMethodDescriptor(uploadMediaMethodDescriptor)
177-
.build();
178173
GrpcCallSettings<ListLocationsRequest, ListLocationsResponse> listLocationsTransportSettings =
179174
GrpcCallSettings.<ListLocationsRequest, ListLocationsResponse>newBuilder()
180175
.setMethodDescriptor(listLocationsMethodDescriptor)
@@ -230,9 +225,6 @@ protected GrpcResumableUploadServiceStub(
230225
.setResourceNameExtractor(request -> request.getResource())
231226
.build();
232227

233-
this.uploadMediaCallable =
234-
callableFactory.createUnaryCallable(
235-
uploadMediaTransportSettings, settings.uploadMediaSettings(), clientContext);
236228
this.listLocationsCallable =
237229
callableFactory.createUnaryCallable(
238230
listLocationsTransportSettings, settings.listLocationsSettings(), clientContext);
@@ -254,19 +246,51 @@ protected GrpcResumableUploadServiceStub(
254246
settings.testIamPermissionsSettings(),
255247
clientContext);
256248

257-
this.backgroundResources =
258-
new BackgroundResourceAggregation(clientContext.getBackgroundResources());
249+
if (clientContext.getCredentials() != null) {
250+
ManagedHttpJsonChannel httpJsonManagedChannel =
251+
ManagedHttpJsonChannel.newBuilder()
252+
.setEndpoint(settings.getEndpoint())
253+
.setExecutor(clientContext.getExecutor())
254+
.build();
255+
HttpJsonTransportChannel httpJsonTransportChannel =
256+
HttpJsonTransportChannel.create(httpJsonManagedChannel);
257+
ClientContext httpJsonClientContext =
258+
ClientContext.newBuilder()
259+
.setCredentials(clientContext.getCredentials())
260+
.setEndpoint(settings.getEndpoint())
261+
.setExecutor(clientContext.getExecutor())
262+
.setHeaders(clientContext.getHeaders())
263+
.setInternalHeaders(clientContext.getInternalHeaders())
264+
.setClock(clientContext.getClock())
265+
.setTransportChannel(httpJsonTransportChannel)
266+
.setDefaultCallContext(
267+
HttpJsonCallContext.createDefault()
268+
.withTransportChannel(httpJsonTransportChannel))
269+
.setBackgroundResources(Arrays.asList(httpJsonTransportChannel))
270+
.build();
271+
this.resumableUploadStub =
272+
HttpJsonResumableUploadServiceResumableUploadStub.create(httpJsonClientContext, settings);
273+
} else {
274+
this.resumableUploadStub = null;
275+
}
276+
277+
if (resumableUploadStub != null) {
278+
this.backgroundResources =
279+
new BackgroundResourceAggregation(
280+
ImmutableList.<BackgroundResource>builder()
281+
.addAll(clientContext.getBackgroundResources())
282+
.add(resumableUploadStub)
283+
.build());
284+
} else {
285+
this.backgroundResources =
286+
new BackgroundResourceAggregation(clientContext.getBackgroundResources());
287+
}
259288
}
260289

261290
public GrpcOperationsStub getOperationsStub() {
262291
return operationsStub;
263292
}
264293

265-
@Override
266-
public UnaryCallable<UploadMediaRequest, UploadMediaResponse> uploadMediaCallable() {
267-
return uploadMediaCallable;
268-
}
269-
270294
@Override
271295
public UnaryCallable<ListLocationsRequest, ListLocationsResponse> listLocationsCallable() {
272296
return listLocationsCallable;
@@ -299,6 +323,20 @@ public UnaryCallable<GetIamPolicyRequest, Policy> getIamPolicyCallable() {
299323
return testIamPermissionsCallable;
300324
}
301325

326+
@Override
327+
public ResumableUploadCallable<UploadMediaRequest, UploadMediaResponse> uploadMediaCallable() {
328+
if (resumableUploadStub == null) {
329+
throw new FailedPreconditionException(
330+
"Resumable uploads execute over HTTP/REST and require credentials. The client was"
331+
+ " initialized with a pre-constructed gRPC Channel, from which credentials cannot be"
332+
+ " extracted. Please configure a CredentialsProvider instead.",
333+
null,
334+
GrpcStatusCode.of(Status.Code.FAILED_PRECONDITION),
335+
false);
336+
}
337+
return resumableUploadStub.uploadMediaCallable();
338+
}
339+
302340
@Override
303341
public final void close() {
304342
try {

0 commit comments

Comments
 (0)