/
BaseClientBuilderInterface.java
194 lines (168 loc) · 9.51 KB
/
BaseClientBuilderInterface.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.codegen.poet.builder;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.CodeBlock;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.ParameterizedTypeName;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;
import com.squareup.javapoet.TypeVariableName;
import java.util.function.Consumer;
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.auth.token.credentials.SdkTokenProvider;
import software.amazon.awssdk.auth.token.credentials.aws.DefaultAwsTokenProvider;
import software.amazon.awssdk.auth.token.signer.aws.BearerTokenSigner;
import software.amazon.awssdk.awscore.client.builder.AwsClientBuilder;
import software.amazon.awssdk.codegen.internal.Utils;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.model.service.ClientContextParam;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.poet.rules.EndpointRulesSpecUtils;
import software.amazon.awssdk.codegen.utils.BearerAuthUtils;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.utils.internal.CodegenNamingUtils;
public class BaseClientBuilderInterface implements ClassSpec {
private final IntermediateModel model;
private final String basePackage;
private final ClassName builderInterfaceName;
private final EndpointRulesSpecUtils endpointRulesSpecUtils;
public BaseClientBuilderInterface(IntermediateModel model) {
this.model = model;
this.basePackage = model.getMetadata().getFullClientPackageName();
this.builderInterfaceName = ClassName.get(basePackage, model.getMetadata().getBaseBuilderInterface());
this.endpointRulesSpecUtils = new EndpointRulesSpecUtils(model);
}
@Override
public TypeSpec poetSpec() {
TypeSpec.Builder builder = PoetUtils.createInterfaceBuilder(builderInterfaceName)
.addTypeVariable(PoetUtils.createBoundedTypeVariableName("B", builderInterfaceName, "B", "C"))
.addTypeVariable(TypeVariableName.get("C"))
.addSuperinterface(PoetUtils.createParameterizedTypeName(AwsClientBuilder.class, "B", "C"))
.addJavadoc(getJavadoc());
if (model.getEndpointOperation().isPresent()) {
if (model.getCustomizationConfig().isEnableEndpointDiscoveryMethodRequired()) {
builder.addMethod(enableEndpointDiscovery());
}
builder.addMethod(endpointDiscovery());
}
if (model.getCustomizationConfig().getServiceConfig().getClassName() != null) {
builder.addMethod(serviceConfigurationMethod());
builder.addMethod(serviceConfigurationConsumerBuilderMethod());
}
builder.addMethod(endpointProviderMethod());
if (hasClientContextParams()) {
model.getClientContextParams().forEach((n, m) -> {
builder.addMethod(clientContextParamSetter(n, m));
});
}
if (generateTokenProviderMethod()) {
builder.addMethod(tokenProviderMethod());
}
return builder.build();
}
private CodeBlock getJavadoc() {
return CodeBlock.of("This includes configuration specific to $L that is supported by both {@link $T} and {@link $T}.",
model.getMetadata().getDescriptiveServiceName(),
ClassName.get(basePackage, model.getMetadata().getSyncBuilderInterface()),
ClassName.get(basePackage, model.getMetadata().getAsyncBuilderInterface()));
}
private MethodSpec enableEndpointDiscovery() {
return MethodSpec.methodBuilder("enableEndpointDiscovery")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(TypeVariableName.get("B"))
.addAnnotation(Deprecated.class)
.addJavadoc("@deprecated Use {@link #endpointDiscoveryEnabled($T)} instead.", boolean.class)
.build();
}
private MethodSpec endpointDiscovery() {
return MethodSpec.methodBuilder("endpointDiscoveryEnabled")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(TypeVariableName.get("B"))
.addParameter(boolean.class, "endpointDiscovery")
.build();
}
private MethodSpec serviceConfigurationMethod() {
ClassName serviceConfiguration = ClassName.get(basePackage,
model.getCustomizationConfig().getServiceConfig().getClassName());
return MethodSpec.methodBuilder("serviceConfiguration")
.addModifiers(Modifier.ABSTRACT, Modifier.PUBLIC)
.returns(TypeVariableName.get("B"))
.addParameter(serviceConfiguration, "serviceConfiguration")
.build();
}
private MethodSpec serviceConfigurationConsumerBuilderMethod() {
ClassName serviceConfiguration = ClassName.get(basePackage,
model.getCustomizationConfig().getServiceConfig().getClassName());
TypeName consumerBuilder = ParameterizedTypeName.get(ClassName.get(Consumer.class),
serviceConfiguration.nestedClass("Builder"));
return MethodSpec.methodBuilder("serviceConfiguration")
.addModifiers(Modifier.DEFAULT, Modifier.PUBLIC)
.returns(TypeVariableName.get("B"))
.addParameter(consumerBuilder, "serviceConfiguration")
.addStatement("return serviceConfiguration($T.builder().applyMutation(serviceConfiguration).build())",
serviceConfiguration)
.build();
}
private MethodSpec endpointProviderMethod() {
return MethodSpec.methodBuilder("endpointProvider")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addParameter(endpointRulesSpecUtils.providerInterfaceName(), "endpointProvider")
.addJavadoc("Set the {@link $T} implementation that will be used by the client to determine the endpoint for each "
+ "request. This is optional; if none is provided a default implementation will be used the SDK.",
endpointRulesSpecUtils.providerInterfaceName())
.returns(TypeVariableName.get("B"))
.build();
}
private MethodSpec clientContextParamSetter(String name, ClientContextParam param) {
String setterName = Utils.unCapitalize(CodegenNamingUtils.pascalCase(name));
TypeName type = endpointRulesSpecUtils.toJavaType(param.getType());
MethodSpec.Builder b = MethodSpec.methodBuilder(setterName)
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.addParameter(type, setterName)
.returns(TypeVariableName.get("B"));
PoetUtils.addJavadoc(b::addJavadoc, param.getDocumentation());
return b.build();
}
private boolean generateTokenProviderMethod() {
return BearerAuthUtils.usesBearerAuth(model);
}
private MethodSpec tokenProviderMethod() {
return MethodSpec.methodBuilder("tokenProvider")
.addModifiers(Modifier.PUBLIC, Modifier.ABSTRACT)
.returns(TypeVariableName.get("B"))
.addParameter(SdkTokenProvider.class, "tokenProvider")
.addJavadoc("Set the token provider to use for bearer token authorization. This is optional, if none "
+ "is provided, the SDK will use {@link $T}.\n"
+ "<p>\n"
+ "If the service, or any of its operations require Bearer Token Authorization, then the "
+ "SDK will default to this token provider to retrieve the token to use for authorization.\n"
+ "<p>\n"
+ "This provider works in conjunction with the {@code $T.TOKEN_SIGNER} set on the client. "
+ "By default it is {@link $T}.",
DefaultAwsTokenProvider.class,
SdkAdvancedClientOption.class,
BearerTokenSigner.class)
.build();
}
@Override
public ClassName className() {
return builderInterfaceName;
}
private boolean hasClientContextParams() {
return model.getClientContextParams() != null && !model.getClientContextParams().isEmpty();
}
}