generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 24
User agent #387
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
User agent #387
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
cb47f65
WIP - add user agent interceptor/plugin
alextwoods 91e8208
Working implementation with basic user agent
alextwoods d3b1d15
Fix pyright errors
alextwoods 2031933
Use short form license
alextwoods d5473b5
Run pyupgrade
alextwoods 83a0a8f
Cleanups from PR (still todo: seperate into generic user agent and aw…
alextwoods b7e15d1
Add Config/HttpConfig Protocols + generate __version__
alextwoods 8afb134
Add __version__ to smithy and aws core
alextwoods a7475d7
WIP - generic useragent refactoring.
alextwoods 296811b
Remove explicit Config protocol
alextwoods f449114
Major refactor - generic and aws specific user agent. Codegen userage…
alextwoods 7b7573d
Merge branch 'develop' into user_agent
alextwoods 6a49728
Use aws-sdk-python as sdk name + correctly use sdkId for serviceId
alextwoods b727753
Merge branch 'develop' into user_agent
alextwoods b88dfc2
Use aws core version as the "main" sdk version. client library versio…
alextwoods 6c6458b
User agent tests
alextwoods 7aac81c
Add user_agent interceptor test
alextwoods 86e461c
Update packages/smithy-http/src/smithy_http/interceptors/user_agent.py
alextwoods 0013139
Update packages/smithy-http/tests/unit/interceptors/test_user_agent.py
alextwoods 00ce11f
PR cleanups
alextwoods 8ea3bf2
PR updates
alextwoods a35f607
Merge branch 'develop' into user_agent
alextwoods 5f128d4
Merge branch 'smithy-lang:develop' into user_agent
alextwoods 20b97ed
PR Cleanups
alextwoods File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...aws/core/src/main/java/software/amazon/smithy/python/aws/codegen/AwsPythonDependency.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.python.aws.codegen; | ||
|
|
||
| import software.amazon.smithy.python.codegen.PythonDependency; | ||
| import software.amazon.smithy.utils.SmithyUnstableApi; | ||
|
|
||
| /** | ||
| * AWS Dependencies used in the smithy python generator. | ||
| */ | ||
| @SmithyUnstableApi | ||
| public class AwsPythonDependency { | ||
| /** | ||
| * The core aws smithy runtime python package. | ||
| * | ||
| * <p>While in development this will use the develop branch. | ||
| */ | ||
| public static final PythonDependency SMITHY_AWS_CORE = new PythonDependency( | ||
| "smithy_aws_core", | ||
| // You'll need to locally install this before we publish | ||
| "==0.0.1", | ||
| PythonDependency.Type.DEPENDENCY, | ||
| false); | ||
| } |
114 changes: 114 additions & 0 deletions
114
...core/src/main/java/software/amazon/smithy/python/aws/codegen/AwsUserAgentIntegration.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
| package software.amazon.smithy.python.aws.codegen; | ||
|
|
||
| import java.util.List; | ||
| import software.amazon.smithy.aws.traits.ServiceTrait; | ||
| import software.amazon.smithy.codegen.core.Symbol; | ||
| import software.amazon.smithy.codegen.core.SymbolReference; | ||
| import software.amazon.smithy.python.codegen.CodegenUtils; | ||
| import software.amazon.smithy.python.codegen.ConfigProperty; | ||
| import software.amazon.smithy.python.codegen.GenerationContext; | ||
| import software.amazon.smithy.python.codegen.integrations.PythonIntegration; | ||
| import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin; | ||
| import software.amazon.smithy.utils.SmithyInternalApi; | ||
|
|
||
| /** | ||
| * Adds a runtime plugin to set user agent. | ||
| */ | ||
| @SmithyInternalApi | ||
| public class AwsUserAgentIntegration implements PythonIntegration { | ||
|
|
||
| public static final String USER_AGENT_PLUGIN = """ | ||
| def aws_user_agent_plugin(config: $1T): | ||
| config.interceptors.append( | ||
| $2T( | ||
| ua_suffix=config.user_agent_extra, | ||
| ua_app_id=config.sdk_ua_app_id, | ||
| sdk_version=$3T, | ||
| service_id='$4L' | ||
| ) | ||
| ) | ||
| """; | ||
|
|
||
| @Override | ||
| public List<RuntimeClientPlugin> getClientPlugins(GenerationContext context) { | ||
| if (context.applicationProtocol().isHttpProtocol()) { | ||
| final ConfigProperty userAgentExtra = ConfigProperty.builder() | ||
| .name("user_agent_extra") | ||
| .documentation("Additional suffix to be added to the User-Agent header.") | ||
| .type(Symbol.builder().name("str").build()) | ||
| .nullable(true) | ||
| .build(); | ||
|
|
||
| final ConfigProperty uaAppId = ConfigProperty.builder() | ||
| .name("sdk_ua_app_id") | ||
| .documentation( | ||
| "A unique and opaque application ID that is appended to the User-Agent header.") | ||
| .type(Symbol.builder().name("str").build()) | ||
| .nullable(true) | ||
| .build(); | ||
|
|
||
| final String user_agent_plugin_file = "user_agent"; | ||
|
|
||
| final String moduleName = context.settings().moduleName(); | ||
| final SymbolReference userAgentPlugin = SymbolReference.builder() | ||
| .symbol(Symbol.builder() | ||
| .namespace(String.format("%s.%s", | ||
| moduleName, | ||
| user_agent_plugin_file.replace('/', '.')), ".") | ||
| .definitionFile(String | ||
| .format("./%s/%s.py", moduleName, user_agent_plugin_file)) | ||
| .name("aws_user_agent_plugin") | ||
| .build()) | ||
| .build(); | ||
| final SymbolReference userAgentInterceptor = SymbolReference.builder() | ||
| .symbol(Symbol.builder() | ||
| .namespace("smithy_aws_core.interceptors.user_agent", ".") | ||
| .name("UserAgentInterceptor") | ||
| .build()) | ||
| .build(); | ||
| final SymbolReference versionSymbol = SymbolReference.builder() | ||
| .symbol(Symbol.builder() | ||
| .namespace(moduleName, ".") | ||
| .name("__version__") | ||
| .build()) | ||
| .build(); | ||
|
|
||
| final String serviceId = context.settings() | ||
| .service(context.model()) | ||
| .getTrait(ServiceTrait.class) | ||
| .map(ServiceTrait::getSdkId) | ||
| .orElse(context.settings().service().getName()) | ||
| .replace(' ', '_'); | ||
|
|
||
| return List.of( | ||
| RuntimeClientPlugin.builder() | ||
| .addConfigProperty(userAgentExtra) | ||
| .addConfigProperty(uaAppId) | ||
| .pythonPlugin(userAgentPlugin) | ||
| .writeAdditionalFiles((c) -> { | ||
| String filename = "%s/%s.py".formatted(moduleName, user_agent_plugin_file); | ||
| c.writerDelegator() | ||
| .useFileWriter( | ||
| filename, | ||
| moduleName + ".", | ||
| writer -> { | ||
| writer.write(USER_AGENT_PLUGIN, | ||
| CodegenUtils.getConfigSymbol(c.settings()), | ||
| userAgentInterceptor, | ||
| versionSymbol, | ||
| serviceId); | ||
|
|
||
| }); | ||
| return List.of(filename); | ||
| }) | ||
| .build()); | ||
| } else { | ||
| return List.of(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,6 +50,7 @@ | |
| import software.amazon.smithy.python.codegen.generators.StructureGenerator; | ||
| import software.amazon.smithy.python.codegen.generators.UnionGenerator; | ||
| import software.amazon.smithy.python.codegen.integrations.PythonIntegration; | ||
| import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin; | ||
| import software.amazon.smithy.python.codegen.writer.PythonDelegator; | ||
| import software.amazon.smithy.python.codegen.writer.PythonWriter; | ||
| import software.amazon.smithy.utils.SmithyUnstableApi; | ||
|
|
@@ -273,9 +274,39 @@ public void generateIntEnumShape(GenerateIntEnumDirective<GenerationContext, Pyt | |
|
|
||
| @Override | ||
| public void customizeBeforeIntegrations(CustomizeDirective<GenerationContext, PythonSettings> directive) { | ||
| generateServiceModuleInit(directive); | ||
| generatePluginFiles(directive); | ||
| generateInits(directive); | ||
| } | ||
|
|
||
| /** | ||
| * Writes out all extra files required by runtime plugins. | ||
| */ | ||
| private void generatePluginFiles(CustomizeDirective<GenerationContext, PythonSettings> directive) { | ||
| GenerationContext context = directive.context(); | ||
| for (PythonIntegration integration : context.integrations()) { | ||
| for (RuntimeClientPlugin runtimeClientPlugin : integration.getClientPlugins(context)) { | ||
| if (runtimeClientPlugin.matchesService(context.model(), directive.service())) { | ||
| runtimeClientPlugin.writeAdditionalFiles(context); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Creates top level __init__.py file. | ||
| */ | ||
| private void generateServiceModuleInit(CustomizeDirective<GenerationContext, PythonSettings> directive) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be done directly in |
||
| directive.context() | ||
| .writerDelegator() | ||
| .useFileWriter( | ||
| "%s/__init__.py".formatted(directive.context().settings().moduleName()), | ||
| writer -> { | ||
| writer | ||
| .write("__version__: str = '$L'", directive.context().settings().moduleVersion()); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Creates __init__.py files where not already present. | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.