Skip to content

Commit 3803f67

Browse files
committed
Building useragent in client class instead of a constant
1 parent bf40579 commit 3803f67

35 files changed

+57
-68
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/AsyncClientClass.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,13 @@ private MethodSpec constructor(TypeSpec.Builder classBuilder) {
229229
.addStatement("this.clientHandler = new $T(clientConfiguration)", AwsAsyncClientHandler.class)
230230
.addStatement("this.clientConfiguration = clientConfiguration.toBuilder()"
231231
+ ".option($T.SDK_CLIENT, this)"
232-
+ ".option($T.API_METADATA, $T.USER_AGENT)"
232+
+ ".option($T.API_METADATA, $S + \"#\" + $T.VERSION)"
233233
+ ".build()",
234234
SdkClientOption.class,
235235
SdkClientOption.class,
236+
transformServiceId(model.getMetadata().getServiceId()),
236237
ClassName.get(model.getMetadata().getFullClientInternalPackageName(),
237-
"ServiceVersionInfo"));
238+
"ServiceVersionInfo"));
238239

239240
FieldSpec protocolFactoryField = protocolSpec.protocolFactory(model);
240241
if (model.getMetadata().isJsonProtocol()) {
@@ -289,6 +290,11 @@ private boolean hasOperationWithEventStreamOutput() {
289290
return model.getOperations().values().stream().anyMatch(OperationModel::hasEventStreamOutput);
290291
}
291292

293+
private String transformServiceId(String serviceId) {
294+
// According to User Agent 2.0 spec, replace spaces with underscores
295+
return serviceId.replace(" ", "_");
296+
}
297+
292298
private MethodSpec nameMethod() {
293299
return MethodSpec.methodBuilder("serviceName")
294300
.addAnnotation(Override.class)

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/SyncClientClass.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,12 +201,13 @@ private MethodSpec constructor() {
201201
.addStatement("this.clientHandler = new $T(clientConfiguration)", protocolSpec.getClientHandlerClass())
202202
.addStatement("this.clientConfiguration = clientConfiguration.toBuilder()"
203203
+ ".option($T.SDK_CLIENT, this)"
204-
+ ".option($T.API_METADATA, $T.USER_AGENT)"
204+
+ ".option($T.API_METADATA, $S + \"#\" + $T.VERSION)"
205205
+ ".build()",
206206
SdkClientOption.class,
207207
SdkClientOption.class,
208+
transformServiceId(model.getMetadata().getServiceId()),
208209
ClassName.get(model.getMetadata().getFullClientInternalPackageName(),
209-
"ServiceVersionInfo"));
210+
"ServiceVersionInfo"));
210211

211212
FieldSpec protocolFactoryField = protocolSpec.protocolFactory(model);
212213
if (model.getMetadata().isJsonProtocol()) {
@@ -254,6 +255,11 @@ private Stream<MethodSpec> operations(OperationModel opModel) {
254255
return methods.stream();
255256
}
256257

258+
private String transformServiceId(String serviceId) {
259+
// According to User Agent 2.0 spec, replace spaces with underscores
260+
return serviceId.replace(" ", "_");
261+
}
262+
257263
private MethodSpec traditionalMethod(OperationModel opModel) {
258264
MethodSpec.Builder method = SyncClientInterface.operationMethodSignature(model, opModel)
259265
.addAnnotation(Override.class);

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionInfoSpec.java

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public TypeSpec poetSpec() {
4949
.addJavadoc("Returns the current version for the AWS SDK in which"
5050
+ " this class is running.")
5151
.build())
52-
.addField(userAgentField())
5352
.addMethod(privateConstructor());
5453

5554
return builder.build();
@@ -61,20 +60,6 @@ protected MethodSpec privateConstructor() {
6160
.build();
6261
}
6362

64-
private FieldSpec userAgentField() {
65-
return FieldSpec.builder(String.class, "USER_AGENT", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL)
66-
.initializer("$S", transformServiceId(model.getMetadata().getServiceId()) + "#" + SDK_VERSION)
67-
.addAnnotation(SdkInternalApi.class)
68-
.addJavadoc("Returns a user agent containing the service and "
69-
+ "version info")
70-
.build();
71-
}
72-
73-
private String transformServiceId(String serviceId) {
74-
// According to User Agent 2.0 spec, replace spaces with underscores
75-
return serviceId.replace(" ", "_");
76-
}
77-
7863
@Override
7964
public ClassName className() {
8065
return poetExtension.getServiceVersionInfoClass();

codegen/src/test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionInfoSpecTest.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,17 @@
3131
public class ServiceVersionInfoSpecTest {
3232

3333
// Fixture test that compares generated ServiceVersionInfo class against expected output.
34-
// The fixture file uses {{VERSION}} as a placeholder for the SDK version and {{USER_AGENT}}
35-
// as a placeholder for the service-specific user agent metadata. Both placeholders get
36-
// replaced with actual values at test time, since the generated code injects the actual
37-
// version and transformed service ID at build time.
34+
// The fixture file uses {{VERSION}} as a placeholder for the SDK version. The placeholder get
35+
// replaced with actual value at test time, since the generated code injects the actual
36+
// version at build time.
3837
@Test
3938
void testServiceVersionInfoClass() {
4039
String currVersion = VersionInfo.SDK_VERSION;
41-
String currUserAgent =
42-
transformServiceId(ClientTestModels.restJsonServiceModels().getMetadata().getServiceId()) + "#" + currVersion;
4340
ClassSpec serviceVersionInfoSpec = new ServiceVersionInfoSpec(ClientTestModels.restJsonServiceModels());
4441

4542
String expectedContent = loadFixtureFile("test-service-version-info-class.java");
4643
expectedContent = expectedContent
47-
.replace("{{VERSION}}", currVersion)
48-
.replace("{{USER_AGENT}}", currUserAgent);
44+
.replace("{{VERSION}}", currVersion);
4945

5046
String actualContent = generateContent(serviceVersionInfoSpec);
5147

@@ -62,8 +58,4 @@ private String generateContent(ClassSpec spec) {
6258
JavaFile javaFile = JavaFile.builder(spec.className().packageName(), typeSpec).build();
6359
return javaFile.toString();
6460
}
65-
66-
private String transformServiceId(String serviceId) {
67-
return serviceId.replace(" ", "_");
68-
}
6961
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/specs/test-service-version-info-class.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,6 @@ public final class ServiceVersionInfo {
1212
*/
1313
public static final String VERSION = "{{VERSION}}";
1414

15-
/**
16-
* Returns a user agent containing the service and version info
17-
*/
18-
@SdkInternalApi
19-
public static final String USER_AGENT = "{{USER_AGENT}}";
20-
2115
private ServiceVersionInfo() {
2216
}
2317
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-aws-json-async-client-class.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient {
142142
protected DefaultJsonAsyncClient(SdkClientConfiguration clientConfiguration) {
143143
this.clientHandler = new AwsAsyncClientHandler(clientConfiguration);
144144
this.clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.SDK_CLIENT, this)
145-
.option(SdkClientOption.API_METADATA, ServiceVersionInfo.USER_AGENT).build();
145+
.option(SdkClientOption.API_METADATA, "Json_Service" + "#" + ServiceVersionInfo.VERSION).build();
146146
this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build();
147147
this.executor = clientConfiguration.option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR);
148148
}

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-cbor-async-client-class.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient {
145145
protected DefaultJsonAsyncClient(SdkClientConfiguration clientConfiguration) {
146146
this.clientHandler = new AwsAsyncClientHandler(clientConfiguration);
147147
this.clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.SDK_CLIENT, this)
148-
.option(SdkClientOption.API_METADATA, ServiceVersionInfo.USER_AGENT).build();
148+
.option(SdkClientOption.API_METADATA,
149+
"Json_Service" + "#" + ServiceVersionInfo.VERSION).build();
149150
this.protocolFactory = init(AwsCborProtocolFactory.builder()).build();
150151
this.jsonProtocolFactory = init(AwsJsonProtocolFactory.builder()).build();
151152
this.executor = clientConfiguration.option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-async-client-class.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ final class DefaultJsonAsyncClient implements JsonAsyncClient {
155155
protected DefaultJsonAsyncClient(SdkClientConfiguration clientConfiguration) {
156156
this.clientHandler = new AwsAsyncClientHandler(clientConfiguration);
157157
this.clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.SDK_CLIENT, this)
158-
.option(SdkClientOption.API_METADATA, ServiceVersionInfo.USER_AGENT).build();
158+
.option(SdkClientOption.API_METADATA,
159+
"Json_Service" + "#" + ServiceVersionInfo.VERSION).build();
159160
this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build();
160161
this.executor = clientConfiguration.option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR);
161162
this.executorService = clientConfiguration.option(SdkClientOption.SCHEDULED_EXECUTOR_SERVICE);

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-json-client-class.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ final class DefaultJsonClient implements JsonClient {
110110
protected DefaultJsonClient(SdkClientConfiguration clientConfiguration) {
111111
this.clientHandler = new AwsSyncClientHandler(clientConfiguration);
112112
this.clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.SDK_CLIENT, this)
113-
.option(SdkClientOption.API_METADATA, ServiceVersionInfo.USER_AGENT).build();
113+
.option(SdkClientOption.API_METADATA, "Json_Service" + "#" + ServiceVersionInfo.VERSION).build();
114114
this.protocolFactory = init(AwsJsonProtocolFactory.builder()).build();
115115
}
116116

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/sra/test-query-async-client-class.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ final class DefaultQueryAsyncClient implements QueryAsyncClient {
124124
protected DefaultQueryAsyncClient(SdkClientConfiguration clientConfiguration) {
125125
this.clientHandler = new AwsAsyncClientHandler(clientConfiguration);
126126
this.clientConfiguration = clientConfiguration.toBuilder().option(SdkClientOption.SDK_CLIENT, this)
127-
.option(SdkClientOption.API_METADATA, ServiceVersionInfo.USER_AGENT).build();
127+
.option(SdkClientOption.API_METADATA, "Query_Service" + "#" + ServiceVersionInfo.VERSION).build();
128128
this.protocolFactory = init();
129129
this.executorService = clientConfiguration.option(SdkClientOption.SCHEDULED_EXECUTOR_SERVICE);
130130
}

0 commit comments

Comments
 (0)