Skip to content

Commit

Permalink
chore(codegen): consolidate user-agent customization to a codegen plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
AllanZhengYP committed Dec 14, 2020
1 parent 3430e25 commit bda1ebe
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,6 @@ public List<RuntimeClientPlugin> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<RuntimeClientPlugin> getClientPlugins() {
return ListUtils.of(
RuntimeClientPlugin.builder()
.withConventions(AwsDependency.MIDDLEWARE_USER_AGENT.dependency, "UserAgent").build());
}

@Override
public void addConfigInterfaceFields(
Expand All @@ -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
Expand All @@ -56,7 +70,7 @@ public Map<String, Consumer<TypeScriptWriter>> 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:
Expand All @@ -65,16 +79,7 @@ public Map<String, Consumer<TypeScriptWriter>> 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:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bda1ebe

Please sign in to comment.