Skip to content

Commit cb47f65

Browse files
committed
WIP - add user agent interceptor/plugin
1 parent 68252bd commit cb47f65

File tree

6 files changed

+124
-0
lines changed

6 files changed

+124
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.python.aws.codegen;
6+
7+
import software.amazon.smithy.python.codegen.PythonDependency;
8+
import software.amazon.smithy.utils.SmithyUnstableApi;
9+
10+
/**
11+
* AWS Dependencies used in the smithy python generator.
12+
*/
13+
@SmithyUnstableApi
14+
public class AwsPythonDependency {
15+
/**
16+
* The core aws smithy runtime python package.
17+
*
18+
* <p>While in development this will use the develop branch.
19+
*/
20+
public static final PythonDependency SMITHY_AWS_CORE = new PythonDependency(
21+
"smithy_aws_core",
22+
// You'll need to locally install this before we publish
23+
"==0.0.1",
24+
PythonDependency.Type.DEPENDENCY,
25+
false);
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package software.amazon.smithy.python.aws.codegen;
6+
7+
import java.util.Collections;
8+
import java.util.List;
9+
import software.amazon.smithy.codegen.core.Symbol;
10+
import software.amazon.smithy.codegen.core.SymbolReference;
11+
import software.amazon.smithy.python.codegen.ConfigProperty;
12+
import software.amazon.smithy.python.codegen.integrations.PythonIntegration;
13+
import software.amazon.smithy.python.codegen.integrations.RuntimeClientPlugin;
14+
import software.amazon.smithy.utils.SmithyInternalApi;
15+
16+
/**
17+
* Adds a runtime plugin to set user agent.
18+
*/
19+
@SmithyInternalApi
20+
public class AwsUserAgentIntegration implements PythonIntegration {
21+
@Override
22+
public List<RuntimeClientPlugin> getClientPlugins() {
23+
return List.of(
24+
RuntimeClientPlugin.builder()
25+
.addConfigProperty(ConfigProperty.builder()
26+
// TODO: This is the name used in boto, but potentially could be user_agent_prefix. Depends on backwards compat strategy.
27+
.name("user_agent_extra")
28+
.documentation("Additional suffix to be added to the user agent")
29+
.type(Symbol.builder().name("str").build()) // TODO: Should common types like this be defined as constants somewhere?
30+
.nullable(true)
31+
.build())
32+
.pythonPlugin(
33+
SymbolReference.builder()
34+
.symbol(Symbol.builder()
35+
.namespace(AwsPythonDependency.SMITHY_AWS_CORE.packageName() + ".plugins", ".")
36+
.name("user_agent_plugin")
37+
.addDependency(AwsPythonDependency.SMITHY_AWS_CORE)
38+
.build())
39+
.build()
40+
)
41+
.build()
42+
);
43+
}
44+
45+
}

codegen/aws/core/src/main/resources/META-INF/services/software.amazon.smithy.python.codegen.integrations.PythonIntegration

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55

66
software.amazon.smithy.python.aws.codegen.AwsAuthIntegration
77
software.amazon.smithy.python.aws.codegen.AwsProtocolsIntegration
8+
software.amazon.smithy.python.aws.codegen.AwsUserAgentIntegration
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
from smithy_core.interceptors import Interceptor, InterceptorContext, Request, TransportRequest
14+
from smithy_http.aio import HTTPRequest
15+
16+
17+
class UserAgentInterceptor(Interceptor):
18+
"""Adds UserAgent header to the Request before signing.
19+
"""
20+
def modify_before_signing(
21+
self, context: InterceptorContext[Request, None, HTTPRequest, None]
22+
) -> HTTPRequest:
23+
print("Oh Hello here I am!")
24+
return context.transport_request
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License"). You
4+
# may not use this file except in compliance with the License. A copy of
5+
# the License is located at
6+
#
7+
# http://aws.amazon.com/apache2.0/
8+
#
9+
# or in the "license" file accompanying this file. This file is
10+
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
11+
# ANY KIND, either express or implied. See the License for the specific
12+
# language governing permissions and limitations under the License.
13+
14+
# TODO: Define a Protocol for Config w/ interceptor method?
15+
def user_agent_plugin(config: any) -> None:
16+
config.interceptors.append()

0 commit comments

Comments
 (0)