-
Notifications
You must be signed in to change notification settings - Fork 190
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
Feature: Customizable Operations #1647
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3e9cf67
feature: customizable operations
Velfi 9cedc99
update: CHANGELOG.next.toml
Velfi da24b9f
update: RFC0017
Velfi 9ef1c6a
fix: clippy lints
Velfi 0c602c0
Merge remote-tracking branch 'origin/main' into feature/customizable-…
Velfi eb9dd16
update: customizable ops codegen to work for both AWS and native clients
Velfi a96dd7e
Merge remote-tracking branch 'origin/main' into feature/customizable-…
Velfi 0ff7ce1
update: CHANGELOG.next.toml refs
Velfi bf21257
format: run kotlin formatter
Velfi 7a96dd5
Merge remote-tracking branch 'origin/main' into feature/customizable-…
Velfi cd70b6d
fix: use fully qualified Result type in FluentClientGenerator
Velfi 039add4
Merge remote-tracking branch 'origin/main' into feature/customizable-…
Velfi 43375f2
refactor: move Writables into their own file
Velfi 2dc6229
refactor: remove RetryPolicy trait in favor of passing it into the Fl…
Velfi 8628478
add: Unit to RustType
Velfi eca1296
remove: unused methods for GenericsGenerator
Velfi 5ddbe7f
Merge branch 'main' into feature/customizable-operations
Velfi 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 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 |
---|---|---|
|
@@ -45,3 +45,6 @@ gradle-app.setting | |
|
||
# Rust build artifacts | ||
target/ | ||
|
||
# IDEs | ||
.idea/ | ||
This file contains 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 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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -25,6 +25,8 @@ import software.amazon.smithy.rust.codegen.smithy.RuntimeConfig | |||||||||
import software.amazon.smithy.rust.codegen.smithy.RuntimeType | ||||||||||
import software.amazon.smithy.rust.codegen.smithy.RustCrate | ||||||||||
import software.amazon.smithy.rust.codegen.smithy.customize.RustCodegenDecorator | ||||||||||
import software.amazon.smithy.rust.codegen.smithy.generators.GenericTypeArg | ||||||||||
import software.amazon.smithy.rust.codegen.smithy.generators.GenericsGenerator | ||||||||||
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsCustomization | ||||||||||
import software.amazon.smithy.rust.codegen.smithy.generators.LibRsSection | ||||||||||
import software.amazon.smithy.rust.codegen.smithy.generators.client.FluentClientCustomization | ||||||||||
|
@@ -73,6 +75,10 @@ private class AwsClientGenerics(private val types: Types) : FluentClientGenerics | |||||||||
|
||||||||||
/** Bounds for generated `send()` functions */ | ||||||||||
override fun sendBounds(input: Symbol, output: Symbol, error: RuntimeType): Writable = writable { } | ||||||||||
|
||||||||||
override fun toGenericsGenerator(): GenericsGenerator { | ||||||||||
return GenericsGenerator() | ||||||||||
} | ||||||||||
Comment on lines
+79
to
+81
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.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
class AwsFluentClientDecorator : RustCodegenDecorator<ClientCodegenContext> { | ||||||||||
|
@@ -82,15 +88,21 @@ class AwsFluentClientDecorator : RustCodegenDecorator<ClientCodegenContext> { | |||||||||
override val order: Byte = (AwsPresigningDecorator.ORDER + 1).toByte() | ||||||||||
|
||||||||||
override fun extras(codegenContext: ClientCodegenContext, rustCrate: RustCrate) { | ||||||||||
val types = Types(codegenContext.runtimeConfig) | ||||||||||
val runtimeConfig = codegenContext.runtimeConfig | ||||||||||
val types = Types(runtimeConfig) | ||||||||||
val generics = AwsClientGenerics(types) | ||||||||||
FluentClientGenerator( | ||||||||||
codegenContext, | ||||||||||
generics = AwsClientGenerics(types), | ||||||||||
generics, | ||||||||||
customizations = listOf( | ||||||||||
AwsPresignedFluentBuilderMethod(codegenContext.runtimeConfig), | ||||||||||
AwsPresignedFluentBuilderMethod(runtimeConfig), | ||||||||||
AwsFluentClientDocs(codegenContext), | ||||||||||
), | ||||||||||
retryPolicyType = runtimeConfig.awsHttp().asType().member("retry::AwsErrorRetryPolicy"), | ||||||||||
).render(rustCrate) | ||||||||||
rustCrate.withModule(FluentClientGenerator.customizableOperationModule) { writer -> | ||||||||||
renderCustomizableOperationSendMethod(runtimeConfig, generics, writer) | ||||||||||
} | ||||||||||
rustCrate.withModule(FluentClientGenerator.clientModule) { writer -> | ||||||||||
AwsFluentClientExtensions(types).render(writer) | ||||||||||
} | ||||||||||
|
@@ -254,3 +266,43 @@ private class AwsFluentClientDocs(private val coreCodegenContext: CoreCodegenCon | |||||||||
} | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
private fun renderCustomizableOperationSendMethod( | ||||||||||
runtimeConfig: RuntimeConfig, | ||||||||||
generics: FluentClientGenerics, | ||||||||||
writer: RustWriter, | ||||||||||
) { | ||||||||||
val smithyHttp = CargoDependency.SmithyHttp(runtimeConfig).asType() | ||||||||||
|
||||||||||
val operationGenerics = GenericsGenerator(GenericTypeArg("O"), GenericTypeArg("Retry")) | ||||||||||
val handleGenerics = generics.toGenericsGenerator() | ||||||||||
val combinedGenerics = operationGenerics + handleGenerics | ||||||||||
|
||||||||||
val codegenScope = arrayOf( | ||||||||||
"combined_generics_decl" to combinedGenerics.declaration(), | ||||||||||
"handle_generics_bounds" to handleGenerics.bounds(), | ||||||||||
"SdkSuccess" to smithyHttp.member("result::SdkSuccess"), | ||||||||||
"ClassifyResponse" to smithyHttp.member("retry::ClassifyResponse"), | ||||||||||
"ParseHttpResponse" to smithyHttp.member("response::ParseHttpResponse"), | ||||||||||
) | ||||||||||
|
||||||||||
writer.rustTemplate( | ||||||||||
""" | ||||||||||
impl#{combined_generics_decl:W} CustomizableOperation#{combined_generics_decl:W} | ||||||||||
where | ||||||||||
#{handle_generics_bounds:W} | ||||||||||
{ | ||||||||||
/// Sends this operation's request | ||||||||||
pub async fn send<T, E>(self) -> Result<T, SdkError<E>> | ||||||||||
where | ||||||||||
E: std::error::Error, | ||||||||||
O: #{ParseHttpResponse}<Output = Result<T, E>> + Send + Sync + Clone + 'static, | ||||||||||
Retry: #{ClassifyResponse}<#{SdkSuccess}<T>, SdkError<E>> + Send + Sync + Clone, | ||||||||||
{ | ||||||||||
self.handle.client.call(self.operation).await | ||||||||||
} | ||||||||||
} | ||||||||||
""", | ||||||||||
*codegenScope, | ||||||||||
) | ||||||||||
} |
75 changes: 75 additions & 0 deletions
75
aws/sdk/integration-tests/s3/tests/customizable-operation.rs
This file contains 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,75 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
use aws_http::user_agent::AwsUserAgent; | ||
use aws_sdk_s3::{Credentials, Region}; | ||
use aws_smithy_async::rt::sleep::TokioSleep; | ||
use aws_smithy_client::test_connection::capture_request; | ||
|
||
use std::convert::Infallible; | ||
use std::sync::Arc; | ||
use std::time::{Duration, UNIX_EPOCH}; | ||
|
||
#[tokio::test] | ||
async fn test_s3_ops_are_customizable() -> Result<(), aws_sdk_s3::Error> { | ||
let creds = Credentials::new( | ||
"ANOTREAL", | ||
"notrealrnrELgWzOk3IfjzDKtFBhDby", | ||
Some("notarealsessiontoken".to_string()), | ||
None, | ||
"test", | ||
); | ||
let conf = aws_sdk_s3::Config::builder() | ||
.credentials_provider(creds) | ||
.region(Region::new("us-east-1")) | ||
.sleep_impl(Arc::new(TokioSleep::new())) | ||
.build(); | ||
let (conn, rcvr) = capture_request(None); | ||
|
||
let client = aws_sdk_s3::Client::from_conf_conn(conf, conn); | ||
|
||
let op = client | ||
.list_buckets() | ||
.customize() | ||
.await | ||
.expect("list_buckets is customizable") | ||
.map_operation(|mut op| { | ||
op.properties_mut() | ||
.insert(UNIX_EPOCH + Duration::from_secs(1624036048)); | ||
op.properties_mut().insert(AwsUserAgent::for_tests()); | ||
|
||
Result::<_, Infallible>::Ok(op) | ||
}) | ||
.expect("inserting into the property bag is infallible"); | ||
|
||
// The response from the fake connection won't return the expected XML but we don't care about | ||
// that error in this test | ||
let _ = op | ||
.send() | ||
.await | ||
.expect_err("this will fail due to not receiving a proper XML response."); | ||
|
||
let expected_req = rcvr.expect_request(); | ||
let auth_header = expected_req | ||
.headers() | ||
.get("Authorization") | ||
.unwrap() | ||
.to_owned(); | ||
|
||
// This is a snapshot test taken from a known working test result | ||
let snapshot_signature = | ||
"Signature=c2028dc806248952fc533ab4b1d9f1bafcdc9b3380ed00482f9935541ae11671"; | ||
assert!( | ||
auth_header | ||
.to_str() | ||
.unwrap() | ||
.contains(snapshot_signature), | ||
"authorization header signature did not match expected signature: got {}, expected it to contain {}", | ||
auth_header.to_str().unwrap(), | ||
snapshot_signature | ||
); | ||
|
||
Ok(()) | ||
} |
This file contains 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 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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing newline.