-
Notifications
You must be signed in to change notification settings - Fork 179
/
HttpConnectorConfigCustomization.kt
150 lines (143 loc) · 6.83 KB
/
HttpConnectorConfigCustomization.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
package software.amazon.smithy.rustsdk
import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.customize.ClientCodegenDecorator
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.config.ServiceConfig
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.rust
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.CodegenContext
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType
class HttpConnectorDecorator : ClientCodegenDecorator {
override val name: String = "HttpConnectorDecorator"
override val order: Byte = 0
override fun configCustomizations(
codegenContext: ClientCodegenContext,
baseCustomizations: List<ConfigCustomization>,
): List<ConfigCustomization> {
return baseCustomizations + HttpConnectorConfigCustomization(codegenContext)
}
}
class HttpConnectorConfigCustomization(
codegenContext: CodegenContext,
) : ConfigCustomization() {
private val runtimeConfig = codegenContext.runtimeConfig
private val moduleUseName = codegenContext.moduleUseName()
private val codegenScope = arrayOf(
"HttpConnector" to RuntimeType.smithyClient(runtimeConfig).resolve("http_connector::HttpConnector"),
)
override fun section(section: ServiceConfig): Writable {
return when (section) {
is ServiceConfig.ConfigStruct -> writable {
rustTemplate("http_connector: Option<#{HttpConnector}>,", *codegenScope)
}
is ServiceConfig.ConfigImpl -> writable {
rustTemplate(
"""
/// Return an [`HttpConnector`](#{HttpConnector}) to use when making requests, if any.
pub fn http_connector(&self) -> Option<&#{HttpConnector}> {
self.http_connector.as_ref()
}
""",
*codegenScope,
)
}
is ServiceConfig.BuilderStruct -> writable {
rustTemplate("http_connector: Option<#{HttpConnector}>,", *codegenScope)
}
ServiceConfig.BuilderImpl -> writable {
rustTemplate(
"""
/// Sets the HTTP connector to use when making requests.
///
/// ## Examples
/// ```no_run
/// ## ##[cfg(test)]
/// ## mod tests {
/// ## ##[test]
/// ## fn example() {
/// use std::time::Duration;
/// use aws_smithy_client::{Client, hyper_ext};
/// use aws_smithy_client::erase::DynConnector;
/// use aws_smithy_client::http_connector::ConnectorSettings;
/// use $moduleUseName::config::Config;
///
/// let https_connector = hyper_rustls::HttpsConnectorBuilder::new()
/// .with_webpki_roots()
/// .https_only()
/// .enable_http1()
/// .enable_http2()
/// .build();
/// let smithy_connector = hyper_ext::Adapter::builder()
/// // Optionally set things like timeouts as well
/// .connector_settings(
/// ConnectorSettings::builder()
/// .connect_timeout(Duration::from_secs(5))
/// .build()
/// )
/// .build(https_connector);
/// ## }
/// ## }
/// ```
pub fn http_connector(mut self, http_connector: impl Into<#{HttpConnector}>) -> Self {
self.http_connector = Some(http_connector.into());
self
}
/// Sets the HTTP connector to use when making requests.
///
/// ## Examples
/// ```no_run
/// ## ##[cfg(test)]
/// ## mod tests {
/// ## ##[test]
/// ## fn example() {
/// use std::time::Duration;
/// use aws_smithy_client::hyper_ext;
/// use aws_smithy_client::http_connector::ConnectorSettings;
/// use crate::sdk_config::{SdkConfig, Builder};
/// use $moduleUseName::config::{Builder, Config};
///
/// fn override_http_connector(builder: &mut Builder) {
/// let https_connector = hyper_rustls::HttpsConnectorBuilder::new()
/// .with_webpki_roots()
/// .https_only()
/// .enable_http1()
/// .enable_http2()
/// .build();
/// let smithy_connector = hyper_ext::Adapter::builder()
/// // Optionally set things like timeouts as well
/// .connector_settings(
/// ConnectorSettings::builder()
/// .connect_timeout(Duration::from_secs(5))
/// .build()
/// )
/// .build(https_connector);
/// builder.set_http_connector(Some(smithy_connector));
/// }
///
/// let mut builder = $moduleUseName::Config::builder();
/// override_http_connector(&mut builder);
/// let config = builder.build();
/// ## }
/// ## }
/// ```
pub fn set_http_connector(&mut self, http_connector: Option<impl Into<#{HttpConnector}>>) -> &mut Self {
self.http_connector = http_connector.map(|inner| inner.into());
self
}
""",
*codegenScope,
)
}
is ServiceConfig.BuilderBuild -> writable {
rust("http_connector: self.http_connector,")
}
else -> emptySection
}
}
}