Skip to content

Add ServiceClientConfiguration to SdkClient #3830

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
merged 20 commits into from
Mar 24, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -22,6 +22,7 @@
import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams;
import software.amazon.awssdk.codegen.poet.builder.BaseClientBuilderClass;
import software.amazon.awssdk.codegen.poet.builder.BaseClientBuilderInterface;
import software.amazon.awssdk.codegen.poet.model.DefaultServiceClientConfigurationClass;

/**
* Task for classes shared by {@link AsyncClientGeneratorTasks} and {@link SyncClientGeneratorTasks}.
Expand All @@ -34,7 +35,8 @@ public CommonClientGeneratorTasks(GeneratorTaskParams dependencies) {
@Override
protected List<GeneratorTask> createTasks() throws Exception {
return Arrays.asList(createBaseBuilderTask(),
createBaseBuilderInterfaceTask());
createBaseBuilderInterfaceTask(),
createDefaultServiceClientConfigurationTask());
}

private GeneratorTask createBaseBuilderTask() throws IOException {
Expand All @@ -44,4 +46,8 @@ private GeneratorTask createBaseBuilderTask() throws IOException {
private GeneratorTask createBaseBuilderInterfaceTask() throws IOException {
return createPoetGeneratorTask(new BaseClientBuilderInterface(model));
}

private GeneratorTask createDefaultServiceClientConfigurationTask() throws IOException {
return createPoetGeneratorTask(new DefaultServiceClientConfigurationClass(model));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private MethodSpec buildClientMethod() {
.returns(clientInterfaceName)
.addStatement("$T clientConfiguration = super.asyncClientConfiguration()", SdkClientConfiguration.class)
.addStatement("this.validateClientOptions(clientConfiguration)")
.addCode("return new $T(clientConfiguration);", clientClassName)
.addCode("return new $T(clientConfiguration, overrideConfiguration());", clientClassName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use addStatement you don't have to add the semicolon and the code looks cleaner

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to addStatement

.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ private MethodSpec buildClientMethod() {
.addStatement("$T clientConfiguration = super.syncClientConfiguration()",
SdkClientConfiguration.class)
.addStatement("this.validateClientOptions(clientConfiguration)")
.addCode("return new $T(clientConfiguration);", clientClassName)
.addCode("return new $T(clientConfiguration, overrideConfiguration());", clientClassName)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,11 @@
import software.amazon.awssdk.codegen.poet.eventstream.EventStreamUtils;
import software.amazon.awssdk.codegen.poet.model.EventStreamSpecHelper;
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.ServiceClientConfiguration;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.core.async.AsyncResponseTransformerUtils;
import software.amazon.awssdk.core.async.SdkPublisher;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
Expand Down Expand Up @@ -133,7 +135,8 @@ protected void addFields(TypeSpec.Builder type) {
.build())
.addField(AsyncClientHandler.class, "clientHandler", PRIVATE, FINAL)
.addField(protocolSpec.protocolFactory(model))
.addField(SdkClientConfiguration.class, "clientConfiguration", PRIVATE, FINAL);
.addField(SdkClientConfiguration.class, "clientConfiguration", PRIVATE, FINAL)
.addField(ServiceClientConfiguration.class, "serviceClientConfiguration", PRIVATE, FINAL);

// Kinesis doesn't support CBOR for STS yet so need another protocol factory for JSON
if (model.getMetadata().isCborProtocol()) {
Expand All @@ -148,6 +151,7 @@ protected void addFields(TypeSpec.Builder type) {
protected void addAdditionalMethods(TypeSpec.Builder type) {
type.addMethod(constructor(type))
.addMethod(nameMethod())
.addMethod(clientConfigMethod())
.addMethods(protocolSpec.additionalMethods())
.addMethod(protocolSpec.initProtocolFactory(model))
.addMethod(resolveMetricPublishersMethod());
Expand Down Expand Up @@ -203,9 +207,16 @@ private MethodSpec constructor(TypeSpec.Builder classBuilder) {
MethodSpec.Builder builder = MethodSpec.constructorBuilder()
.addModifiers(PROTECTED)
.addParameter(SdkClientConfiguration.class, "clientConfiguration")
.addParameter(ClientOverrideConfiguration.class,
"clientOverrideConfiguration")
.addStatement("this.clientHandler = new $T(clientConfiguration)",
AwsAsyncClientHandler.class)
.addStatement("this.clientConfiguration = clientConfiguration");
.addStatement("this.clientConfiguration = clientConfiguration")
.addStatement("this.serviceClientConfiguration = new $T(clientConfiguration, "
+ "clientOverrideConfiguration)",
PoetUtils.classNameFromFqcn(model.getMetadata()
.getFullModelPackageName()
+ ".DefaultServiceClientConfiguration"));
FieldSpec protocolFactoryField = protocolSpec.protocolFactory(model);
if (model.getMetadata().isJsonProtocol()) {
builder.addStatement("this.$N = init($T.builder()).build()", protocolFactoryField.name,
Expand Down Expand Up @@ -264,6 +275,15 @@ private MethodSpec nameMethod() {
.build();
}

private MethodSpec clientConfigMethod() {
return MethodSpec.methodBuilder("serviceClientConfiguration")
.addAnnotation(Override.class)
.addModifiers(PUBLIC, FINAL)
.returns(ServiceClientConfiguration.class)
.addStatement("return this.serviceClientConfiguration")
.build();
}

@Override
protected void addCloseMethod(TypeSpec.Builder type) {
MethodSpec method = MethodSpec.methodBuilder("close")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.core.ServiceClientConfiguration;
import software.amazon.awssdk.utils.Validate;

public class DelegatingAsyncClientClass extends AsyncClientInterface {
Expand Down Expand Up @@ -95,6 +96,7 @@ protected void addAdditionalMethods(TypeSpec.Builder type) {
.build();

type.addMethod(nameMethod())
.addMethod(clientConfigMethod())
.addMethod(delegate);
}

Expand All @@ -107,6 +109,15 @@ private MethodSpec nameMethod() {
.build();
}

private MethodSpec clientConfigMethod() {
return MethodSpec.methodBuilder("serviceClientConfiguration")
.addAnnotation(Override.class)
.addModifiers(PUBLIC, FINAL)
.returns(ServiceClientConfiguration.class)
.addStatement("return delegate.serviceClientConfiguration()")
.build();
}

@Override
protected void addCloseMethod(TypeSpec.Builder type) {
MethodSpec method = MethodSpec.methodBuilder("close")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.core.ServiceClientConfiguration;
import software.amazon.awssdk.utils.Validate;

public class DelegatingSyncClientClass extends SyncClientInterface {
Expand Down Expand Up @@ -96,6 +97,7 @@ protected void addAdditionalMethods(TypeSpec.Builder type) {
.build();

type.addMethod(nameMethod())
.addMethod(clientConfigMethod())
.addMethod(delegate);
}

Expand Down Expand Up @@ -166,4 +168,13 @@ private MethodSpec nameMethod() {
.addStatement("return delegate.serviceName()")
.build();
}

private MethodSpec clientConfigMethod() {
return MethodSpec.methodBuilder("serviceClientConfiguration")
.addAnnotation(Override.class)
.addModifiers(PUBLIC, FINAL)
.returns(ServiceClientConfiguration.class)
.addStatement("return delegate.serviceClientConfiguration()")
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@
import software.amazon.awssdk.codegen.poet.client.specs.XmlProtocolSpec;
import software.amazon.awssdk.codegen.utils.PaginatorUtils;
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.ServiceClientConfiguration;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.client.handler.SyncClientHandler;
Expand Down Expand Up @@ -105,7 +107,8 @@ protected void addFields(TypeSpec.Builder type) {
type.addField(logger())
.addField(SyncClientHandler.class, "clientHandler", PRIVATE, FINAL)
.addField(protocolSpec.protocolFactory(model))
.addField(SdkClientConfiguration.class, "clientConfiguration", PRIVATE, FINAL);
.addField(SdkClientConfiguration.class, "clientConfiguration", PRIVATE, FINAL)
.addField(ServiceClientConfiguration.class, "serviceClientConfiguration", PRIVATE, FINAL);
}

@Override
Expand All @@ -124,6 +127,7 @@ protected void addAdditionalMethods(TypeSpec.Builder type) {

type.addMethod(constructor())
.addMethod(nameMethod())
.addMethod(clientConfigMethod())
.addMethods(protocolSpec.additionalMethods())
.addMethod(resolveMetricPublishersMethod());

Expand All @@ -147,6 +151,15 @@ private MethodSpec nameMethod() {
.build();
}

private MethodSpec clientConfigMethod() {
return MethodSpec.methodBuilder("serviceClientConfiguration")
.addAnnotation(Override.class)
.addModifiers(PUBLIC, FINAL)
.returns(ServiceClientConfiguration.class)
.addStatement("return this.serviceClientConfiguration")
.build();
}

@Override
public ClassName className() {
return className;
Expand All @@ -156,9 +169,16 @@ private MethodSpec constructor() {
MethodSpec.Builder builder = MethodSpec.constructorBuilder()
.addModifiers(PROTECTED)
.addParameter(SdkClientConfiguration.class, "clientConfiguration")
.addParameter(ClientOverrideConfiguration.class,
"clientOverrideConfiguration")
.addStatement("this.clientHandler = new $T(clientConfiguration)",
protocolSpec.getClientHandlerClass())
.addStatement("this.clientConfiguration = clientConfiguration");
.addStatement("this.clientConfiguration = clientConfiguration")
.addStatement("this.serviceClientConfiguration = new $T(clientConfiguration, "
+ "clientOverrideConfiguration)",
PoetUtils.classNameFromFqcn(model.getMetadata()
.getFullModelPackageName()
+ ".DefaultServiceClientConfiguration"));
FieldSpec protocolFactoryField = protocolSpec.protocolFactory(model);
if (model.getMetadata().isJsonProtocol()) {
builder.addStatement("this.$N = init($T.builder()).build()", protocolFactoryField.name,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.model;

import static javax.lang.model.element.Modifier.FINAL;
import static javax.lang.model.element.Modifier.PRIVATE;
import static javax.lang.model.element.Modifier.PUBLIC;

import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.FieldSpec;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeSpec;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel;
import software.amazon.awssdk.codegen.poet.ClassSpec;
import software.amazon.awssdk.codegen.poet.PoetUtils;
import software.amazon.awssdk.core.ServiceClientConfiguration;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;

public class DefaultServiceClientConfigurationClass implements ClassSpec {

private final ClassName defaultClientMetadataClassName;

public DefaultServiceClientConfigurationClass(IntermediateModel model) {
String basePackage = model.getMetadata().getFullModelPackageName();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default client config class is annotated as Internal, but it's placed in the model package. It should probably go in the internal package.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved to internal

this.defaultClientMetadataClassName = ClassName.get(basePackage, "DefaultServiceClientConfiguration");
}

@Override
public TypeSpec poetSpec() {
return PoetUtils.createClassBuilder(defaultClientMetadataClassName)
.addSuperinterface(ServiceClientConfiguration.class)
.addMethod(constructor())
.addMethod(regionMethod())
.addMethod(clientOverrideConfigMethod())
.addModifiers(PUBLIC)
.addAnnotation(SdkInternalApi.class)
.addField(regionField())
.addField(clientOverrideConfigField())
.build();
}

@Override
public ClassName className() {
return defaultClientMetadataClassName;
}

public MethodSpec constructor() {
return MethodSpec.constructorBuilder()
.addModifiers(PUBLIC)
.addParameter(SdkClientConfiguration.class, "clientConfiguration")
.addParameter(ClientOverrideConfiguration.class, "clientOverrideConfiguration")
.addStatement("this.region = clientConfiguration.option($T.AWS_REGION).toString()",
AwsClientOption.class)
.addStatement("this.overrideConfiguration = clientOverrideConfiguration")
.build();
}

public MethodSpec regionMethod() {
return MethodSpec.methodBuilder("region")
.addModifiers(PUBLIC)
.returns(String.class)
.addStatement("return this.region")
.build();
}

public FieldSpec regionField() {
return FieldSpec.builder(ClassName.get(String.class), "region")
.addModifiers(PRIVATE, FINAL)
.build();
}

public MethodSpec clientOverrideConfigMethod() {
return MethodSpec.methodBuilder("overrideConfiguration")
.addModifiers(PUBLIC)
.returns(ClientOverrideConfiguration.class)
.addStatement("return this.overrideConfiguration")
.build();
}

public FieldSpec clientOverrideConfigField() {
return FieldSpec.builder(ClassName.get(ClientOverrideConfiguration.class), "overrideConfiguration")
.addModifiers(PRIVATE, FINAL)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public DefaultJsonAsyncClientBuilder tokenProvider(SdkTokenProvider tokenProvide
protected final JsonAsyncClient buildClient() {
SdkClientConfiguration clientConfiguration = super.asyncClientConfiguration();
this.validateClientOptions(clientConfiguration);
return new DefaultJsonAsyncClient(clientConfiguration);
return new DefaultJsonAsyncClient(clientConfiguration, overrideConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ public DefaultJsonClientBuilder tokenProvider(SdkTokenProvider tokenProvider) {
protected final JsonClient buildClient() {
SdkClientConfiguration clientConfiguration = super.syncClientConfiguration();
this.validateClientOptions(clientConfiguration);
return new DefaultJsonClient(clientConfiguration);
return new DefaultJsonClient(clientConfiguration, overrideConfiguration());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import software.amazon.awssdk.annotations.Generated;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.core.ServiceClientConfiguration;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.services.json.model.APostOperationRequest;
Expand Down Expand Up @@ -657,6 +658,11 @@ public final String serviceName() {
return delegate.serviceName();
}

@Override
public final ServiceClientConfiguration serviceClientConfiguration() {
return delegate.serviceClientConfiguration();
}

public SdkClient delegate() {
return this.delegate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import software.amazon.awssdk.core.ResponseBytes;
import software.amazon.awssdk.core.ResponseInputStream;
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.core.ServiceClientConfiguration;
import software.amazon.awssdk.core.exception.SdkClientException;
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
Expand Down Expand Up @@ -859,6 +860,11 @@ public final String serviceName() {
return delegate.serviceName();
}

@Override
public final ServiceClientConfiguration serviceClientConfiguration() {
return delegate.serviceClientConfiguration();
}

public SdkClient delegate() {
return this.delegate;
}
Expand Down
Loading