Skip to content
This repository was archived by the owner on May 14, 2026. It is now read-only.

Commit 16ad6de

Browse files
committed
[samplecode][1/3]Implement Pure Unary RPC sample code (#573)
1 parent 53153cf commit 16ad6de

2 files changed

Lines changed: 19 additions & 96 deletions

File tree

src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,12 @@ private static List<MethodDefinition> createMethodVariants(
619619
return javaMethods;
620620
}
621621

622-
private static MethodDefinition createMethodDefaultMethod(Method method, TypeStore typeStore) {
622+
private static MethodDefinition createMethodDefaultMethod(
623+
Method method,
624+
String clientName,
625+
Map<String, Message> messageTypes,
626+
TypeStore typeStore,
627+
Map<String, ResourceName> resourceNames) {
623628
String methodName = JavaStyle.toLowerCamelCase(method.name());
624629
TypeNode methodInputType = method.inputType();
625630
TypeNode methodOutputType =
@@ -652,6 +657,11 @@ private static MethodDefinition createMethodDefaultMethod(Method method, TypeSto
652657
callableMethodName = String.format(OPERATION_CALLABLE_NAME_PATTERN, methodName);
653658
}
654659

660+
Optional<String> defaultMethodSampleCode =
661+
Optional.of(
662+
ServiceClientSampleCodeComposer.composeRpcDefaultMethodHeaderSampleCode(
663+
method, typeStore.get(clientName), resourceNames, messageTypes));
664+
655665
MethodInvocationExpr callableMethodExpr =
656666
MethodInvocationExpr.builder().setMethodName(callableMethodName).build();
657667
callableMethodExpr =
@@ -664,7 +674,8 @@ private static MethodDefinition createMethodDefaultMethod(Method method, TypeSto
664674
MethodDefinition.Builder methodBuilder =
665675
MethodDefinition.builder()
666676
.setHeaderCommentStatements(
667-
ServiceClientCommentComposer.createRpcMethodHeaderComment(method))
677+
ServiceClientCommentComposer.createRpcMethodHeaderComment(
678+
method, defaultMethodSampleCode))
668679
.setScope(ScopeNode.PUBLIC)
669680
.setIsFinal(true)
670681
.setName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName))

test/integration/goldens/logging/MetricsClient.java

Lines changed: 6 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -151,17 +151,6 @@ public MetricsServiceV2Stub getStub() {
151151
/**
152152
* Lists logs-based metrics.
153153
*
154-
* <p>Sample code:
155-
*
156-
* <pre>{@code
157-
* try (MetricsClient metricsClient = MetricsClient.create()) {
158-
* ProjectName parent = ProjectName.of("[PROJECT]");
159-
* for (LogMetric element : metricsClient.listLogMetrics(parent).iterateAll()) {
160-
* // doThingsWith(element);
161-
* }
162-
* }
163-
* }</pre>
164-
*
165154
* @param parent Required. The name of the project containing the metrics:
166155
* <p>"projects/[PROJECT_ID]"
167156
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
@@ -178,17 +167,6 @@ public final ListLogMetricsPagedResponse listLogMetrics(ProjectName parent) {
178167
/**
179168
* Lists logs-based metrics.
180169
*
181-
* <p>Sample code:
182-
*
183-
* <pre>{@code
184-
* try (MetricsClient metricsClient = MetricsClient.create()) {
185-
* String parent = ProjectName.of("[PROJECT]").toString();
186-
* for (LogMetric element : metricsClient.listLogMetrics(parent).iterateAll()) {
187-
* // doThingsWith(element);
188-
* }
189-
* }
190-
* }</pre>
191-
*
192170
* @param parent Required. The name of the project containing the metrics:
193171
* <p>"projects/[PROJECT_ID]"
194172
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
@@ -202,22 +180,6 @@ public final ListLogMetricsPagedResponse listLogMetrics(String parent) {
202180
/**
203181
* Lists logs-based metrics.
204182
*
205-
* <p>Sample code:
206-
*
207-
* <pre>{@code
208-
* try (MetricsClient metricsClient = MetricsClient.create()) {
209-
* ListLogMetricsRequest request =
210-
* ListLogMetricsRequest.newBuilder()
211-
* .setParent(ProjectName.of("[PROJECT]").toString())
212-
* .setPageToken("pageToken873572522")
213-
* .setPageSize(883849137)
214-
* .build();
215-
* for (LogMetric element : metricsClient.listLogMetrics(request).iterateAll()) {
216-
* // doThingsWith(element);
217-
* }
218-
* }
219-
* }</pre>
220-
*
221183
* @param request The request object containing all of the parameters for the API call.
222184
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
223185
*/
@@ -260,7 +222,7 @@ public final ListLogMetricsPagedResponse listLogMetrics(ListLogMetricsRequest re
260222
* }
261223
* }</pre>
262224
*
263-
* @param metricName Required. The resource name of the desired metric:
225+
* @param metric_name Required. The resource name of the desired metric:
264226
* <p>"projects/[PROJECT_ID]/metrics/[METRIC_ID]"
265227
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
266228
*/
@@ -285,7 +247,7 @@ public final LogMetric getLogMetric(LogMetricName metricName) {
285247
* }
286248
* }</pre>
287249
*
288-
* @param metricName Required. The resource name of the desired metric:
250+
* @param metric_name Required. The resource name of the desired metric:
289251
* <p>"projects/[PROJECT_ID]/metrics/[METRIC_ID]"
290252
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
291253
*/
@@ -299,18 +261,6 @@ public final LogMetric getLogMetric(String metricName) {
299261
/**
300262
* Gets a logs-based metric.
301263
*
302-
* <p>Sample code:
303-
*
304-
* <pre>{@code
305-
* try (MetricsClient metricsClient = MetricsClient.create()) {
306-
* GetLogMetricRequest request =
307-
* GetLogMetricRequest.newBuilder()
308-
* .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
309-
* .build();
310-
* LogMetric response = metricsClient.getLogMetric(request);
311-
* }
312-
* }</pre>
313-
*
314264
* @param request The request object containing all of the parameters for the API call.
315265
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
316266
*/
@@ -389,19 +339,6 @@ public final LogMetric createLogMetric(String parent, LogMetric metric) {
389339
/**
390340
* Creates a logs-based metric.
391341
*
392-
* <p>Sample code:
393-
*
394-
* <pre>{@code
395-
* try (MetricsClient metricsClient = MetricsClient.create()) {
396-
* CreateLogMetricRequest request =
397-
* CreateLogMetricRequest.newBuilder()
398-
* .setParent(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
399-
* .setMetric(LogMetric.newBuilder().build())
400-
* .build();
401-
* LogMetric response = metricsClient.createLogMetric(request);
402-
* }
403-
* }</pre>
404-
*
405342
* @param request The request object containing all of the parameters for the API call.
406343
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
407344
*/
@@ -433,7 +370,7 @@ public final UnaryCallable<CreateLogMetricRequest, LogMetric> createLogMetricCal
433370
* }
434371
* }</pre>
435372
*
436-
* @param metricName Required. The resource name of the metric to update:
373+
* @param metric_name Required. The resource name of the metric to update:
437374
* <p>"projects/[PROJECT_ID]/metrics/[METRIC_ID]"
438375
* <p>The updated metric must be provided in the request and it's `name` field must be the
439376
* same as `[METRIC_ID]` If the metric does not exist in `[PROJECT_ID]`, then a new metric is
@@ -464,7 +401,7 @@ public final LogMetric updateLogMetric(LogMetricName metricName, LogMetric metri
464401
* }
465402
* }</pre>
466403
*
467-
* @param metricName Required. The resource name of the metric to update:
404+
* @param metric_name Required. The resource name of the metric to update:
468405
* <p>"projects/[PROJECT_ID]/metrics/[METRIC_ID]"
469406
* <p>The updated metric must be provided in the request and it's `name` field must be the
470407
* same as `[METRIC_ID]` If the metric does not exist in `[PROJECT_ID]`, then a new metric is
@@ -482,19 +419,6 @@ public final LogMetric updateLogMetric(String metricName, LogMetric metric) {
482419
/**
483420
* Creates or updates a logs-based metric.
484421
*
485-
* <p>Sample code:
486-
*
487-
* <pre>{@code
488-
* try (MetricsClient metricsClient = MetricsClient.create()) {
489-
* UpdateLogMetricRequest request =
490-
* UpdateLogMetricRequest.newBuilder()
491-
* .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
492-
* .setMetric(LogMetric.newBuilder().build())
493-
* .build();
494-
* LogMetric response = metricsClient.updateLogMetric(request);
495-
* }
496-
* }</pre>
497-
*
498422
* @param request The request object containing all of the parameters for the API call.
499423
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
500424
*/
@@ -525,7 +449,7 @@ public final UnaryCallable<UpdateLogMetricRequest, LogMetric> updateLogMetricCal
525449
* }
526450
* }</pre>
527451
*
528-
* @param metricName Required. The resource name of the metric to delete:
452+
* @param metric_name Required. The resource name of the metric to delete:
529453
* <p>"projects/[PROJECT_ID]/metrics/[METRIC_ID]"
530454
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
531455
*/
@@ -550,7 +474,7 @@ public final void deleteLogMetric(LogMetricName metricName) {
550474
* }
551475
* }</pre>
552476
*
553-
* @param metricName Required. The resource name of the metric to delete:
477+
* @param metric_name Required. The resource name of the metric to delete:
554478
* <p>"projects/[PROJECT_ID]/metrics/[METRIC_ID]"
555479
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
556480
*/
@@ -564,18 +488,6 @@ public final void deleteLogMetric(String metricName) {
564488
/**
565489
* Deletes a logs-based metric.
566490
*
567-
* <p>Sample code:
568-
*
569-
* <pre>{@code
570-
* try (MetricsClient metricsClient = MetricsClient.create()) {
571-
* DeleteLogMetricRequest request =
572-
* DeleteLogMetricRequest.newBuilder()
573-
* .setMetricName(LogMetricName.of("[PROJECT]", "[METRIC]").toString())
574-
* .build();
575-
* metricsClient.deleteLogMetric(request);
576-
* }
577-
* }</pre>
578-
*
579491
* @param request The request object containing all of the parameters for the API call.
580492
* @throws com.google.api.gax.rpc.ApiException if the remote call fails
581493
*/

0 commit comments

Comments
 (0)