From 33a8ccf3757a22845e046cf21e0a965e89798673 Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Mon, 27 Jun 2022 18:48:16 +0000 Subject: [PATCH] feat: Enable REST transport for most of Java and Go clients (#86) - [ ] Regenerate this pull request now. PiperOrigin-RevId: 456641589 Source-Link: https://github.com/googleapis/googleapis/commit/8a251f5225b789b2383207ffd978f6aa3d77fcf7 Source-Link: https://github.com/googleapis/googleapis-gen/commit/4ca52a529cf01308d9714950edffbea3560cfbdb Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiNGNhNTJhNTI5Y2YwMTMwOGQ5NzE0OTUwZWRmZmJlYTM1NjBjZmJkYiJ9 --- google-cloud-eventarc-publishing/pom.xml | 16 ++ .../publishing/v1/PublisherClient.java | 14 + .../publishing/v1/PublisherSettings.java | 23 +- .../HttpJsonPublisherCallableFactory.java | 105 +++++++ .../v1/stub/HttpJsonPublisherStub.java | 264 ++++++++++++++++++ .../v1/stub/PublisherStubSettings.java | 52 +++- .../v1/PublisherClientHttpJsonTest.java | 176 ++++++++++++ 7 files changed, 645 insertions(+), 5 deletions(-) create mode 100644 google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherCallableFactory.java create mode 100644 google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherStub.java create mode 100644 google-cloud-eventarc-publishing/src/test/java/com/google/cloud/eventarc/publishing/v1/PublisherClientHttpJsonTest.java diff --git a/google-cloud-eventarc-publishing/pom.xml b/google-cloud-eventarc-publishing/pom.xml index b8f780e..3ecff62 100644 --- a/google-cloud-eventarc-publishing/pom.xml +++ b/google-cloud-eventarc-publishing/pom.xml @@ -58,6 +58,10 @@ com.google.api gax-grpc + + com.google.api + gax-httpjson + org.threeten threetenbp @@ -77,12 +81,24 @@ test + + com.google.api + gax + testlib + test + com.google.api gax-grpc testlib test + + com.google.api + gax-httpjson + testlib + test + diff --git a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherClient.java b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherClient.java index aa6f53a..ac7f302 100644 --- a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherClient.java +++ b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherClient.java @@ -113,6 +113,20 @@ * PublisherClient publisherClient = PublisherClient.create(publisherSettings); * } * + *

To use REST (HTTP1.1/JSON) transport (instead of gRPC) for sending and receiving requests over + * the wire: + * + *

{@code
+ * // This snippet has been automatically generated for illustrative purposes only.
+ * // It may require modifications to work in your environment.
+ * PublisherSettings publisherSettings =
+ *     PublisherSettings.newBuilder()
+ *         .setTransportChannelProvider(
+ *             PublisherSettings.defaultHttpJsonTransportProviderBuilder().build())
+ *         .build();
+ * PublisherClient publisherClient = PublisherClient.create(publisherSettings);
+ * }
+ * *

Please refer to the GitHub repository's samples for more quickstart code snippets. */ @Generated("by gapic-generator-java") diff --git a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherSettings.java b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherSettings.java index 8a78619..5c9fea4 100644 --- a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherSettings.java +++ b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/PublisherSettings.java @@ -21,6 +21,7 @@ import com.google.api.gax.core.GoogleCredentialsProvider; import com.google.api.gax.core.InstantiatingExecutorProvider; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider; import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.ClientContext; import com.google.api.gax.rpc.ClientSettings; @@ -104,11 +105,18 @@ public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilde return PublisherStubSettings.defaultCredentialsProviderBuilder(); } - /** Returns a builder for the default ChannelProvider for this service. */ + /** Returns a builder for the default gRPC ChannelProvider for this service. */ public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { return PublisherStubSettings.defaultGrpcTransportProviderBuilder(); } + /** Returns a builder for the default REST ChannelProvider for this service. */ + @BetaApi + public static InstantiatingHttpJsonChannelProvider.Builder + defaultHttpJsonTransportProviderBuilder() { + return PublisherStubSettings.defaultHttpJsonTransportProviderBuilder(); + } + public static TransportChannelProvider defaultTransportChannelProvider() { return PublisherStubSettings.defaultTransportChannelProvider(); } @@ -118,11 +126,17 @@ public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuil return PublisherStubSettings.defaultApiClientHeaderProviderBuilder(); } - /** Returns a new builder for this class. */ + /** Returns a new gRPC builder for this class. */ public static Builder newBuilder() { return Builder.createDefault(); } + /** Returns a new REST builder for this class. */ + @BetaApi + public static Builder newHttpJsonBuilder() { + return Builder.createHttpJsonDefault(); + } + /** Returns a new builder for this class. */ public static Builder newBuilder(ClientContext clientContext) { return new Builder(clientContext); @@ -160,6 +174,11 @@ private static Builder createDefault() { return new Builder(PublisherStubSettings.newBuilder()); } + @BetaApi + private static Builder createHttpJsonDefault() { + return new Builder(PublisherStubSettings.newHttpJsonBuilder()); + } + public PublisherStubSettings.Builder getStubSettingsBuilder() { return ((PublisherStubSettings.Builder) getStubSettings()); } diff --git a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherCallableFactory.java b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherCallableFactory.java new file mode 100644 index 0000000..f9c715c --- /dev/null +++ b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherCallableFactory.java @@ -0,0 +1,105 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.eventarc.publishing.v1.stub; + +import com.google.api.core.BetaApi; +import com.google.api.gax.httpjson.HttpJsonCallSettings; +import com.google.api.gax.httpjson.HttpJsonCallableFactory; +import com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable; +import com.google.api.gax.httpjson.HttpJsonStubCallableFactory; +import com.google.api.gax.httpjson.longrunning.stub.OperationsStub; +import com.google.api.gax.rpc.BatchingCallSettings; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.OperationCallSettings; +import com.google.api.gax.rpc.OperationCallable; +import com.google.api.gax.rpc.PagedCallSettings; +import com.google.api.gax.rpc.ServerStreamingCallSettings; +import com.google.api.gax.rpc.ServerStreamingCallable; +import com.google.api.gax.rpc.UnaryCallSettings; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.longrunning.Operation; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * REST callable factory implementation for the Publisher service API. + * + *

This class is for advanced usage. + */ +@Generated("by gapic-generator-java") +@BetaApi +public class HttpJsonPublisherCallableFactory + implements HttpJsonStubCallableFactory { + + @Override + public UnaryCallable createUnaryCallable( + HttpJsonCallSettings httpJsonCallSettings, + UnaryCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createUnaryCallable( + httpJsonCallSettings, callSettings, clientContext); + } + + @Override + public + UnaryCallable createPagedCallable( + HttpJsonCallSettings httpJsonCallSettings, + PagedCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createPagedCallable( + httpJsonCallSettings, callSettings, clientContext); + } + + @Override + public UnaryCallable createBatchingCallable( + HttpJsonCallSettings httpJsonCallSettings, + BatchingCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createBatchingCallable( + httpJsonCallSettings, callSettings, clientContext); + } + + @BetaApi( + "The surface for long-running operations is not stable yet and may change in the future.") + @Override + public + OperationCallable createOperationCallable( + HttpJsonCallSettings httpJsonCallSettings, + OperationCallSettings callSettings, + ClientContext clientContext, + OperationsStub operationsStub) { + UnaryCallable innerCallable = + HttpJsonCallableFactory.createBaseUnaryCallable( + httpJsonCallSettings, callSettings.getInitialCallSettings(), clientContext); + HttpJsonOperationSnapshotCallable initialCallable = + new HttpJsonOperationSnapshotCallable( + innerCallable, + httpJsonCallSettings.getMethodDescriptor().getOperationSnapshotFactory()); + return HttpJsonCallableFactory.createOperationCallable( + callSettings, clientContext, operationsStub.longRunningClient(), initialCallable); + } + + @Override + public + ServerStreamingCallable createServerStreamingCallable( + HttpJsonCallSettings httpJsonCallSettings, + ServerStreamingCallSettings callSettings, + ClientContext clientContext) { + return HttpJsonCallableFactory.createServerStreamingCallable( + httpJsonCallSettings, callSettings, clientContext); + } +} diff --git a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherStub.java b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherStub.java new file mode 100644 index 0000000..17a0f2f --- /dev/null +++ b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/HttpJsonPublisherStub.java @@ -0,0 +1,264 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.eventarc.publishing.v1.stub; + +import com.google.api.core.BetaApi; +import com.google.api.core.InternalApi; +import com.google.api.gax.core.BackgroundResource; +import com.google.api.gax.core.BackgroundResourceAggregation; +import com.google.api.gax.httpjson.ApiMethodDescriptor; +import com.google.api.gax.httpjson.HttpJsonCallSettings; +import com.google.api.gax.httpjson.HttpJsonStubCallableFactory; +import com.google.api.gax.httpjson.ProtoMessageRequestFormatter; +import com.google.api.gax.httpjson.ProtoMessageResponseParser; +import com.google.api.gax.httpjson.ProtoRestSerializer; +import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.cloud.eventarc.publishing.v1.PublishChannelConnectionEventsRequest; +import com.google.cloud.eventarc.publishing.v1.PublishChannelConnectionEventsResponse; +import com.google.cloud.eventarc.publishing.v1.PublishEventsRequest; +import com.google.cloud.eventarc.publishing.v1.PublishEventsResponse; +import com.google.protobuf.TypeRegistry; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.TimeUnit; +import javax.annotation.Generated; + +// AUTO-GENERATED DOCUMENTATION AND CLASS. +/** + * REST stub implementation for the Publisher service API. + * + *

This class is for advanced usage and reflects the underlying API directly. + */ +@Generated("by gapic-generator-java") +@BetaApi +public class HttpJsonPublisherStub extends PublisherStub { + private static final TypeRegistry typeRegistry = TypeRegistry.newBuilder().build(); + + private static final ApiMethodDescriptor< + PublishChannelConnectionEventsRequest, PublishChannelConnectionEventsResponse> + publishChannelConnectionEventsMethodDescriptor = + ApiMethodDescriptor + . + newBuilder() + .setFullMethodName( + "google.cloud.eventarc.publishing.v1.Publisher/PublishChannelConnectionEvents") + .setHttpMethod("POST") + .setType(ApiMethodDescriptor.MethodType.UNARY) + .setRequestFormatter( + ProtoMessageRequestFormatter.newBuilder() + .setPath( + "/v1/{channelConnection=projects/*/locations/*/channelConnections/*}:publishEvents", + request -> { + Map fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + serializer.putPathParam( + fields, "channelConnection", request.getChannelConnection()); + return fields; + }) + .setQueryParamsExtractor( + request -> { + Map> fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setRequestBodyExtractor( + request -> + ProtoRestSerializer.create() + .toBody( + "*", request.toBuilder().clearChannelConnection().build())) + .build()) + .setResponseParser( + ProtoMessageResponseParser.newBuilder() + .setDefaultInstance( + PublishChannelConnectionEventsResponse.getDefaultInstance()) + .setDefaultTypeRegistry(typeRegistry) + .build()) + .build(); + + private static final ApiMethodDescriptor + publishEventsMethodDescriptor = + ApiMethodDescriptor.newBuilder() + .setFullMethodName("google.cloud.eventarc.publishing.v1.Publisher/PublishEvents") + .setHttpMethod("POST") + .setType(ApiMethodDescriptor.MethodType.UNARY) + .setRequestFormatter( + ProtoMessageRequestFormatter.newBuilder() + .setPath( + "/v1/{channel=projects/*/locations/*/channels/*}:publishEvents", + request -> { + Map fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + serializer.putPathParam(fields, "channel", request.getChannel()); + return fields; + }) + .setQueryParamsExtractor( + request -> { + Map> fields = new HashMap<>(); + ProtoRestSerializer serializer = + ProtoRestSerializer.create(); + return fields; + }) + .setRequestBodyExtractor( + request -> + ProtoRestSerializer.create() + .toBody("*", request.toBuilder().clearChannel().build())) + .build()) + .setResponseParser( + ProtoMessageResponseParser.newBuilder() + .setDefaultInstance(PublishEventsResponse.getDefaultInstance()) + .setDefaultTypeRegistry(typeRegistry) + .build()) + .build(); + + private final UnaryCallable< + PublishChannelConnectionEventsRequest, PublishChannelConnectionEventsResponse> + publishChannelConnectionEventsCallable; + private final UnaryCallable publishEventsCallable; + + private final BackgroundResource backgroundResources; + private final HttpJsonStubCallableFactory callableFactory; + + public static final HttpJsonPublisherStub create(PublisherStubSettings settings) + throws IOException { + return new HttpJsonPublisherStub(settings, ClientContext.create(settings)); + } + + public static final HttpJsonPublisherStub create(ClientContext clientContext) throws IOException { + return new HttpJsonPublisherStub( + PublisherStubSettings.newHttpJsonBuilder().build(), clientContext); + } + + public static final HttpJsonPublisherStub create( + ClientContext clientContext, HttpJsonStubCallableFactory callableFactory) throws IOException { + return new HttpJsonPublisherStub( + PublisherStubSettings.newHttpJsonBuilder().build(), clientContext, callableFactory); + } + + /** + * Constructs an instance of HttpJsonPublisherStub, using the given settings. This is protected so + * that it is easy to make a subclass, but otherwise, the static factory methods should be + * preferred. + */ + protected HttpJsonPublisherStub(PublisherStubSettings settings, ClientContext clientContext) + throws IOException { + this(settings, clientContext, new HttpJsonPublisherCallableFactory()); + } + + /** + * Constructs an instance of HttpJsonPublisherStub, using the given settings. This is protected so + * that it is easy to make a subclass, but otherwise, the static factory methods should be + * preferred. + */ + protected HttpJsonPublisherStub( + PublisherStubSettings settings, + ClientContext clientContext, + HttpJsonStubCallableFactory callableFactory) + throws IOException { + this.callableFactory = callableFactory; + + HttpJsonCallSettings< + PublishChannelConnectionEventsRequest, PublishChannelConnectionEventsResponse> + publishChannelConnectionEventsTransportSettings = + HttpJsonCallSettings + . + newBuilder() + .setMethodDescriptor(publishChannelConnectionEventsMethodDescriptor) + .setTypeRegistry(typeRegistry) + .build(); + HttpJsonCallSettings + publishEventsTransportSettings = + HttpJsonCallSettings.newBuilder() + .setMethodDescriptor(publishEventsMethodDescriptor) + .setTypeRegistry(typeRegistry) + .build(); + + this.publishChannelConnectionEventsCallable = + callableFactory.createUnaryCallable( + publishChannelConnectionEventsTransportSettings, + settings.publishChannelConnectionEventsSettings(), + clientContext); + this.publishEventsCallable = + callableFactory.createUnaryCallable( + publishEventsTransportSettings, settings.publishEventsSettings(), clientContext); + + this.backgroundResources = + new BackgroundResourceAggregation(clientContext.getBackgroundResources()); + } + + @InternalApi + public static List getMethodDescriptors() { + List methodDescriptors = new ArrayList<>(); + methodDescriptors.add(publishChannelConnectionEventsMethodDescriptor); + methodDescriptors.add(publishEventsMethodDescriptor); + return methodDescriptors; + } + + @Override + public UnaryCallable< + PublishChannelConnectionEventsRequest, PublishChannelConnectionEventsResponse> + publishChannelConnectionEventsCallable() { + return publishChannelConnectionEventsCallable; + } + + @Override + public UnaryCallable publishEventsCallable() { + return publishEventsCallable; + } + + @Override + public final void close() { + try { + backgroundResources.close(); + } catch (RuntimeException e) { + throw e; + } catch (Exception e) { + throw new IllegalStateException("Failed to close resource", e); + } + } + + @Override + public void shutdown() { + backgroundResources.shutdown(); + } + + @Override + public boolean isShutdown() { + return backgroundResources.isShutdown(); + } + + @Override + public boolean isTerminated() { + return backgroundResources.isTerminated(); + } + + @Override + public void shutdownNow() { + backgroundResources.shutdownNow(); + } + + @Override + public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { + return backgroundResources.awaitTermination(duration, unit); + } +} diff --git a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/PublisherStubSettings.java b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/PublisherStubSettings.java index 2346ff6..fd85736 100644 --- a/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/PublisherStubSettings.java +++ b/google-cloud-eventarc-publishing/src/main/java/com/google/cloud/eventarc/publishing/v1/stub/PublisherStubSettings.java @@ -24,6 +24,9 @@ import com.google.api.gax.grpc.GaxGrpcProperties; import com.google.api.gax.grpc.GrpcTransportChannel; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; +import com.google.api.gax.httpjson.GaxHttpJsonProperties; +import com.google.api.gax.httpjson.HttpJsonTransportChannel; +import com.google.api.gax.httpjson.InstantiatingHttpJsonChannelProvider; import com.google.api.gax.retrying.RetrySettings; import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.ClientContext; @@ -108,6 +111,11 @@ public PublisherStub createStub() throws IOException { .equals(GrpcTransportChannel.getGrpcTransportName())) { return GrpcPublisherStub.create(this); } + if (getTransportChannelProvider() + .getTransportName() + .equals(HttpJsonTransportChannel.getHttpJsonTransportName())) { + return HttpJsonPublisherStub.create(this); + } throw new UnsupportedOperationException( String.format( "Transport not supported: %s", getTransportChannelProvider().getTransportName())); @@ -140,29 +148,54 @@ public static GoogleCredentialsProvider.Builder defaultCredentialsProviderBuilde .setUseJwtAccessWithScope(true); } - /** Returns a builder for the default ChannelProvider for this service. */ + /** Returns a builder for the default gRPC ChannelProvider for this service. */ public static InstantiatingGrpcChannelProvider.Builder defaultGrpcTransportProviderBuilder() { return InstantiatingGrpcChannelProvider.newBuilder() .setMaxInboundMessageSize(Integer.MAX_VALUE); } + /** Returns a builder for the default REST ChannelProvider for this service. */ + @BetaApi + public static InstantiatingHttpJsonChannelProvider.Builder + defaultHttpJsonTransportProviderBuilder() { + return InstantiatingHttpJsonChannelProvider.newBuilder(); + } + public static TransportChannelProvider defaultTransportChannelProvider() { return defaultGrpcTransportProviderBuilder().build(); } @BetaApi("The surface for customizing headers is not stable yet and may change in the future.") - public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() { + public static ApiClientHeaderProvider.Builder defaultGrpcApiClientHeaderProviderBuilder() { return ApiClientHeaderProvider.newBuilder() .setGeneratedLibToken("gapic", GaxProperties.getLibraryVersion(PublisherStubSettings.class)) .setTransportToken( GaxGrpcProperties.getGrpcTokenName(), GaxGrpcProperties.getGrpcVersion()); } - /** Returns a new builder for this class. */ + @BetaApi("The surface for customizing headers is not stable yet and may change in the future.") + public static ApiClientHeaderProvider.Builder defaultHttpJsonApiClientHeaderProviderBuilder() { + return ApiClientHeaderProvider.newBuilder() + .setGeneratedLibToken("gapic", GaxProperties.getLibraryVersion(PublisherStubSettings.class)) + .setTransportToken( + GaxHttpJsonProperties.getHttpJsonTokenName(), + GaxHttpJsonProperties.getHttpJsonVersion()); + } + + public static ApiClientHeaderProvider.Builder defaultApiClientHeaderProviderBuilder() { + return PublisherStubSettings.defaultGrpcApiClientHeaderProviderBuilder(); + } + + /** Returns a new gRPC builder for this class. */ public static Builder newBuilder() { return Builder.createDefault(); } + /** Returns a new REST builder for this class. */ + public static Builder newHttpJsonBuilder() { + return Builder.createHttpJsonDefault(); + } + /** Returns a new builder for this class. */ public static Builder newBuilder(ClientContext clientContext) { return new Builder(clientContext); @@ -260,6 +293,19 @@ private static Builder createDefault() { return initDefaults(builder); } + private static Builder createHttpJsonDefault() { + Builder builder = new Builder(((ClientContext) null)); + + builder.setTransportChannelProvider(defaultHttpJsonTransportProviderBuilder().build()); + builder.setCredentialsProvider(defaultCredentialsProviderBuilder().build()); + builder.setInternalHeaderProvider(defaultHttpJsonApiClientHeaderProviderBuilder().build()); + builder.setEndpoint(getDefaultEndpoint()); + builder.setMtlsEndpoint(getDefaultMtlsEndpoint()); + builder.setSwitchToMtlsEndpointAllowed(true); + + return initDefaults(builder); + } + private static Builder initDefaults(Builder builder) { builder .publishChannelConnectionEventsSettings() diff --git a/google-cloud-eventarc-publishing/src/test/java/com/google/cloud/eventarc/publishing/v1/PublisherClientHttpJsonTest.java b/google-cloud-eventarc-publishing/src/test/java/com/google/cloud/eventarc/publishing/v1/PublisherClientHttpJsonTest.java new file mode 100644 index 0000000..0ee9768 --- /dev/null +++ b/google-cloud-eventarc-publishing/src/test/java/com/google/cloud/eventarc/publishing/v1/PublisherClientHttpJsonTest.java @@ -0,0 +1,176 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.google.cloud.eventarc.publishing.v1; + +import com.google.api.gax.core.NoCredentialsProvider; +import com.google.api.gax.httpjson.GaxHttpJsonProperties; +import com.google.api.gax.httpjson.testing.MockHttpService; +import com.google.api.gax.rpc.ApiClientHeaderProvider; +import com.google.api.gax.rpc.ApiException; +import com.google.api.gax.rpc.ApiExceptionFactory; +import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.api.gax.rpc.StatusCode; +import com.google.api.gax.rpc.testing.FakeStatusCode; +import com.google.cloud.eventarc.publishing.v1.stub.HttpJsonPublisherStub; +import com.google.protobuf.Any; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import javax.annotation.Generated; +import org.junit.After; +import org.junit.AfterClass; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +@Generated("by gapic-generator-java") +public class PublisherClientHttpJsonTest { + private static MockHttpService mockService; + private static PublisherClient client; + + @BeforeClass + public static void startStaticServer() throws IOException { + mockService = + new MockHttpService( + HttpJsonPublisherStub.getMethodDescriptors(), PublisherSettings.getDefaultEndpoint()); + PublisherSettings settings = + PublisherSettings.newHttpJsonBuilder() + .setTransportChannelProvider( + PublisherSettings.defaultHttpJsonTransportProviderBuilder() + .setHttpTransport(mockService) + .build()) + .setCredentialsProvider(NoCredentialsProvider.create()) + .build(); + client = PublisherClient.create(settings); + } + + @AfterClass + public static void stopServer() { + client.close(); + } + + @Before + public void setUp() {} + + @After + public void tearDown() throws Exception { + mockService.reset(); + } + + @Test + public void publishChannelConnectionEventsTest() throws Exception { + PublishChannelConnectionEventsResponse expectedResponse = + PublishChannelConnectionEventsResponse.newBuilder().build(); + mockService.addResponse(expectedResponse); + + PublishChannelConnectionEventsRequest request = + PublishChannelConnectionEventsRequest.newBuilder() + .setChannelConnection( + "projects/project-748/locations/location-748/channelConnections/channelConnection-748") + .addAllEvents(new ArrayList()) + .build(); + + PublishChannelConnectionEventsResponse actualResponse = + client.publishChannelConnectionEvents(request); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockService.getRequestPaths(); + Assert.assertEquals(1, actualRequests.size()); + + String apiClientHeaderKey = + mockService + .getRequestHeaders() + .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) + .iterator() + .next(); + Assert.assertTrue( + GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() + .matcher(apiClientHeaderKey) + .matches()); + } + + @Test + public void publishChannelConnectionEventsExceptionTest() throws Exception { + ApiException exception = + ApiExceptionFactory.createException( + new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); + mockService.addException(exception); + + try { + PublishChannelConnectionEventsRequest request = + PublishChannelConnectionEventsRequest.newBuilder() + .setChannelConnection( + "projects/project-748/locations/location-748/channelConnections/channelConnection-748") + .addAllEvents(new ArrayList()) + .build(); + client.publishChannelConnectionEvents(request); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } + + @Test + public void publishEventsTest() throws Exception { + PublishEventsResponse expectedResponse = PublishEventsResponse.newBuilder().build(); + mockService.addResponse(expectedResponse); + + PublishEventsRequest request = + PublishEventsRequest.newBuilder() + .setChannel("projects/project-2616/locations/location-2616/channels/channel-2616") + .addAllEvents(new ArrayList()) + .build(); + + PublishEventsResponse actualResponse = client.publishEvents(request); + Assert.assertEquals(expectedResponse, actualResponse); + + List actualRequests = mockService.getRequestPaths(); + Assert.assertEquals(1, actualRequests.size()); + + String apiClientHeaderKey = + mockService + .getRequestHeaders() + .get(ApiClientHeaderProvider.getDefaultApiClientHeaderKey()) + .iterator() + .next(); + Assert.assertTrue( + GaxHttpJsonProperties.getDefaultApiClientHeaderPattern() + .matcher(apiClientHeaderKey) + .matches()); + } + + @Test + public void publishEventsExceptionTest() throws Exception { + ApiException exception = + ApiExceptionFactory.createException( + new Exception(), FakeStatusCode.of(StatusCode.Code.INVALID_ARGUMENT), false); + mockService.addException(exception); + + try { + PublishEventsRequest request = + PublishEventsRequest.newBuilder() + .setChannel("projects/project-2616/locations/location-2616/channels/channel-2616") + .addAllEvents(new ArrayList()) + .build(); + client.publishEvents(request); + Assert.fail("No exception raised"); + } catch (InvalidArgumentException e) { + // Expected exception. + } + } +}