-
Notifications
You must be signed in to change notification settings - Fork 69
[compose][samplecode][1/2]Implement sample code of set credentials in Service Client Class header comment #493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
summer-ji-eng
merged 16 commits into
master
from
service_client_credential_sample_code1
Nov 18, 2020
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
de732d4
Implement sample code of customize credentials in service client clas…
summer-ji-eng e5df14b
Add license
summer-ji-eng 81d71db
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng e214824
Add comments in composer, add TODOs, refactor the signatures
summer-ji-eng b9f8024
format the files
summer-ji-eng 85b6f27
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng 2ae0cce
refactor signature
summer-ji-eng e390c07
Merge branch 'service_client_credential_sample_code1' of github.com:g…
summer-ji-eng cc843c6
simplify the input parameters
summer-ji-eng aa87b09
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng d164c52
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng 0a5e0e5
Merge branch 'master' into service_client_credential_sample_code1
summer-ji-eng 0bca282
use utils samplecodewriter
summer-ji-eng 2f75476
use samplecodewriter
summer-ji-eng 9954c1b
remove unused two string variables
summer-ji-eng 74f65e7
remove unused methods
summer-ji-eng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/main/java/com/google/api/generator/gapic/composer/ServiceClientSampleCodeComposer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,110 @@ | ||
| // 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 | ||
| // | ||
| // http://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.api.generator.gapic.composer; | ||
|
|
||
| import com.google.api.gax.core.FixedCredentialsProvider; | ||
| import com.google.api.generator.engine.ast.AssignmentExpr; | ||
| import com.google.api.generator.engine.ast.ConcreteReference; | ||
| import com.google.api.generator.engine.ast.Expr; | ||
| import com.google.api.generator.engine.ast.ExprStatement; | ||
| import com.google.api.generator.engine.ast.MethodInvocationExpr; | ||
| import com.google.api.generator.engine.ast.Statement; | ||
| import com.google.api.generator.engine.ast.StringObjectValue; | ||
| import com.google.api.generator.engine.ast.TypeNode; | ||
| import com.google.api.generator.engine.ast.ValueExpr; | ||
| import com.google.api.generator.engine.ast.Variable; | ||
| import com.google.api.generator.engine.ast.VariableExpr; | ||
| import com.google.api.generator.engine.writer.JavaWriterVisitor; | ||
| import com.google.api.generator.gapic.composer.samplecode.SampleCodeJavaFormatter; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ServiceClientSampleCodeComposer { | ||
| // TODO(summerji): Add unit tests for ServiceClientSampleCodeComposer. | ||
|
|
||
| public static String composeClassHeaderCredentialsSampleCode( | ||
| String clientName, TypeNode clientType, String settingsName, TypeNode settingsType) { | ||
| // Initialize clientSettings with builder() method. | ||
| // e.g. EchoSettings echoSettings = | ||
| // EchoSettings.newBuilder().setCredentialsProvider(FixedCredentialsProvider.create("myCredentials")).build(); | ||
| VariableExpr settingsVarExpr = createVariableExpr(settingsName, settingsType); | ||
| MethodInvocationExpr newBuilderMethodExpr = | ||
| MethodInvocationExpr.builder() | ||
| .setStaticReferenceType(settingsType) | ||
| .setMethodName("newBuilder") | ||
| .build(); | ||
| TypeNode fixedCredentialProvideType = | ||
| TypeNode.withReference(ConcreteReference.withClazz(FixedCredentialsProvider.class)); | ||
| MethodInvocationExpr credentialArgExpr = | ||
| MethodInvocationExpr.builder() | ||
| .setStaticReferenceType(fixedCredentialProvideType) | ||
| .setArguments(ValueExpr.withValue(StringObjectValue.withValue("myCredentials"))) | ||
| .setMethodName("create") | ||
| .build(); | ||
| MethodInvocationExpr credentialsMethodExpr = | ||
| MethodInvocationExpr.builder() | ||
| .setExprReferenceExpr(newBuilderMethodExpr) | ||
| .setArguments(credentialArgExpr) | ||
| .setMethodName("setCredentialsProvider") | ||
| .build(); | ||
| MethodInvocationExpr buildMethodExpr = | ||
| MethodInvocationExpr.builder() | ||
| .setExprReferenceExpr(credentialsMethodExpr) | ||
| .setReturnType(settingsType) | ||
| .setMethodName("build") | ||
| .build(); | ||
| Expr initSettingsVarExpr = | ||
| AssignmentExpr.builder() | ||
| .setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()) | ||
| .setValueExpr(buildMethodExpr) | ||
| .build(); | ||
|
|
||
| // Initialized client with create() method. | ||
| // e.g. EchoClient echoClient = EchoClient.create(echoSettings); | ||
| VariableExpr clientVarExpr = createVariableExpr(clientName, clientType); | ||
| MethodInvocationExpr createMethodExpr = | ||
| MethodInvocationExpr.builder() | ||
| .setStaticReferenceType(clientType) | ||
| .setArguments(settingsVarExpr) | ||
| .setMethodName("create") | ||
| .setReturnType(clientType) | ||
| .build(); | ||
| Expr initClientVarExpr = | ||
| AssignmentExpr.builder() | ||
| .setVariableExpr(clientVarExpr.toBuilder().setIsDecl(true).build()) | ||
| .setValueExpr(createMethodExpr) | ||
| .build(); | ||
|
|
||
| return writeSampleCode(Arrays.asList(initSettingsVarExpr, initClientVarExpr)); | ||
| } | ||
|
|
||
| // ======================================== Helpers ==========================================// | ||
| // TODO(summerji): Use writeSampleCode method in new class once PR#499 merged. | ||
| private static String writeSampleCode(List<Expr> exprs) { | ||
summer-ji-eng marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| List<Statement> statements = | ||
| exprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()); | ||
| JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
| for (Statement statement : statements) { | ||
| statement.accept(visitor); | ||
| } | ||
| return SampleCodeJavaFormatter.format(visitor.write()); | ||
| } | ||
|
|
||
| private static VariableExpr createVariableExpr(String variableName, TypeNode type) { | ||
miraleung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return VariableExpr.withVariable( | ||
| Variable.builder().setName(variableName).setType(type).build()); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,6 +53,14 @@ import javax.annotation.Generated; | |
| * | ||
| * <p>To customize credentials: | ||
| * | ||
| * <pre>{@code | ||
| * IdentitySettings identitySettings = | ||
| * IdentitySettings.newBuilder() | ||
| * .setCredentialsProvider(FixedCredentialsProvider.create("myCredentials")) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just noticed this - this should be some variable, not a string. Example here |
||
| * .build(); | ||
| * IdentityClient identityClient = IdentityClient.create(identitySettings); | ||
| * }</pre> | ||
| * | ||
| * <p>To customize the endpoint: | ||
| */ | ||
| @BetaApi | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.