From 60ed1e0341319b218a86d6fe1908b755d194047d Mon Sep 17 00:00:00 2001 From: yoshi-automation Date: Tue, 28 Apr 2020 02:48:07 -0700 Subject: [PATCH] Configure retry for Diaglogflow v2.Environments.ListEnvironments and v2beta1.Environments.ListEnvironments. PiperOrigin-RevId: 308749074 Source-Author: Google APIs Source-Date: Mon Apr 27 20:01:53 2020 -0700 Source-Repo: googleapis/googleapis Source-Sha: c284e4f849e4b085a297d336cef8721ab69ba013 Source-Link: https://github.com/googleapis/googleapis/commit/c284e4f849e4b085a297d336cef8721ab69ba013 --- .../v2beta1/EnvironmentsClient.java | 205 +++++++++++++++++- .../v2beta1/EnvironmentsSettings.java | 9 +- .../dialogflow/v2beta1/package-info.java | 4 +- .../v2beta1/stub/EnvironmentsStub.java | 7 + .../stub/EnvironmentsStubSettings.java | 85 +++++++- .../v2beta1/stub/GrpcEnvironmentsStub.java | 12 + .../v2beta1/EnvironmentsClientTest.java | 26 ++- .../dialogflow/v2beta1/EnvironmentName.java | 182 ++++++++++++++++ synth.metadata | 10 +- 9 files changed, 509 insertions(+), 31 deletions(-) create mode 100644 proto-google-cloud-dialogflow-v2beta1/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentName.java diff --git a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClient.java b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClient.java index cd8cc95a2..8267eacbf 100644 --- a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClient.java +++ b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClient.java @@ -15,12 +15,21 @@ */ package com.google.cloud.dialogflow.v2beta1; +import com.google.api.core.ApiFunction; +import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutures; import com.google.api.core.BetaApi; import com.google.api.gax.core.BackgroundResource; +import com.google.api.gax.paging.AbstractFixedSizeCollection; +import com.google.api.gax.paging.AbstractPage; +import com.google.api.gax.paging.AbstractPagedListResponse; +import com.google.api.gax.rpc.PageContext; import com.google.api.gax.rpc.UnaryCallable; import com.google.cloud.dialogflow.v2beta1.stub.EnvironmentsStub; import com.google.cloud.dialogflow.v2beta1.stub.EnvironmentsStubSettings; +import com.google.common.util.concurrent.MoreExecutors; import java.io.IOException; +import java.util.List; import java.util.concurrent.TimeUnit; import javax.annotation.Generated; @@ -34,8 +43,10 @@ *
  * 
  * try (EnvironmentsClient environmentsClient = EnvironmentsClient.create()) {
- *   ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder().build();
- *   ListEnvironmentsResponse response = environmentsClient.listEnvironments(request);
+ *   ProjectAgentName parent = ProjectAgentName.of("[PROJECT]");
+ *   for (Environment element : environmentsClient.listEnvironments(parent).iterateAll()) {
+ *     // doThingsWith(element);
+ *   }
  * }
  * 
  * 
@@ -151,16 +162,73 @@ public EnvironmentsStub getStub() { * *

    * try (EnvironmentsClient environmentsClient = EnvironmentsClient.create()) {
-   *   ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder().build();
-   *   ListEnvironmentsResponse response = environmentsClient.listEnvironments(request);
+   *   ProjectAgentName parent = ProjectAgentName.of("[PROJECT]");
+   *   for (Environment element : environmentsClient.listEnvironments(parent).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param parent Required. The agent to list all environments from. Format: `projects/<Project + * ID>/agent`. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final ListEnvironmentsPagedResponse listEnvironments(ProjectAgentName parent) { + ListEnvironmentsRequest request = + ListEnvironmentsRequest.newBuilder() + .setParent(parent == null ? null : parent.toString()) + .build(); + return listEnvironments(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns the list of all non-draft environments of the specified agent. + * + *

Sample code: + * + *


+   * try (EnvironmentsClient environmentsClient = EnvironmentsClient.create()) {
+   *   ProjectAgentName parent = ProjectAgentName.of("[PROJECT]");
+   *   for (Environment element : environmentsClient.listEnvironments(parent.toString()).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ * + * @param parent Required. The agent to list all environments from. Format: `projects/<Project + * ID>/agent`. + * @throws com.google.api.gax.rpc.ApiException if the remote call fails + */ + public final ListEnvironmentsPagedResponse listEnvironments(String parent) { + ListEnvironmentsRequest request = + ListEnvironmentsRequest.newBuilder().setParent(parent).build(); + return listEnvironments(request); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns the list of all non-draft environments of the specified agent. + * + *

Sample code: + * + *


+   * try (EnvironmentsClient environmentsClient = EnvironmentsClient.create()) {
+   *   ProjectAgentName parent = ProjectAgentName.of("[PROJECT]");
+   *   ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder()
+   *     .setParent(parent.toString())
+   *     .build();
+   *   for (Environment element : environmentsClient.listEnvironments(request).iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
    * }
    * 
* * @param request The request object containing all of the parameters for the API call. * @throws com.google.api.gax.rpc.ApiException if the remote call fails */ - public final ListEnvironmentsResponse listEnvironments(ListEnvironmentsRequest request) { - return listEnvironmentsCallable().call(request); + public final ListEnvironmentsPagedResponse listEnvironments(ListEnvironmentsRequest request) { + return listEnvironmentsPagedCallable().call(request); } // AUTO-GENERATED DOCUMENTATION AND METHOD @@ -171,10 +239,47 @@ public final ListEnvironmentsResponse listEnvironments(ListEnvironmentsRequest r * *

    * try (EnvironmentsClient environmentsClient = EnvironmentsClient.create()) {
-   *   ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder().build();
-   *   ApiFuture<ListEnvironmentsResponse> future = environmentsClient.listEnvironmentsCallable().futureCall(request);
+   *   ProjectAgentName parent = ProjectAgentName.of("[PROJECT]");
+   *   ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder()
+   *     .setParent(parent.toString())
+   *     .build();
+   *   ApiFuture<ListEnvironmentsPagedResponse> future = environmentsClient.listEnvironmentsPagedCallable().futureCall(request);
    *   // Do something
-   *   ListEnvironmentsResponse response = future.get();
+   *   for (Environment element : future.get().iterateAll()) {
+   *     // doThingsWith(element);
+   *   }
+   * }
+   * 
+ */ + public final UnaryCallable + listEnvironmentsPagedCallable() { + return stub.listEnvironmentsPagedCallable(); + } + + // AUTO-GENERATED DOCUMENTATION AND METHOD + /** + * Returns the list of all non-draft environments of the specified agent. + * + *

Sample code: + * + *


+   * try (EnvironmentsClient environmentsClient = EnvironmentsClient.create()) {
+   *   ProjectAgentName parent = ProjectAgentName.of("[PROJECT]");
+   *   ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder()
+   *     .setParent(parent.toString())
+   *     .build();
+   *   while (true) {
+   *     ListEnvironmentsResponse response = environmentsClient.listEnvironmentsCallable().call(request);
+   *     for (Environment element : response.getEnvironmentsList()) {
+   *       // doThingsWith(element);
+   *     }
+   *     String nextPageToken = response.getNextPageToken();
+   *     if (!Strings.isNullOrEmpty(nextPageToken)) {
+   *       request = request.toBuilder().setPageToken(nextPageToken).build();
+   *     } else {
+   *       break;
+   *     }
+   *   }
    * }
    * 
*/ @@ -212,4 +317,86 @@ public void shutdownNow() { public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException { return stub.awaitTermination(duration, unit); } + + public static class ListEnvironmentsPagedResponse + extends AbstractPagedListResponse< + ListEnvironmentsRequest, + ListEnvironmentsResponse, + Environment, + ListEnvironmentsPage, + ListEnvironmentsFixedSizeCollection> { + + public static ApiFuture createAsync( + PageContext context, + ApiFuture futureResponse) { + ApiFuture futurePage = + ListEnvironmentsPage.createEmptyPage().createPageAsync(context, futureResponse); + return ApiFutures.transform( + futurePage, + new ApiFunction() { + @Override + public ListEnvironmentsPagedResponse apply(ListEnvironmentsPage input) { + return new ListEnvironmentsPagedResponse(input); + } + }, + MoreExecutors.directExecutor()); + } + + private ListEnvironmentsPagedResponse(ListEnvironmentsPage page) { + super(page, ListEnvironmentsFixedSizeCollection.createEmptyCollection()); + } + } + + public static class ListEnvironmentsPage + extends AbstractPage< + ListEnvironmentsRequest, ListEnvironmentsResponse, Environment, ListEnvironmentsPage> { + + private ListEnvironmentsPage( + PageContext context, + ListEnvironmentsResponse response) { + super(context, response); + } + + private static ListEnvironmentsPage createEmptyPage() { + return new ListEnvironmentsPage(null, null); + } + + @Override + protected ListEnvironmentsPage createPage( + PageContext context, + ListEnvironmentsResponse response) { + return new ListEnvironmentsPage(context, response); + } + + @Override + public ApiFuture createPageAsync( + PageContext context, + ApiFuture futureResponse) { + return super.createPageAsync(context, futureResponse); + } + } + + public static class ListEnvironmentsFixedSizeCollection + extends AbstractFixedSizeCollection< + ListEnvironmentsRequest, + ListEnvironmentsResponse, + Environment, + ListEnvironmentsPage, + ListEnvironmentsFixedSizeCollection> { + + private ListEnvironmentsFixedSizeCollection( + List pages, int collectionSize) { + super(pages, collectionSize); + } + + private static ListEnvironmentsFixedSizeCollection createEmptyCollection() { + return new ListEnvironmentsFixedSizeCollection(null, 0); + } + + @Override + protected ListEnvironmentsFixedSizeCollection createCollection( + List pages, int collectionSize) { + return new ListEnvironmentsFixedSizeCollection(pages, collectionSize); + } + } } diff --git a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsSettings.java b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsSettings.java index 1e8ce8e28..ba62d3164 100644 --- a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsSettings.java +++ b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsSettings.java @@ -15,6 +15,8 @@ */ package com.google.cloud.dialogflow.v2beta1; +import static com.google.cloud.dialogflow.v2beta1.EnvironmentsClient.ListEnvironmentsPagedResponse; + import com.google.api.core.ApiFunction; import com.google.api.core.BetaApi; import com.google.api.gax.core.GoogleCredentialsProvider; @@ -23,6 +25,7 @@ import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.ClientContext; import com.google.api.gax.rpc.ClientSettings; +import com.google.api.gax.rpc.PagedCallSettings; import com.google.api.gax.rpc.TransportChannelProvider; import com.google.api.gax.rpc.UnaryCallSettings; import com.google.cloud.dialogflow.v2beta1.stub.EnvironmentsStubSettings; @@ -65,7 +68,8 @@ @BetaApi public class EnvironmentsSettings extends ClientSettings { /** Returns the object with the settings used for calls to listEnvironments. */ - public UnaryCallSettings + public PagedCallSettings< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse> listEnvironmentsSettings() { return ((EnvironmentsStubSettings) getStubSettings()).listEnvironmentsSettings(); } @@ -168,7 +172,8 @@ public Builder applyToAllUnaryMethods( } /** Returns the builder for the settings used for calls to listEnvironments. */ - public UnaryCallSettings.Builder + public PagedCallSettings.Builder< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse> listEnvironmentsSettings() { return getStubSettingsBuilder().listEnvironmentsSettings(); } diff --git a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/package-info.java b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/package-info.java index 4ae803685..ab0e5f0e9 100644 --- a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/package-info.java +++ b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/package-info.java @@ -149,8 +149,8 @@ *
  * 
  * try (EnvironmentsClient environmentsClient = EnvironmentsClient.create()) {
- *   ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder().build();
- *   ListEnvironmentsResponse response = environmentsClient.listEnvironments(request);
+ *   ProjectAgentName parent = ProjectAgentName.of("[PROJECT]");
+ *   ListEnvironmentsPagedResponse response = environmentsClient.listEnvironments(parent);
  * }
  * 
  * 
diff --git a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStub.java b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStub.java index b6441f17f..fd6fd7889 100644 --- a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStub.java +++ b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStub.java @@ -15,6 +15,8 @@ */ package com.google.cloud.dialogflow.v2beta1.stub; +import static com.google.cloud.dialogflow.v2beta1.EnvironmentsClient.ListEnvironmentsPagedResponse; + import com.google.api.core.BetaApi; import com.google.api.gax.core.BackgroundResource; import com.google.api.gax.rpc.UnaryCallable; @@ -32,6 +34,11 @@ @BetaApi("A restructuring of stub classes is planned, so this may break in the future") public abstract class EnvironmentsStub implements BackgroundResource { + public UnaryCallable + listEnvironmentsPagedCallable() { + throw new UnsupportedOperationException("Not implemented: listEnvironmentsPagedCallable()"); + } + public UnaryCallable listEnvironmentsCallable() { throw new UnsupportedOperationException("Not implemented: listEnvironmentsCallable()"); diff --git a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStubSettings.java b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStubSettings.java index 77bb95264..3b2b8ff2e 100644 --- a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStubSettings.java +++ b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/EnvironmentsStubSettings.java @@ -15,7 +15,10 @@ */ package com.google.cloud.dialogflow.v2beta1.stub; +import static com.google.cloud.dialogflow.v2beta1.EnvironmentsClient.ListEnvironmentsPagedResponse; + import com.google.api.core.ApiFunction; +import com.google.api.core.ApiFuture; import com.google.api.core.BetaApi; import com.google.api.gax.core.GaxProperties; import com.google.api.gax.core.GoogleCredentialsProvider; @@ -24,12 +27,19 @@ import com.google.api.gax.grpc.GrpcTransportChannel; import com.google.api.gax.grpc.InstantiatingGrpcChannelProvider; import com.google.api.gax.retrying.RetrySettings; +import com.google.api.gax.rpc.ApiCallContext; import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.ClientContext; +import com.google.api.gax.rpc.PageContext; +import com.google.api.gax.rpc.PagedCallSettings; +import com.google.api.gax.rpc.PagedListDescriptor; +import com.google.api.gax.rpc.PagedListResponseFactory; import com.google.api.gax.rpc.StatusCode; import com.google.api.gax.rpc.StubSettings; import com.google.api.gax.rpc.TransportChannelProvider; import com.google.api.gax.rpc.UnaryCallSettings; +import com.google.api.gax.rpc.UnaryCallable; +import com.google.cloud.dialogflow.v2beta1.Environment; import com.google.cloud.dialogflow.v2beta1.ListEnvironmentsRequest; import com.google.cloud.dialogflow.v2beta1.ListEnvironmentsResponse; import com.google.common.collect.ImmutableList; @@ -82,11 +92,13 @@ public class EnvironmentsStubSettings extends StubSettings + private final PagedCallSettings< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse> listEnvironmentsSettings; /** Returns the object with the settings used for calls to listEnvironments. */ - public UnaryCallSettings + public PagedCallSettings< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse> listEnvironmentsSettings() { return listEnvironmentsSettings; } @@ -163,11 +175,71 @@ protected EnvironmentsStubSettings(Builder settingsBuilder) throws IOException { listEnvironmentsSettings = settingsBuilder.listEnvironmentsSettings().build(); } + private static final PagedListDescriptor< + ListEnvironmentsRequest, ListEnvironmentsResponse, Environment> + LIST_ENVIRONMENTS_PAGE_STR_DESC = + new PagedListDescriptor< + ListEnvironmentsRequest, ListEnvironmentsResponse, Environment>() { + @Override + public String emptyToken() { + return ""; + } + + @Override + public ListEnvironmentsRequest injectToken( + ListEnvironmentsRequest payload, String token) { + return ListEnvironmentsRequest.newBuilder(payload).setPageToken(token).build(); + } + + @Override + public ListEnvironmentsRequest injectPageSize( + ListEnvironmentsRequest payload, int pageSize) { + return ListEnvironmentsRequest.newBuilder(payload).setPageSize(pageSize).build(); + } + + @Override + public Integer extractPageSize(ListEnvironmentsRequest payload) { + return payload.getPageSize(); + } + + @Override + public String extractNextToken(ListEnvironmentsResponse payload) { + return payload.getNextPageToken(); + } + + @Override + public Iterable extractResources(ListEnvironmentsResponse payload) { + return payload.getEnvironmentsList() != null + ? payload.getEnvironmentsList() + : ImmutableList.of(); + } + }; + + private static final PagedListResponseFactory< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse> + LIST_ENVIRONMENTS_PAGE_STR_FACT = + new PagedListResponseFactory< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse>() { + @Override + public ApiFuture getFuturePagedResponse( + UnaryCallable callable, + ListEnvironmentsRequest request, + ApiCallContext context, + ApiFuture futureResponse) { + PageContext + pageContext = + PageContext.create( + callable, LIST_ENVIRONMENTS_PAGE_STR_DESC, request, context); + return ListEnvironmentsPagedResponse.createAsync(pageContext, futureResponse); + } + }; + /** Builder for EnvironmentsStubSettings. */ public static class Builder extends StubSettings.Builder { private final ImmutableList> unaryMethodSettingsBuilders; - private final UnaryCallSettings.Builder + private final PagedCallSettings.Builder< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse> listEnvironmentsSettings; private static final ImmutableMap> @@ -180,7 +252,7 @@ public static class Builder extends StubSettings.BuildernewArrayList( - StatusCode.Code.DEADLINE_EXCEEDED, StatusCode.Code.UNAVAILABLE))); + StatusCode.Code.INTERNAL, StatusCode.Code.UNAVAILABLE))); definitions.put("non_idempotent", ImmutableSet.copyOf(Lists.newArrayList())); RETRYABLE_CODE_DEFINITIONS = definitions.build(); } @@ -211,7 +283,7 @@ protected Builder() { protected Builder(ClientContext clientContext) { super(clientContext); - listEnvironmentsSettings = UnaryCallSettings.newUnaryCallSettingsBuilder(); + listEnvironmentsSettings = PagedCallSettings.newBuilder(LIST_ENVIRONMENTS_PAGE_STR_FACT); unaryMethodSettingsBuilders = ImmutableList.>of(listEnvironmentsSettings); @@ -264,7 +336,8 @@ public Builder applyToAllUnaryMethods( } /** Returns the builder for the settings used for calls to listEnvironments. */ - public UnaryCallSettings.Builder + public PagedCallSettings.Builder< + ListEnvironmentsRequest, ListEnvironmentsResponse, ListEnvironmentsPagedResponse> listEnvironmentsSettings() { return listEnvironmentsSettings; } diff --git a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/GrpcEnvironmentsStub.java b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/GrpcEnvironmentsStub.java index 0651ec3cb..641b78748 100644 --- a/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/GrpcEnvironmentsStub.java +++ b/google-cloud-dialogflow/src/main/java/com/google/cloud/dialogflow/v2beta1/stub/GrpcEnvironmentsStub.java @@ -15,6 +15,8 @@ */ package com.google.cloud.dialogflow.v2beta1.stub; +import static com.google.cloud.dialogflow.v2beta1.EnvironmentsClient.ListEnvironmentsPagedResponse; + import com.google.api.core.BetaApi; import com.google.api.gax.core.BackgroundResource; import com.google.api.gax.core.BackgroundResourceAggregation; @@ -58,6 +60,8 @@ public class GrpcEnvironmentsStub extends EnvironmentsStub { private final UnaryCallable listEnvironmentsCallable; + private final UnaryCallable + listEnvironmentsPagedCallable; private final GrpcStubCallableFactory callableFactory; @@ -116,10 +120,18 @@ public Map extract(ListEnvironmentsRequest request) { this.listEnvironmentsCallable = callableFactory.createUnaryCallable( listEnvironmentsTransportSettings, settings.listEnvironmentsSettings(), clientContext); + this.listEnvironmentsPagedCallable = + callableFactory.createPagedCallable( + listEnvironmentsTransportSettings, settings.listEnvironmentsSettings(), clientContext); backgroundResources = new BackgroundResourceAggregation(clientContext.getBackgroundResources()); } + public UnaryCallable + listEnvironmentsPagedCallable() { + return listEnvironmentsPagedCallable; + } + public UnaryCallable listEnvironmentsCallable() { return listEnvironmentsCallable; diff --git a/google-cloud-dialogflow/src/test/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClientTest.java b/google-cloud-dialogflow/src/test/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClientTest.java index 199c3ca4d..e193df425 100644 --- a/google-cloud-dialogflow/src/test/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClientTest.java +++ b/google-cloud-dialogflow/src/test/java/com/google/cloud/dialogflow/v2beta1/EnvironmentsClientTest.java @@ -15,6 +15,8 @@ */ package com.google.cloud.dialogflow.v2beta1; +import static com.google.cloud.dialogflow.v2beta1.EnvironmentsClient.ListEnvironmentsPagedResponse; + import com.google.api.gax.core.NoCredentialsProvider; import com.google.api.gax.grpc.GaxGrpcProperties; import com.google.api.gax.grpc.testing.LocalChannelProvider; @@ -22,6 +24,7 @@ import com.google.api.gax.grpc.testing.MockServiceHelper; import com.google.api.gax.rpc.ApiClientHeaderProvider; import com.google.api.gax.rpc.InvalidArgumentException; +import com.google.common.collect.Lists; import com.google.protobuf.AbstractMessage; import io.grpc.Status; import io.grpc.StatusRuntimeException; @@ -103,20 +106,29 @@ public void tearDown() throws Exception { @Test @SuppressWarnings("all") public void listEnvironmentsTest() { - String nextPageToken = "nextPageToken-1530815211"; + String nextPageToken = ""; + Environment environmentsElement = Environment.newBuilder().build(); + List environments = Arrays.asList(environmentsElement); ListEnvironmentsResponse expectedResponse = - ListEnvironmentsResponse.newBuilder().setNextPageToken(nextPageToken).build(); + ListEnvironmentsResponse.newBuilder() + .setNextPageToken(nextPageToken) + .addAllEnvironments(environments) + .build(); mockEnvironments.addResponse(expectedResponse); - ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder().build(); + ProjectAgentName parent = ProjectAgentName.of("[PROJECT]"); + + ListEnvironmentsPagedResponse pagedListResponse = client.listEnvironments(parent); - ListEnvironmentsResponse actualResponse = client.listEnvironments(request); - Assert.assertEquals(expectedResponse, actualResponse); + List resources = Lists.newArrayList(pagedListResponse.iterateAll()); + Assert.assertEquals(1, resources.size()); + Assert.assertEquals(expectedResponse.getEnvironmentsList().get(0), resources.get(0)); List actualRequests = mockEnvironments.getRequests(); Assert.assertEquals(1, actualRequests.size()); ListEnvironmentsRequest actualRequest = (ListEnvironmentsRequest) actualRequests.get(0); + Assert.assertEquals(parent, ProjectAgentName.parse(actualRequest.getParent())); Assert.assertTrue( channelProvider.isHeaderSent( ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), @@ -130,9 +142,9 @@ public void listEnvironmentsExceptionTest() throws Exception { mockEnvironments.addException(exception); try { - ListEnvironmentsRequest request = ListEnvironmentsRequest.newBuilder().build(); + ProjectAgentName parent = ProjectAgentName.of("[PROJECT]"); - client.listEnvironments(request); + client.listEnvironments(parent); Assert.fail("No exception raised"); } catch (InvalidArgumentException e) { // Expected exception diff --git a/proto-google-cloud-dialogflow-v2beta1/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentName.java b/proto-google-cloud-dialogflow-v2beta1/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentName.java new file mode 100644 index 000000000..28b40a1d2 --- /dev/null +++ b/proto-google-cloud-dialogflow-v2beta1/src/main/java/com/google/cloud/dialogflow/v2beta1/EnvironmentName.java @@ -0,0 +1,182 @@ +/* + * Copyright 2020 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.dialogflow.v2beta1; + +import com.google.api.pathtemplate.PathTemplate; +import com.google.api.resourcenames.ResourceName; +import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableMap; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +/** AUTO-GENERATED DOCUMENTATION AND CLASS */ +@javax.annotation.Generated("by GAPIC protoc plugin") +public class EnvironmentName implements ResourceName { + + private static final PathTemplate PATH_TEMPLATE = + PathTemplate.createWithoutUrlEncoding("projects/{project}/agent/environments/{environment}"); + + private volatile Map fieldValuesMap; + + private final String project; + private final String environment; + + public String getProject() { + return project; + } + + public String getEnvironment() { + return environment; + } + + public static Builder newBuilder() { + return new Builder(); + } + + public Builder toBuilder() { + return new Builder(this); + } + + private EnvironmentName(Builder builder) { + project = Preconditions.checkNotNull(builder.getProject()); + environment = Preconditions.checkNotNull(builder.getEnvironment()); + } + + public static EnvironmentName of(String project, String environment) { + return newBuilder().setProject(project).setEnvironment(environment).build(); + } + + public static String format(String project, String environment) { + return newBuilder().setProject(project).setEnvironment(environment).build().toString(); + } + + public static EnvironmentName parse(String formattedString) { + if (formattedString.isEmpty()) { + return null; + } + Map matchMap = + PATH_TEMPLATE.validatedMatch( + formattedString, "EnvironmentName.parse: formattedString not in valid format"); + return of(matchMap.get("project"), matchMap.get("environment")); + } + + public static List parseList(List formattedStrings) { + List list = new ArrayList<>(formattedStrings.size()); + for (String formattedString : formattedStrings) { + list.add(parse(formattedString)); + } + return list; + } + + public static List toStringList(List values) { + List list = new ArrayList(values.size()); + for (EnvironmentName value : values) { + if (value == null) { + list.add(""); + } else { + list.add(value.toString()); + } + } + return list; + } + + public static boolean isParsableFrom(String formattedString) { + return PATH_TEMPLATE.matches(formattedString); + } + + public Map getFieldValuesMap() { + if (fieldValuesMap == null) { + synchronized (this) { + if (fieldValuesMap == null) { + ImmutableMap.Builder fieldMapBuilder = ImmutableMap.builder(); + fieldMapBuilder.put("project", project); + fieldMapBuilder.put("environment", environment); + fieldValuesMap = fieldMapBuilder.build(); + } + } + } + return fieldValuesMap; + } + + public String getFieldValue(String fieldName) { + return getFieldValuesMap().get(fieldName); + } + + @Override + public String toString() { + return PATH_TEMPLATE.instantiate("project", project, "environment", environment); + } + + /** Builder for EnvironmentName. */ + public static class Builder { + + private String project; + private String environment; + + public String getProject() { + return project; + } + + public String getEnvironment() { + return environment; + } + + public Builder setProject(String project) { + this.project = project; + return this; + } + + public Builder setEnvironment(String environment) { + this.environment = environment; + return this; + } + + private Builder() {} + + private Builder(EnvironmentName environmentName) { + project = environmentName.project; + environment = environmentName.environment; + } + + public EnvironmentName build() { + return new EnvironmentName(this); + } + } + + @Override + public boolean equals(Object o) { + if (o == this) { + return true; + } + if (o instanceof EnvironmentName) { + EnvironmentName that = (EnvironmentName) o; + return (this.project.equals(that.project)) && (this.environment.equals(that.environment)); + } + return false; + } + + @Override + public int hashCode() { + int h = 1; + h *= 1000003; + h ^= project.hashCode(); + h *= 1000003; + h ^= environment.hashCode(); + return h; + } +} diff --git a/synth.metadata b/synth.metadata index b1e4fd9e9..2fa9f53a7 100644 --- a/synth.metadata +++ b/synth.metadata @@ -4,23 +4,23 @@ "git": { "name": ".", "remote": "https://github.com/googleapis/java-dialogflow.git", - "sha": "26a4fc41fd364fa0031317367ca34577ba24ee8d" + "sha": "17fd213b88123f7467ed794d640d651b11f3e10d" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "28e76243c23cc282efbb288cb558c174e3e5e9ee", - "internalRef": "308294748" + "sha": "c284e4f849e4b085a297d336cef8721ab69ba013", + "internalRef": "308749074" } }, { "git": { "name": "googleapis", "remote": "https://github.com/googleapis/googleapis.git", - "sha": "28e76243c23cc282efbb288cb558c174e3e5e9ee", - "internalRef": "308294748" + "sha": "c284e4f849e4b085a297d336cef8721ab69ba013", + "internalRef": "308749074" } }, {