Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
cb47f65
WIP - add user agent interceptor/plugin
alextwoods Feb 25, 2025
91e8208
Working implementation with basic user agent
alextwoods Feb 25, 2025
d3b1d15
Fix pyright errors
alextwoods Feb 25, 2025
2031933
Use short form license
alextwoods Feb 25, 2025
d5473b5
Run pyupgrade
alextwoods Feb 26, 2025
83a0a8f
Cleanups from PR (still todo: seperate into generic user agent and aw…
alextwoods Feb 26, 2025
b7e15d1
Add Config/HttpConfig Protocols + generate __version__
alextwoods Feb 26, 2025
8afb134
Add __version__ to smithy and aws core
alextwoods Feb 26, 2025
a7475d7
WIP - generic useragent refactoring.
alextwoods Feb 27, 2025
296811b
Remove explicit Config protocol
alextwoods Feb 27, 2025
f449114
Major refactor - generic and aws specific user agent. Codegen userage…
alextwoods Feb 27, 2025
7b7573d
Merge branch 'develop' into user_agent
alextwoods Feb 28, 2025
6a49728
Use aws-sdk-python as sdk name + correctly use sdkId for serviceId
alextwoods Feb 28, 2025
b727753
Merge branch 'develop' into user_agent
alextwoods Feb 28, 2025
b88dfc2
Use aws core version as the "main" sdk version. client library versio…
alextwoods Feb 28, 2025
6c6458b
User agent tests
alextwoods Mar 3, 2025
7aac81c
Add user_agent interceptor test
alextwoods Mar 3, 2025
86e461c
Update packages/smithy-http/src/smithy_http/interceptors/user_agent.py
alextwoods Mar 3, 2025
0013139
Update packages/smithy-http/tests/unit/interceptors/test_user_agent.py
alextwoods Mar 3, 2025
00ce11f
PR cleanups
alextwoods Mar 3, 2025
8ea3bf2
PR updates
alextwoods Mar 3, 2025
a35f607
Merge branch 'develop' into user_agent
alextwoods Mar 4, 2025
5f128d4
Merge branch 'smithy-lang:develop' into user_agent
alextwoods Mar 5, 2025
20b97ed
PR Cleanups
alextwoods Mar 5, 2025
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
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* 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.Collections;
import java.util.List;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.codegen.core.SymbolReference;
import software.amazon.smithy.python.codegen.ConfigProperty;
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 {
@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return List.of(
RuntimeClientPlugin.builder()
.addConfigProperty(
ConfigProperty.builder()
// TODO: This is the name used in boto, but potentially could be user_agent_prefix. Depends on backwards compat strategy.
.name("user_agent_extra")
.documentation("Additional suffix to be added to the User-Agent header.")
.type(Symbol.builder().name("str").build()) // TODO: Should common types like this be defined as constants somewhere?
.nullable(true)
.build())
.addConfigProperty(
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()
)
.pythonPlugin(
SymbolReference.builder()
.symbol(Symbol.builder()
.namespace(AwsPythonDependency.SMITHY_AWS_CORE.packageName() + ".plugins", ".")
.name("user_agent_plugin")
.addDependency(AwsPythonDependency.SMITHY_AWS_CORE)
.build())
.build()
)
.build()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@

software.amazon.smithy.python.aws.codegen.AwsAuthIntegration
software.amazon.smithy.python.aws.codegen.AwsProtocolsIntegration
software.amazon.smithy.python.aws.codegen.AwsUserAgentIntegration
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# 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.

from smithy_aws_core.user_agent import UserAgent
from smithy_core.interceptors import Interceptor, InterceptorContext, Request
from smithy_http import Field
from smithy_http.aio import HTTPRequest


class UserAgentInterceptor(Interceptor):
"""Adds UserAgent header to the Request before signing."""

def __init__(
self,
ua_suffix: str | None = None,
ua_app_id: str | None = None,
sdk_version: str | None = "0.0.1",
) -> None:
"""Initialize the UserAgentInterceptor.

:ua_suffix: Additional suffix to be added to the UserAgent header. :ua_app_id:
User defined and opaque application ID to be added to the UserAgent header.
"""
super().__init__()
self._ua_suffix = ua_suffix
self._ua_app_id = ua_app_id
self._sdk_version = sdk_version

def modify_before_signing(
self, context: InterceptorContext[Request, None, HTTPRequest, None]
) -> HTTPRequest:
user_agent = UserAgent.from_environment().with_config(
ua_suffix=self._ua_suffix,
ua_app_id=self._ua_app_id,
sdk_version=self._sdk_version,
)
request = context.transport_request
request.fields.set_field(
Field(name="User-Agent", values=[user_agent.to_string()])
)
return context.transport_request
25 changes: 25 additions & 0 deletions packages/smithy-aws-core/src/smithy_aws_core/plugins/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.
from typing import Any

from smithy_aws_core.interceptors.user_agent import UserAgentInterceptor


# TODO: Define a Protocol for Config w/ interceptor method?
def user_agent_plugin(config: Any) -> None:
config.interceptors.append(
UserAgentInterceptor(
ua_suffix=config.user_agent_extra,
ua_app_id=config.sdk_ua_app_id,
)
)
Loading
Loading