From bda1ebe38893fa2db34e964c4e49cad4717b9ae5 Mon Sep 17 00:00:00 2001 From: AllanFly120 Date: Mon, 14 Dec 2020 00:19:59 +0000 Subject: [PATCH] chore(codegen): consolidate user-agent customization to a codegen plugin --- .../typescript/codegen/AddBuiltinPlugins.java | 3 -- ...onfig.java => AddUserAgentDependency.java} | 35 +++++++++++-------- .../aws/typescript/codegen/AwsDependency.java | 1 + ....codegen.integration.TypeScriptIntegration | 2 +- 4 files changed, 22 insertions(+), 19 deletions(-) rename codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/{AddDefaultUserAgentConfig.java => AddUserAgentDependency.java} (68%) diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java index f4db8f1157934..bda22f0671629 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddBuiltinPlugins.java @@ -72,9 +72,6 @@ public List getClientPlugins() { RuntimeClientPlugin.builder() .withConventions(TypeScriptDependency.MIDDLEWARE_RETRY.dependency, "Retry") .build(), - RuntimeClientPlugin.builder() - .withConventions(TypeScriptDependency.MIDDLEWARE_USER_AGENT.dependency, "UserAgent") - .build(), RuntimeClientPlugin.builder() .withConventions(TypeScriptDependency.MIDDLEWARE_CONTENT_LENGTH.dependency, "ContentLength", HAS_MIDDLEWARE) diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddDefaultUserAgentConfig.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddUserAgentDependency.java similarity index 68% rename from codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddDefaultUserAgentConfig.java rename to codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddUserAgentDependency.java index 45099fe2c26b0..2e68885a1892c 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddDefaultUserAgentConfig.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AddUserAgentDependency.java @@ -16,20 +16,32 @@ package software.amazon.smithy.aws.typescript.codegen; import java.util.Collections; +import java.util.List; import java.util.Map; import java.util.function.Consumer; + +import software.amazon.smithy.aws.typescript.codegen.AwsDependency; import software.amazon.smithy.codegen.core.SymbolProvider; import software.amazon.smithy.model.Model; import software.amazon.smithy.typescript.codegen.LanguageTarget; +import software.amazon.smithy.typescript.codegen.TypeScriptDependency; import software.amazon.smithy.typescript.codegen.TypeScriptSettings; import software.amazon.smithy.typescript.codegen.TypeScriptWriter; +import software.amazon.smithy.typescript.codegen.integration.RuntimeClientPlugin; import software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration; +import software.amazon.smithy.utils.ListUtils; import software.amazon.smithy.utils.MapUtils; /** - * Adds default user agent to AWS clients + * Add client plubins and configs to support injecting user agent. */ -public final class AddDefaultUserAgentConfig implements TypeScriptIntegration { +public class AddUserAgentDependency implements TypeScriptIntegration { + @Override + public List getClientPlugins() { + return ListUtils.of( + RuntimeClientPlugin.builder() + .withConventions(AwsDependency.MIDDLEWARE_USER_AGENT.dependency, "UserAgent").build()); + } @Override public void addConfigInterfaceFields( @@ -38,8 +50,10 @@ public void addConfigInterfaceFields( SymbolProvider symbolProvider, TypeScriptWriter writer ) { - writer.writeDocs("The string that will be used to populate default value in 'User-Agent' header"); - writer.write("defaultUserAgent?: string;\n"); + writer.addImport("Provider", "Provider", TypeScriptDependency.AWS_SDK_TYPES.packageName); + writer.addImport("UserAgent", "__UserAgent", TypeScriptDependency.AWS_SDK_TYPES.packageName); + writer.writeDocs("The provider populating default tracking information to be sent with `user-agent`, `x-amz-user-agent` header"); + writer.write("defaultUserAgent?: Provider<__UserAgent>;\n"); } @Override @@ -56,7 +70,7 @@ public Map> getRuntimeConfigWriters( writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.dependency); writer.addImport("defaultUserAgent", "defaultUserAgent", AwsDependency.AWS_SDK_UTIL_USER_AGENT_NODE.packageName); writer.addDefaultImport("packageInfo", "./package.json"); - writer.write("defaultUserAgent: defaultUserAgent(packageInfo.name, packageInfo.version),"); + writer.write("defaultUserAgent: defaultUserAgent({serviceId: ClientSharedValues.serviceId, clientVersion: packageInfo.version}),"); } ); case BROWSER: @@ -65,16 +79,7 @@ public Map> getRuntimeConfigWriters( writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.dependency); writer.addImport("defaultUserAgent", "defaultUserAgent", AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.packageName); writer.addDefaultImport("packageInfo", "./package.json"); - writer.write("defaultUserAgent: defaultUserAgent(packageInfo.name, packageInfo.version),"); - } - ); - case REACT_NATIVE: - return MapUtils.of( - "defaultUserAgent", writer -> { - writer.addDependency(AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.dependency); - writer.addImport("defaultUserAgent", "defaultUserAgent", AwsDependency.AWS_SDK_UTIL_USER_AGENT_BROWSER.packageName); - writer.addDefaultImport("packageInfo", "./package.json"); - writer.write("defaultUserAgent: defaultUserAgent(packageInfo.name, packageInfo.version),"); + writer.write("defaultUserAgent: defaultUserAgent({serviceId: ClientSharedValues.serviceId, clientVersion: packageInfo.version}),"); } ); default: diff --git a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java index 99ce0fb2d44dc..7401979bd55d5 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java +++ b/codegen/smithy-aws-typescript-codegen/src/main/java/software/amazon/smithy/aws/typescript/codegen/AwsDependency.java @@ -62,6 +62,7 @@ public enum AwsDependency implements SymbolDependencyContainer { RETRY_CONFIG_PROVIDER(NORMAL_DEPENDENCY, "@aws-sdk/retry-config-provider", "^1.0.0-rc.1"), NODE_CONFIG_PROVIDER(NORMAL_DEPENDENCY, "@aws-sdk/node-config-provider", "^1.0.0-rc.1"), MIDDLEWARE_LOGGER(NORMAL_DEPENDENCY, "@aws-sdk/middleware-logger", "^1.0.0-rc.1"), + MIDDLEWARE_USER_AGENT("dependencies", "@aws-sdk/middleware-user-agent", "1.0.0-rc.1"), AWS_SDK_UTIL_USER_AGENT_BROWSER(NORMAL_DEPENDENCY, "@aws-sdk/util-user-agent-browser", "1.0.0-rc.1"), AWS_SDK_UTIL_USER_AGENT_NODE(NORMAL_DEPENDENCY, "@aws-sdk/util-user-agent-node", "1.0.0-rc.1"); diff --git a/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration b/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration index 5fb15896eb5a2..cc49c3d7e7e99 100644 --- a/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration +++ b/codegen/smithy-aws-typescript-codegen/src/main/resources/META-INF/services/software.amazon.smithy.typescript.codegen.integration.TypeScriptIntegration @@ -12,4 +12,4 @@ software.amazon.smithy.aws.typescript.codegen.AddS3ControlDependency software.amazon.smithy.aws.typescript.codegen.AddEventStreamHandlingDependency software.amazon.smithy.aws.typescript.codegen.AddHttp2Dependency software.amazon.smithy.aws.typescript.codegen.AddTranscribeStreamingDependency -software.amazon.smithy.aws.typescript.codegen.AddDefaultUserAgentConfig +software.amazon.smithy.aws.typescript.codegen.AddUserAgentDependency