Skip to content

Commit

Permalink
moving appId to client override config
Browse files Browse the repository at this point in the history
  • Loading branch information
cenedhryn committed Oct 9, 2024
1 parent 3819b2d commit 633dd51
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.core.SdkPlugin;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkAdvancedClientOption;
import software.amazon.awssdk.endpoints.EndpointProvider;
import software.amazon.awssdk.http.auth.spi.scheme.AuthScheme;
import software.amazon.awssdk.utils.builder.SdkBuilder;
Expand Down Expand Up @@ -97,23 +96,4 @@ default List<SdkPlugin> plugins() {
throw new UnsupportedOperationException();
}

/**
* Configure an optional identification value to be appended to the user agent header.
* The value should be less than 50 characters in length and is null by default.
* <p>
* Users can additionally supply the appId value through environment and JVM settings, and
* it will be resolved using the following order of precedence (highest first):
* <ol>
* <li>This client builder configuration </li>
* <li>The {@code AWS_SDK_UA_APP_ID} environment variable</li>
* <li>The {@code sdk.ua.appId} JVM system property</li>
* <li>The {@code sdk_ua_app_id} setting in the profile file for the active profile</li>
* </ol>
* <p>
* This configuration option supersedes {@link SdkAdvancedClientOption#USER_AGENT_PREFIX} and
* {@link SdkAdvancedClientOption#USER_AGENT_SUFFIX} and should be used instead of those options.
*/
default B appId(String appId) {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY;
import static software.amazon.awssdk.core.client.config.SdkClientOption.SCHEDULED_EXECUTOR_SERVICE;
import static software.amazon.awssdk.core.client.config.SdkClientOption.SYNC_HTTP_CLIENT;
import static software.amazon.awssdk.core.client.config.SdkClientOption.USER_AGENT_APP_ID;
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.APP_ID;
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.HTTP;
import static software.amazon.awssdk.core.internal.useragent.UserAgentConstant.INTERNAL_METADATA_MARKER;
Expand Down Expand Up @@ -151,7 +152,6 @@ public abstract class SdkDefaultClientBuilder<B extends SdkClientBuilder<B, C>,
private final SdkHttpClient.Builder defaultHttpClientBuilder;
private final SdkAsyncHttpClient.Builder defaultAsyncHttpClientBuilder;
private final List<SdkPlugin> plugins = new ArrayList<>();
private String appId;


protected SdkDefaultClientBuilder() {
Expand Down Expand Up @@ -415,7 +415,7 @@ private String resolveClientUserAgent(LazyValueSource config) {
SdkClientUserAgentProperties clientProperties = new SdkClientUserAgentProperties();

ClientType clientType = config.get(CLIENT_TYPE);
ClientType resolvedClientType = clientType == null ? ClientType.UNKNOWN : config.get(CLIENT_TYPE);
ClientType resolvedClientType = clientType == null ? ClientType.UNKNOWN : clientType;

clientProperties.putProperty(RETRY_MODE, StringUtils.lowerCase(resolveRetryMode(config.get(RETRY_POLICY),
config.get(RETRY_STRATEGY))));
Expand All @@ -424,7 +424,9 @@ private String resolveClientUserAgent(LazyValueSource config) {
clientProperties.putProperty(HTTP, SdkHttpUtils.urlEncode(clientName(resolvedClientType,
config.get(SYNC_HTTP_CLIENT),
config.get(ASYNC_HTTP_CLIENT))));
clientProperties.putProperty(APP_ID, appId().orElseGet(() -> resolveAppId(config)));
String appId = config.get(USER_AGENT_APP_ID);
String resolvedAppId = appId == null ? resolveAppId(config) : appId;
clientProperties.putProperty(APP_ID, resolvedAppId);
return SdkUserAgentBuilder.buildClientUserAgentString(SystemUserAgent.getOrCreate(), clientProperties);
}

Expand Down Expand Up @@ -457,10 +459,6 @@ private RetryStrategy resolveRetryStrategy(LazyValueSource config) {
return SdkDefaultRetryStrategy.forRetryMode(retryMode);
}

public Optional<String> appId() {
return Optional.ofNullable(appId);
}

/**
* Finalize which sync HTTP client will be used for the created client.
*/
Expand Down Expand Up @@ -658,12 +656,6 @@ public final List<SdkPlugin> plugins() {
return Collections.unmodifiableList(plugins);
}

@Override
public final B appId(String appId) {
this.appId = appId;
return thisBuilder();
}

/**
* Return "this" for method chaining.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_POLICY;
import static software.amazon.awssdk.core.client.config.SdkClientOption.RETRY_STRATEGY;
import static software.amazon.awssdk.core.client.config.SdkClientOption.SCHEDULED_EXECUTOR_SERVICE;
import static software.amazon.awssdk.core.client.config.SdkClientOption.USER_AGENT_APP_ID;
import static software.amazon.awssdk.utils.ScheduledExecutorUtils.unmanagedScheduledExecutor;
import static software.amazon.awssdk.utils.ScheduledExecutorUtils.unwrapUnmanagedScheduledExecutor;

Expand Down Expand Up @@ -120,6 +121,7 @@ public final class ClientOverrideConfiguration
options.add(CONFIGURED_RETRY_STRATEGY);
options.add(CONFIGURED_RETRY_CONFIGURATOR);
options.add(CONFIGURED_RETRY_MODE);
options.add(USER_AGENT_APP_ID);
CLIENT_OVERRIDE_OPTIONS = Collections.unmodifiableSet(options);

Set<ClientOption<?>> resolvedOptions = new HashSet<>();
Expand Down Expand Up @@ -381,6 +383,14 @@ public Optional<CompressionConfiguration> compressionConfiguration() {
return Optional.ofNullable(compressionConfig);
}

/**
* An optional user specified identification value to be appended to the user agent header.
* For more information, see {@link SdkClientOption#USER_AGENT_APP_ID}.
*/
public Optional<String> appId() {
return Optional.ofNullable(config.option(USER_AGENT_APP_ID));
}

@Override
public String toString() {
return ToString.builder("ClientOverrideConfiguration")
Expand All @@ -395,6 +405,7 @@ public String toString() {
.add("profileName", defaultProfileName().orElse(null))
.add("scheduledExecutorService", scheduledExecutorService().orElse(null))
.add("compressionConfiguration", compressionConfiguration().orElse(null))
.add("appId", appId().orElse(null))
.build();
}

Expand Down Expand Up @@ -757,6 +768,16 @@ default Builder compressionConfiguration(Consumer<CompressionConfiguration.Build
}

CompressionConfiguration compressionConfiguration();

/**
* Sets the appId for this client. See {@link SdkClientOption#USER_AGENT_APP_ID}.
*/
Builder appId(String appId);

/**
* The appId for this client. See {@link SdkClientOption#USER_AGENT_APP_ID}.
*/
String appId();
}

/**
Expand Down Expand Up @@ -1089,6 +1110,17 @@ public CompressionConfiguration compressionConfiguration() {
return config.option(CONFIGURED_COMPRESSION_CONFIGURATION);
}

@Override
public String appId() {
return config.option(USER_AGENT_APP_ID);
}

@Override
public Builder appId(String appId) {
config.option(USER_AGENT_APP_ID, appId);
return this;
}

@Override
public ClientOverrideConfiguration build() {
return new ClientOverrideConfiguration(config.build(), resolvedConfig.build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@ public final class SdkClientOption<T> extends ClientOption<T> {
public static final SdkClientOption<CompressionConfiguration> COMPRESSION_CONFIGURATION =
new SdkClientOption<>(CompressionConfiguration.class);

/**
* An optional identification value to be appended to the user agent header.
* The value should be less than 50 characters in length and is null by default.
* <p>
* Users can additionally supply the appId value through environment and JVM settings, and
* it will be resolved using the following order of precedence (highest first):
* <ol>
* <li>This client option configuration </li>
* <li>The {@code AWS_SDK_UA_APP_ID} environment variable</li>
* <li>The {@code sdk.ua.appId} JVM system property</li>
* <li>The {@code sdk_ua_app_id} setting in the profile file for the active profile</li>
* </ol>
* <p>
* This configuration option supersedes {@link SdkAdvancedClientOption#USER_AGENT_PREFIX} and
* {@link SdkAdvancedClientOption#USER_AGENT_SUFFIX} and should be used instead of those options.
*/
public static final SdkClientOption<String> USER_AGENT_APP_ID = new SdkClientOption<>(String.class);

/**
* Option to specify a reference to the SDK client in use.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.SdkSystemSetting;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.core.interceptor.Context;
import software.amazon.awssdk.core.interceptor.ExecutionAttributes;
import software.amazon.awssdk.core.interceptor.ExecutionInterceptor;
Expand Down Expand Up @@ -62,7 +63,8 @@ void resolveAppIdFromEnvironment(String description, String clientAppId, String
RestJsonEndpointProvidersClientBuilder clientBuilder = syncClientBuilder();

if (!StringUtils.isEmpty(clientAppId)) {
clientBuilder.appId(clientAppId);
ClientOverrideConfiguration config = clientBuilder.overrideConfiguration().toBuilder().appId(clientAppId).build();
clientBuilder.overrideConfiguration(config);
}

assertThatThrownBy(() -> clientBuilder.build().allTypes(r -> {}))
Expand Down

0 comments on commit 633dd51

Please sign in to comment.