Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.google.api.generator.engine.ast.JavaDocComment;
import com.google.api.generator.engine.ast.PackageInfoDefinition;
import com.google.api.generator.engine.ast.TypeNode;
import com.google.api.generator.engine.ast.VaporReference;
import com.google.api.generator.gapic.composer.samplecode.ServiceClientSampleCodeComposer;
import com.google.api.generator.gapic.composer.utils.ClassNames;
import com.google.api.generator.gapic.model.GapicContext;
import com.google.api.generator.gapic.model.GapicPackageInfo;
Expand Down Expand Up @@ -111,7 +113,16 @@ private static CommentStatement createPackageInfoJavadoc(GapicContext context) {
javaDocCommentBuilder =
javaDocCommentBuilder.addParagraph(
String.format(SAMPLE_CODE_HEADER_PATTERN, javaClientName));
// TODO(summerji): Add package-info.java sample code here.
TypeNode clientType =
TypeNode.withReference(
VaporReference.builder()
.setPakkage(service.pakkage())
.setName(ClassNames.getServiceClientClassName(service))
.build());
String packageInfoSampleCode =
ServiceClientSampleCodeComposer.composeClassHeaderMethodSampleCode(
service, clientType, context.resourceNames(), context.messages());
javaDocCommentBuilder.addSampleCode(packageInfoSampleCode);
}

return CommentStatement.withComment(javaDocCommentBuilder.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,8 @@ public GapicClass generate(

ClassDefinition classDef =
ClassDefinition.builder()
.setHeaderCommentStatements(createClassHeaderComments(service, typeStore))
.setHeaderCommentStatements(
createClassHeaderComments(service, typeStore, resourceNames, messageTypes))
.setPackageString(pakkage)
.setAnnotations(createClassAnnotations(pakkage, typeStore))
.setScope(ScopeNode.PUBLIC)
Expand Down Expand Up @@ -161,17 +162,23 @@ private static List<TypeNode> createClassImplements(TypeStore typeStore) {
}

private static List<CommentStatement> createClassHeaderComments(
Service service, TypeStore typeStore) {
Service service,
TypeStore typeStore,
Map<String, ResourceName> resourceNames,
Map<String, Message> messageTypes) {
TypeNode clientType = typeStore.get(ClassNames.getServiceClientClassName(service));
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
String classMethodSampleCode =
ServiceClientSampleCodeComposer.composeClassHeaderMethodSampleCode(
service, clientType, resourceNames, messageTypes);
String credentialsSampleCode =
ServiceClientSampleCodeComposer.composeClassHeaderCredentialsSampleCode(
clientType, settingsType);
String endpointSampleCode =
ServiceClientSampleCodeComposer.composeClassHeaderEndpointSampleCode(
clientType, settingsType);
return ServiceClientCommentComposer.createClassHeaderComments(
service, credentialsSampleCode, endpointSampleCode);
service, classMethodSampleCode, credentialsSampleCode, endpointSampleCode);
}

private static List<MethodDefinition> createClassMethods(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ public class ServiceClientCommentComposer {
+ " operation returned by another API method call.");

public static List<CommentStatement> createClassHeaderComments(
Service service, String credentialsSampleCode, String endpointSampleCode) {
Service service,
String classMethodSampleCode,
String credentialsSampleCode,
String endpointSampleCode) {
JavaDocComment.Builder classHeaderJavadocBuilder = JavaDocComment.builder();
if (service.hasDescription()) {
classHeaderJavadocBuilder =
Expand All @@ -122,7 +125,7 @@ public static List<CommentStatement> createClassHeaderComments(

// Service introduction.
classHeaderJavadocBuilder.addParagraph(SERVICE_DESCRIPTION_INTRO_STRING);
// TODO(summerji): Add sample code here.
classHeaderJavadocBuilder.addSampleCode(classMethodSampleCode);

// API surface description.
classHeaderJavadocBuilder.addParagraph(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import com.google.api.generator.gapic.model.Method.Stream;
import com.google.api.generator.gapic.model.MethodArgument;
import com.google.api.generator.gapic.model.ResourceName;
import com.google.api.generator.gapic.model.Service;
import com.google.api.generator.gapic.utils.JavaStyle;
import com.google.common.base.Preconditions;
import java.util.ArrayList;
Expand All @@ -56,8 +57,31 @@

public class ServiceClientSampleCodeComposer {

public static String composeClassHeaderMethodSampleCode(
Service service,
TypeNode clientType,
Map<String, ResourceName> resourceNames,
Map<String, Message> messageTypes) {
// Use the first pure unary RPC method's sample code as showcase, if no such method exists, use
// the first method in the service's methods list.
Method method =
service.methods().stream()
.filter(m -> m.stream() == Stream.NONE && !m.hasLro() && !m.isPaged())
.findFirst()
.orElse(service.methods().get(0));
if (method.stream() == Stream.NONE) {
if (method.methodSignatures().isEmpty()) {
return composeRpcDefaultMethodHeaderSampleCode(
method, clientType, resourceNames, messageTypes);
}
return composeRpcMethodHeaderSampleCode(
method, clientType, method.methodSignatures().get(0), resourceNames, messageTypes);
}
return composeStreamCallableMethodHeaderSampleCode(
method, clientType, resourceNames, messageTypes);
}

public static String composeClassHeaderCredentialsSampleCode(
// TODO(summerji): Add unit tests for composeClassHeaderCredentialsSampleCode.
TypeNode clientType, TypeNode settingsType) {
// Initialize clientSettings with builder() method.
// e.g. EchoSettings echoSettings =
Expand Down Expand Up @@ -130,7 +154,6 @@ public static String composeClassHeaderCredentialsSampleCode(
}

public static String composeClassHeaderEndpointSampleCode(
// TODO(summerji): Add unit tests for composeClassHeaderEndpointSampleCode.
TypeNode clientType, TypeNode settingsType) {
// Initialize client settings with builder() method.
// e.g. EchoSettings echoSettings = EchoSettings.newBuilder().setEndpoint("myEndpoint").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ import javax.annotation.Generated;
* This class provides the ability to make remote calls to the backing service through method calls
* that map to API methods. Sample code to get started:
*
* <pre>{@code
* try (EchoClient echoClient = EchoClient.create()) {
* EchoResponse response = echoClient.echo();
* }
* }</pre>
*
* <p>Note: close() needs to be called on the EchoClient object to clean up resources such as
* threads. In the example above, try-with-resources is used, which automatically calls close().
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ import javax.annotation.Generated;
* This class provides the ability to make remote calls to the backing service through method calls
* that map to API methods. Sample code to get started:
*
* <pre>{@code
* try (IdentityClient identityClient = IdentityClient.create()) {
* String parent = UserName.of("[USER]").toString();
* String displayName = "displayName1714148973";
* String email = "email96619420";
* User response = identityClient.createUser(parent, displayName, email);
* }
* }</pre>
*
* <p>Note: close() needs to be called on the IdentityClient object to clean up resources such as
* threads. In the example above, try-with-resources is used, which automatically calls close().
*
Expand Down
Loading