diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt index 8fe4d9002469fdc9a44bfd3746a2ec018fbfe707..5ffd15a6a8aef55daa2bb622094a438924199949 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/AwsPresigningDecorator.kt @@ -320,9 +320,8 @@ class AwsPresignedFluentBuilderMethod( """ #{alternate_presigning_serializer} - let runtime_plugins = #{Operation}::register_runtime_plugins( - #{RuntimePlugins}::new(), - self.handle.clone(), + let runtime_plugins = #{Operation}::operation_runtime_plugins( + self.handle.runtime_plugins.clone(), self.config_override ) .with_client_plugin(#{SigV4PresigningRuntimePlugin}::new(presigning_config, #{payload_override})) @@ -344,8 +343,7 @@ class AwsPresignedFluentBuilderMethod( *codegenScope, "Operation" to codegenContext.symbolProvider.toSymbol(section.operationShape), "OperationError" to section.operationErrorType, - "RuntimePlugins" to RuntimeType.smithyRuntimeApi(runtimeConfig) - .resolve("client::runtime_plugin::RuntimePlugins"), + "RuntimePlugins" to RuntimeType.runtimePlugins(runtimeConfig), "SharedInterceptor" to RuntimeType.smithyRuntimeApi(runtimeConfig).resolve("client::interceptors") .resolve("SharedInterceptor"), "SigV4PresigningRuntimePlugin" to AwsRuntimeType.presigningInterceptor(runtimeConfig) diff --git a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt index 4ca6c2453493af6ae1218c9be478e12a209a3a49..f73d1f84831a2a673a9cecad9619d820322ad9b2 100644 --- a/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt +++ b/aws/sdk-codegen/src/main/kotlin/software/amazon/smithy/rustsdk/CredentialProviders.kt @@ -139,7 +139,7 @@ class CredentialsIdentityResolverRegistration( override fun section(section: ServiceRuntimePluginSection): Writable = writable { when (section) { is ServiceRuntimePluginSection.AdditionalConfig -> { - rustBlockTemplate("if let Some(credentials_cache) = self.handle.conf.credentials_cache()") { + rustBlockTemplate("if let Some(credentials_cache) = ${section.serviceConfigName}.credentials_cache()") { section.registerIdentityResolver(this, runtimeConfig) { rustTemplate( """ diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/OperationGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/OperationGenerator.kt index 0110a1620fea43fa776db22bd91105ad6b90ef9e..b59ff1c33951acbe2728bd00185dbea2b9876ff4 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/OperationGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/OperationGenerator.kt @@ -7,7 +7,6 @@ package software.amazon.smithy.rust.codegen.client.smithy.generators import software.amazon.smithy.model.shapes.OperationShape import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext -import software.amazon.smithy.rust.codegen.client.smithy.ClientRustModule import software.amazon.smithy.rust.codegen.client.smithy.customize.AuthOption import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator import software.amazon.smithy.rust.codegen.client.smithy.endpoint.generators.EndpointParamsInterceptorGenerator @@ -23,7 +22,6 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rust import software.amazon.smithy.rust.codegen.core.rustlang.rustBlock import software.amazon.smithy.rust.codegen.core.rustlang.rustTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable -import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.preludeScope import software.amazon.smithy.rust.codegen.core.smithy.customize.writeCustomizations @@ -45,39 +43,6 @@ open class OperationGenerator( // TODO(enableNewSmithyRuntimeCleanup): Remove the `traitGenerator` private val traitGenerator: HttpBoundProtocolTraitImplGenerator, ) { - companion object { - fun registerDefaultRuntimePluginsFn(runtimeConfig: RuntimeConfig): RuntimeType = - RuntimeType.forInlineFun("register_default_runtime_plugins", ClientRustModule.Operation) { - rustTemplate( - """ - pub(crate) fn register_default_runtime_plugins( - runtime_plugins: #{RuntimePlugins}, - operation: impl #{RuntimePlugin} + 'static, - handle: #{Arc}, - config_override: #{Option}, - ) -> #{RuntimePlugins} { - let mut runtime_plugins = runtime_plugins - .with_client_plugin(handle.conf.clone()) - .with_client_plugin(crate::config::ServiceRuntimePlugin::new(handle)) - .with_client_plugin(#{NoAuthRuntimePlugin}::new()) - .with_operation_plugin(operation); - if let Some(config_override) = config_override { - runtime_plugins = runtime_plugins.with_operation_plugin(config_override); - } - runtime_plugins - } - """, - *preludeScope, - "Arc" to RuntimeType.Arc, - "RuntimePlugin" to RuntimeType.runtimePlugin(runtimeConfig), - "RuntimePlugins" to RuntimeType.smithyRuntimeApi(runtimeConfig) - .resolve("client::runtime_plugin::RuntimePlugins"), - "NoAuthRuntimePlugin" to RuntimeType.smithyRuntime(runtimeConfig) - .resolve("client::auth::no_auth::NoAuthRuntimePlugin"), - ) - } - } - private val model = codegenContext.model private val runtimeConfig = codegenContext.runtimeConfig private val symbolProvider = codegenContext.symbolProvider @@ -180,18 +145,16 @@ open class OperationGenerator( #{invoke_with_stop_point}(input, runtime_plugins, stop_point).await } - pub(crate) fn register_runtime_plugins( - runtime_plugins: #{RuntimePlugins}, - handle: #{Arc}, + pub(crate) fn operation_runtime_plugins( + client_runtime_plugins: #{RuntimePlugins}, config_override: #{Option}, ) -> #{RuntimePlugins} { - #{register_default_runtime_plugins}( - runtime_plugins, - Self::new(), - handle, - config_override - ) - #{additional_runtime_plugins} + let mut runtime_plugins = client_runtime_plugins.with_operation_plugin(Self::new()); + if let Some(config_override) = config_override { + runtime_plugins = runtime_plugins.with_operation_plugin(config_override); + } + runtime_plugins + #{additional_runtime_plugins} } """, *codegenScope, @@ -200,10 +163,9 @@ open class OperationGenerator( "InterceptorContext" to RuntimeType.interceptorContext(runtimeConfig), "OrchestratorError" to RuntimeType.smithyRuntimeApi(runtimeConfig).resolve("client::orchestrator::error::OrchestratorError"), "RuntimePlugin" to RuntimeType.runtimePlugin(runtimeConfig), - "RuntimePlugins" to RuntimeType.smithyRuntimeApi(runtimeConfig).resolve("client::runtime_plugin::RuntimePlugins"), + "RuntimePlugins" to RuntimeType.runtimePlugins(runtimeConfig), "StopPoint" to RuntimeType.smithyRuntime(runtimeConfig).resolve("client::orchestrator::StopPoint"), "invoke_with_stop_point" to RuntimeType.smithyRuntime(runtimeConfig).resolve("client::orchestrator::invoke_with_stop_point"), - "register_default_runtime_plugins" to registerDefaultRuntimePluginsFn(runtimeConfig), "additional_runtime_plugins" to writable { writeCustomizations( operationCustomizations, diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/PaginatorGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/PaginatorGenerator.kt index fc72dbb564737c7a0d6d382ee2bee7eabdbbf8be..5e18a991146c68dfaee35ef58630bb9734fadb57 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/PaginatorGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/PaginatorGenerator.kt @@ -240,15 +240,13 @@ class PaginatorGenerator private constructor( if (codegenContext.smithyRuntimeMode.defaultToOrchestrator) { rustTemplate( """ - let runtime_plugins = #{operation}::register_runtime_plugins( - #{RuntimePlugins}::new(), - handle, - None + let runtime_plugins = #{operation}::operation_runtime_plugins( + handle.runtime_plugins.clone(), + None, ); """, *codegenScope, - "RuntimePlugins" to RuntimeType.smithyRuntimeApi(runtimeConfig) - .resolve("client::runtime_plugin::RuntimePlugins"), + "RuntimePlugins" to RuntimeType.runtimePlugins(runtimeConfig), ) } }, diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceRuntimePluginGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceRuntimePluginGenerator.kt index bf47e9f41c5045ec5810f9606139e282ef7a7cd2..b8023cd6c4a65fc24e73e91d552d1cb1f6bf0449 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceRuntimePluginGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/ServiceRuntimePluginGenerator.kt @@ -31,7 +31,7 @@ sealed class ServiceRuntimePluginSection(name: String) : Section(name) { /** * Hook for adding additional things to config inside service runtime plugins. */ - data class AdditionalConfig(val newLayerName: String) : ServiceRuntimePluginSection("AdditionalConfig") { + data class AdditionalConfig(val newLayerName: String, val serviceConfigName: String) : ServiceRuntimePluginSection("AdditionalConfig") { /** Adds a value to the config bag */ fun putConfigValue(writer: RustWriter, value: Writable) { writer.rust("$newLayerName.store_put(#T);", value) @@ -107,27 +107,28 @@ class ServiceRuntimePluginGenerator( customizations: List, ) { val additionalConfig = writable { - writeCustomizations(customizations, ServiceRuntimePluginSection.AdditionalConfig("cfg")) + writeCustomizations(customizations, ServiceRuntimePluginSection.AdditionalConfig("cfg", "_service_config")) } writer.rustTemplate( """ - // TODO(enableNewSmithyRuntimeLaunch) Remove `allow(dead_code)` as well as a field `handle` when - // the field is no longer used. - ##[allow(dead_code)] ##[derive(Debug)] pub(crate) struct ServiceRuntimePlugin { - handle: #{Arc}, + config: #{Option}<#{FrozenLayer}>, } impl ServiceRuntimePlugin { - pub fn new(handle: #{Arc}) -> Self { - Self { handle } + pub fn new(_service_config: crate::config::Config) -> Self { + Self { + config: { + #{config} + }, + } } } impl #{RuntimePlugin} for ServiceRuntimePlugin { fn config(&self) -> #{Option}<#{FrozenLayer}> { - #{config} + self.config.clone() } fn interceptors(&self, interceptors: &mut #{InterceptorRegistrar}) { @@ -145,13 +146,7 @@ class ServiceRuntimePluginGenerator( rustTemplate( """ let mut cfg = #{Layer}::new(${codegenContext.serviceShape.id.name.dq()}); - - // TODO(enableNewSmithyRuntimeLaunch): Make it possible to set retry classifiers at the service level. - // Retry classifiers can also be set at the operation level and those should be added to the - // list of classifiers defined here, rather than replacing them. - #{additional_config} - Some(cfg.freeze()) """, *codegenScope, diff --git a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientGenerator.kt b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientGenerator.kt index 3f2f451f440016f0a39ae9803cda3417d1d767b7..3292ae10bbd6ed175a595fce1d45cdb401d8a544 100644 --- a/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientGenerator.kt +++ b/codegen-client/src/main/kotlin/software/amazon/smithy/rust/codegen/client/smithy/generators/client/FluentClientGenerator.kt @@ -41,6 +41,7 @@ import software.amazon.smithy.rust.codegen.core.rustlang.rustTypeParameters import software.amazon.smithy.rust.codegen.core.rustlang.stripOuter import software.amazon.smithy.rust.codegen.core.rustlang.withBlockTemplate import software.amazon.smithy.rust.codegen.core.rustlang.writable +import software.amazon.smithy.rust.codegen.core.smithy.RuntimeConfig import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType.Companion.preludeScope import software.amazon.smithy.rust.codegen.core.smithy.RustCrate @@ -125,6 +126,7 @@ class FluentClientGenerator( } }, "RetryConfig" to RuntimeType.smithyTypes(runtimeConfig).resolve("retry::RetryConfig"), + "RuntimePlugins" to RuntimeType.runtimePlugins(runtimeConfig), "TimeoutConfig" to RuntimeType.smithyTypes(runtimeConfig).resolve("timeout::TimeoutConfig"), // TODO(enableNewSmithyRuntimeCleanup): Delete the generics when cleaning up middleware "generics_decl" to generics.decl, @@ -182,18 +184,13 @@ class FluentClientGenerator( ##[derive(Debug)] pub(crate) struct Handle { pub(crate) conf: crate::Config, + pub(crate) runtime_plugins: #{RuntimePlugins}, } #{client_docs:W} - ##[derive(::std::fmt::Debug)] + ##[derive(#{Clone}, ::std::fmt::Debug)] pub struct Client { - handle: #{Arc} - } - - impl #{Clone} for Client { - fn clone(&self) -> Self { - Self { handle: self.handle.clone() } - } + handle: #{Arc}, } impl Client { @@ -215,7 +212,12 @@ class FluentClientGenerator( } Self { - handle: #{Arc}::new(Handle { conf }) + handle: #{Arc}::new( + Handle { + conf: conf.clone(), + runtime_plugins: #{base_client_runtime_plugins}(conf), + } + ) } } @@ -229,9 +231,7 @@ class FluentClientGenerator( // This is currently kept around so the tests still compile in both modes /// Creates a client with the given service configuration. pub fn with_config(_client: #{client}::Client, conf: crate::Config) -> Self { - Self { - handle: #{Arc}::new(Handle { conf }) - } + Self::from_conf(conf) } ##[doc(hidden)] @@ -244,6 +244,7 @@ class FluentClientGenerator( } """, *clientScope, + "base_client_runtime_plugins" to baseClientRuntimePluginsFn(runtimeConfig), ) } } @@ -505,8 +506,7 @@ class FluentClientGenerator( "Operation" to operationSymbol, "OperationError" to errorType, "OperationOutput" to outputType, - "RuntimePlugins" to RuntimeType.smithyRuntimeApi(runtimeConfig) - .resolve("client::runtime_plugin::RuntimePlugins"), + "RuntimePlugins" to RuntimeType.runtimePlugins(runtimeConfig), "SendResult" to ClientRustModule.Client.customize.toType() .resolve("internal::SendResult"), "SdkError" to RuntimeType.sdkError(runtimeConfig), @@ -516,9 +516,8 @@ class FluentClientGenerator( ##[doc(hidden)] pub async fn send_orchestrator(self) -> #{Result}<#{OperationOutput}, #{SdkError}<#{OperationError}, #{HttpResponse}>> { let input = self.inner.build().map_err(#{SdkError}::construction_failure)?; - let runtime_plugins = #{Operation}::register_runtime_plugins( - #{RuntimePlugins}::new(), - self.handle, + let runtime_plugins = #{Operation}::operation_runtime_plugins( + self.handle.runtime_plugins.clone(), self.config_override ); #{Operation}::orchestrate(&runtime_plugins, input).await @@ -647,6 +646,26 @@ class FluentClientGenerator( } } +private fun baseClientRuntimePluginsFn(runtimeConfig: RuntimeConfig): RuntimeType = + RuntimeType.forInlineFun("base_client_runtime_plugins", ClientRustModule.config) { + rustTemplate( + """ + pub(crate) fn base_client_runtime_plugins( + config: crate::Config, + ) -> #{RuntimePlugins} { + #{RuntimePlugins}::new() + .with_client_plugin(config.clone()) + .with_client_plugin(crate::config::ServiceRuntimePlugin::new(config)) + .with_client_plugin(#{NoAuthRuntimePlugin}::new()) + } + """, + *preludeScope, + "RuntimePlugins" to RuntimeType.runtimePlugins(runtimeConfig), + "NoAuthRuntimePlugin" to RuntimeType.smithyRuntime(runtimeConfig) + .resolve("client::auth::no_auth::NoAuthRuntimePlugin"), + ) + } + /** * For a given `operation` shape, return a list of strings where each string describes the name and input type of one of * the operation's corresponding fluent builder methods as well as that method's documentation from the smithy model diff --git a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt index 89854d42bab14734288f36e88df8cb7e1af630c3..cbf2514157a0e0222c342672df6d68bfdd5bb7f9 100644 --- a/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt +++ b/codegen-core/src/main/kotlin/software/amazon/smithy/rust/codegen/core/smithy/RuntimeType.kt @@ -338,6 +338,8 @@ data class RuntimeType(val path: String, val dependency: RustDependency? = null) smithyTypes(runtimeConfig).resolve("config_bag::ConfigBag") fun configBagAccessors(runtimeConfig: RuntimeConfig): RuntimeType = smithyRuntimeApi(runtimeConfig).resolve("client::config_bag_accessors::ConfigBagAccessors") + fun runtimePlugins(runtimeConfig: RuntimeConfig): RuntimeType = + smithyRuntimeApi(runtimeConfig).resolve("client::runtime_plugin::RuntimePlugins") fun boxError(runtimeConfig: RuntimeConfig): RuntimeType = smithyRuntimeApi(runtimeConfig).resolve("box_error::BoxError") fun interceptor(runtimeConfig: RuntimeConfig): RuntimeType =