aws_sdk_notifications/config/
endpoint.rs

1// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
2pub use ::aws_smithy_runtime_api::client::endpoint::EndpointFuture;
3pub use ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver;
4pub use ::aws_smithy_types::endpoint::Endpoint;
5
6/// Interceptor that tracks endpoint override business metric.
7#[derive(Debug, Default)]
8pub(crate) struct EndpointOverrideFeatureTrackerInterceptor;
9
10impl ::aws_smithy_runtime_api::client::interceptors::Intercept for EndpointOverrideFeatureTrackerInterceptor {
11    fn name(&self) -> &'static str {
12        "EndpointOverrideFeatureTrackerInterceptor"
13    }
14
15    fn read_before_execution(
16        &self,
17        _context: &::aws_smithy_runtime_api::client::interceptors::context::BeforeSerializationInterceptorContextRef<'_>,
18        cfg: &mut ::aws_smithy_types::config_bag::ConfigBag,
19    ) -> ::std::result::Result<(), ::aws_smithy_runtime_api::box_error::BoxError> {
20        if cfg.load::<::aws_types::endpoint_config::EndpointUrl>().is_some() {
21            cfg.interceptor_state()
22                .store_append(::aws_runtime::sdk_feature::AwsSdkFeature::EndpointOverride);
23        }
24        ::std::result::Result::Ok(())
25    }
26}
27
28#[cfg(test)]
29mod test {
30
31    /// For custom endpoint with region not set and fips disabled
32    #[test]
33    fn test_1() {
34        let params = crate::config::endpoint::Params::builder()
35            .endpoint("https://example.com".to_string())
36            .use_fips(false)
37            .build()
38            .expect("invalid params");
39        let resolver = crate::config::endpoint::DefaultResolver::new();
40        let endpoint = resolver.resolve_endpoint(&params);
41        let endpoint = endpoint.expect("Expected valid endpoint: https://example.com");
42        assert_eq!(
43            endpoint,
44            ::aws_smithy_types::endpoint::Endpoint::builder().url("https://example.com").build()
45        );
46    }
47
48    /// For custom endpoint with fips enabled
49    #[test]
50    fn test_2() {
51        let params = crate::config::endpoint::Params::builder()
52            .endpoint("https://example.com".to_string())
53            .use_fips(true)
54            .build()
55            .expect("invalid params");
56        let resolver = crate::config::endpoint::DefaultResolver::new();
57        let endpoint = resolver.resolve_endpoint(&params);
58        let error = endpoint
59            .expect_err("expected error: Invalid Configuration: FIPS and custom endpoint are not supported [For custom endpoint with fips enabled]");
60        assert_eq!(format!("{}", error), "Invalid Configuration: FIPS and custom endpoint are not supported")
61    }
62
63    /// For region us-east-1 with FIPS enabled and DualStack enabled
64    #[test]
65    fn test_3() {
66        let params = crate::config::endpoint::Params::builder()
67            .region("us-east-1".to_string())
68            .use_fips(true)
69            .build()
70            .expect("invalid params");
71        let resolver = crate::config::endpoint::DefaultResolver::new();
72        let endpoint = resolver.resolve_endpoint(&params);
73        let endpoint = endpoint.expect("Expected valid endpoint: https://notifications-fips.us-east-1.api.aws");
74        assert_eq!(
75            endpoint,
76            ::aws_smithy_types::endpoint::Endpoint::builder()
77                .url("https://notifications-fips.us-east-1.api.aws")
78                .build()
79        );
80    }
81
82    /// For region us-east-1 with FIPS disabled and DualStack enabled
83    #[test]
84    fn test_4() {
85        let params = crate::config::endpoint::Params::builder()
86            .region("us-east-1".to_string())
87            .use_fips(false)
88            .build()
89            .expect("invalid params");
90        let resolver = crate::config::endpoint::DefaultResolver::new();
91        let endpoint = resolver.resolve_endpoint(&params);
92        let endpoint = endpoint.expect("Expected valid endpoint: https://notifications.us-east-1.api.aws");
93        assert_eq!(
94            endpoint,
95            ::aws_smithy_types::endpoint::Endpoint::builder()
96                .url("https://notifications.us-east-1.api.aws")
97                .build()
98        );
99    }
100
101    /// For region cn-northwest-1 with FIPS enabled and DualStack enabled
102    #[test]
103    fn test_5() {
104        let params = crate::config::endpoint::Params::builder()
105            .region("cn-northwest-1".to_string())
106            .use_fips(true)
107            .build()
108            .expect("invalid params");
109        let resolver = crate::config::endpoint::DefaultResolver::new();
110        let endpoint = resolver.resolve_endpoint(&params);
111        let endpoint = endpoint.expect("Expected valid endpoint: https://notifications-fips.cn-northwest-1.api.amazonwebservices.com.cn");
112        assert_eq!(
113            endpoint,
114            ::aws_smithy_types::endpoint::Endpoint::builder()
115                .url("https://notifications-fips.cn-northwest-1.api.amazonwebservices.com.cn")
116                .build()
117        );
118    }
119
120    /// For region cn-northwest-1 with FIPS disabled and DualStack enabled
121    #[test]
122    fn test_6() {
123        let params = crate::config::endpoint::Params::builder()
124            .region("cn-northwest-1".to_string())
125            .use_fips(false)
126            .build()
127            .expect("invalid params");
128        let resolver = crate::config::endpoint::DefaultResolver::new();
129        let endpoint = resolver.resolve_endpoint(&params);
130        let endpoint = endpoint.expect("Expected valid endpoint: https://notifications.cn-northwest-1.api.amazonwebservices.com.cn");
131        assert_eq!(
132            endpoint,
133            ::aws_smithy_types::endpoint::Endpoint::builder()
134                .url("https://notifications.cn-northwest-1.api.amazonwebservices.com.cn")
135                .build()
136        );
137    }
138
139    /// For region us-gov-west-1 with FIPS enabled and DualStack enabled
140    #[test]
141    fn test_7() {
142        let params = crate::config::endpoint::Params::builder()
143            .region("us-gov-west-1".to_string())
144            .use_fips(true)
145            .build()
146            .expect("invalid params");
147        let resolver = crate::config::endpoint::DefaultResolver::new();
148        let endpoint = resolver.resolve_endpoint(&params);
149        let endpoint = endpoint.expect("Expected valid endpoint: https://notifications-fips.us-gov-west-1.api.aws");
150        assert_eq!(
151            endpoint,
152            ::aws_smithy_types::endpoint::Endpoint::builder()
153                .url("https://notifications-fips.us-gov-west-1.api.aws")
154                .build()
155        );
156    }
157
158    /// For region us-gov-west-1 with FIPS disabled and DualStack enabled
159    #[test]
160    fn test_8() {
161        let params = crate::config::endpoint::Params::builder()
162            .region("us-gov-west-1".to_string())
163            .use_fips(false)
164            .build()
165            .expect("invalid params");
166        let resolver = crate::config::endpoint::DefaultResolver::new();
167        let endpoint = resolver.resolve_endpoint(&params);
168        let endpoint = endpoint.expect("Expected valid endpoint: https://notifications.us-gov-west-1.api.aws");
169        assert_eq!(
170            endpoint,
171            ::aws_smithy_types::endpoint::Endpoint::builder()
172                .url("https://notifications.us-gov-west-1.api.aws")
173                .build()
174        );
175    }
176
177    /// Missing region
178    #[test]
179    fn test_9() {
180        let params = crate::config::endpoint::Params::builder().build().expect("invalid params");
181        let resolver = crate::config::endpoint::DefaultResolver::new();
182        let endpoint = resolver.resolve_endpoint(&params);
183        let error = endpoint.expect_err("expected error: Invalid Configuration: Missing Region [Missing region]");
184        assert_eq!(format!("{}", error), "Invalid Configuration: Missing Region")
185    }
186}
187
188/// Endpoint resolver trait specific to AWS User Notifications
189pub trait ResolveEndpoint: ::std::marker::Send + ::std::marker::Sync + ::std::fmt::Debug {
190    /// Resolve an endpoint with the given parameters
191    fn resolve_endpoint<'a>(&'a self, params: &'a crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a>;
192
193    /// Convert this service-specific resolver into a `SharedEndpointResolver`
194    ///
195    /// The resulting resolver will downcast `EndpointResolverParams` into `crate::config::endpoint::Params`.
196    fn into_shared_resolver(self) -> ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver
197    where
198        Self: Sized + 'static,
199    {
200        ::aws_smithy_runtime_api::client::endpoint::SharedEndpointResolver::new(DowncastParams(self))
201    }
202}
203
204#[derive(Debug)]
205struct DowncastParams<T>(T);
206impl<T> ::aws_smithy_runtime_api::client::endpoint::ResolveEndpoint for DowncastParams<T>
207where
208    T: ResolveEndpoint,
209{
210    fn resolve_endpoint<'a>(
211        &'a self,
212        params: &'a ::aws_smithy_runtime_api::client::endpoint::EndpointResolverParams,
213    ) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'a> {
214        let ep = match params.get::<crate::config::endpoint::Params>() {
215            Some(params) => self.0.resolve_endpoint(params),
216            None => ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(Err("params of expected type was not present".into())),
217        };
218        ep
219    }
220}
221
222/// The default endpoint resolver
223#[derive(Debug, Default)]
224pub struct DefaultResolver {
225    partition_resolver: crate::endpoint_lib::partition::PartitionResolver,
226}
227
228impl DefaultResolver {
229    /// Create a new endpoint resolver with default settings
230    pub fn new() -> Self {
231        Self {
232            partition_resolver: crate::endpoint_lib::DEFAULT_PARTITION_RESOLVER.clone(),
233        }
234    }
235
236    fn resolve_endpoint(
237        &self,
238        params: &crate::config::endpoint::Params,
239    ) -> ::std::result::Result<::aws_smithy_types::endpoint::Endpoint, ::aws_smithy_runtime_api::box_error::BoxError> {
240        let mut diagnostic_collector = crate::endpoint_lib::diagnostic::DiagnosticCollector::new();
241        Ok(
242            crate::config::endpoint::internals::resolve_endpoint(params, &mut diagnostic_collector, &self.partition_resolver)
243                .map_err(|err| err.with_source(diagnostic_collector.take_last_error()))?,
244        )
245    }
246}
247
248impl crate::config::endpoint::ResolveEndpoint for DefaultResolver {
249    fn resolve_endpoint(&self, params: &crate::config::endpoint::Params) -> ::aws_smithy_runtime_api::client::endpoint::EndpointFuture<'_> {
250        ::aws_smithy_runtime_api::client::endpoint::EndpointFuture::ready(self.resolve_endpoint(params))
251    }
252}
253
254#[non_exhaustive]
255#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::fmt::Debug)]
256/// Configuration parameters for resolving the correct endpoint
257pub struct Params {
258    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
259    pub(crate) use_fips: bool,
260    /// Override the endpoint used to send this request
261    pub(crate) endpoint: ::std::option::Option<::std::string::String>,
262    /// The AWS region used to dispatch the request.
263    pub(crate) region: ::std::option::Option<::std::string::String>,
264}
265impl Params {
266    /// Create a builder for [`Params`]
267    pub fn builder() -> crate::config::endpoint::ParamsBuilder {
268        crate::config::endpoint::ParamsBuilder::default()
269    }
270    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
271    pub fn use_fips(&self) -> ::std::option::Option<bool> {
272        Some(self.use_fips)
273    }
274    /// Override the endpoint used to send this request
275    pub fn endpoint(&self) -> ::std::option::Option<&str> {
276        self.endpoint.as_deref()
277    }
278    /// The AWS region used to dispatch the request.
279    pub fn region(&self) -> ::std::option::Option<&str> {
280        self.region.as_deref()
281    }
282}
283
284/// Builder for [`Params`]
285#[derive(::std::clone::Clone, ::std::cmp::PartialEq, ::std::default::Default, ::std::fmt::Debug)]
286pub struct ParamsBuilder {
287    use_fips: ::std::option::Option<bool>,
288    endpoint: ::std::option::Option<::std::string::String>,
289    region: ::std::option::Option<::std::string::String>,
290}
291impl ParamsBuilder {
292    /// Consume this builder, creating [`Params`].
293    pub fn build(self) -> ::std::result::Result<crate::config::endpoint::Params, crate::config::endpoint::InvalidParams> {
294        if let Some(region) = &self.region {
295            if !crate::endpoint_lib::host::is_valid_host_label(
296                region.as_ref() as &str,
297                true,
298                &mut crate::endpoint_lib::diagnostic::DiagnosticCollector::new(),
299            ) {
300                return Err(crate::config::endpoint::InvalidParams::invalid_value(
301                    "region",
302                    "must be a valid host label",
303                ));
304            }
305        };
306        Ok(
307            #[allow(clippy::unnecessary_lazy_evaluations)]
308            crate::config::endpoint::Params {
309                use_fips: self
310                    .use_fips
311                    .or_else(|| Some(false))
312                    .ok_or_else(|| crate::config::endpoint::InvalidParams::missing("use_fips"))?,
313                endpoint: self.endpoint,
314                region: self.region,
315            },
316        )
317    }
318    /// Sets the value for use_fips
319    ///
320    /// When unset, this parameter has a default value of `false`.
321    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
322    pub fn use_fips(mut self, value: impl Into<bool>) -> Self {
323        self.use_fips = Some(value.into());
324        self
325    }
326
327    /// Sets the value for use_fips
328    ///
329    /// When unset, this parameter has a default value of `false`.
330    /// When true, send this request to the FIPS-compliant regional endpoint. If the configured endpoint does not have a FIPS compliant endpoint, dispatching the request will return an error.
331    pub fn set_use_fips(mut self, param: Option<bool>) -> Self {
332        self.use_fips = param;
333        self
334    }
335    /// Sets the value for endpoint
336    ///
337    /// Override the endpoint used to send this request
338    pub fn endpoint(mut self, value: impl Into<::std::string::String>) -> Self {
339        self.endpoint = Some(value.into());
340        self
341    }
342
343    /// Sets the value for endpoint
344    ///
345    /// Override the endpoint used to send this request
346    pub fn set_endpoint(mut self, param: Option<::std::string::String>) -> Self {
347        self.endpoint = param;
348        self
349    }
350    /// Sets the value for region
351    ///
352    /// The AWS region used to dispatch the request.
353    pub fn region(mut self, value: impl Into<::std::string::String>) -> Self {
354        self.region = Some(value.into());
355        self
356    }
357
358    /// Sets the value for region
359    ///
360    /// The AWS region used to dispatch the request.
361    pub fn set_region(mut self, param: Option<::std::string::String>) -> Self {
362        self.region = param;
363        self
364    }
365}
366
367/// An error that occurred during endpoint resolution
368#[derive(Debug)]
369pub struct InvalidParams {
370    field: std::borrow::Cow<'static, str>,
371    kind: InvalidParamsErrorKind,
372}
373
374/// The kind of invalid parameter error
375#[derive(Debug)]
376enum InvalidParamsErrorKind {
377    MissingField,
378    InvalidValue { message: &'static str },
379}
380
381impl InvalidParams {
382    #[allow(dead_code)]
383    fn missing(field: &'static str) -> Self {
384        Self {
385            field: field.into(),
386            kind: InvalidParamsErrorKind::MissingField,
387        }
388    }
389
390    #[allow(dead_code)]
391    fn invalid_value(field: &'static str, message: &'static str) -> Self {
392        Self {
393            field: field.into(),
394            kind: InvalidParamsErrorKind::InvalidValue { message },
395        }
396    }
397}
398
399impl std::fmt::Display for InvalidParams {
400    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
401        match self.kind {
402            InvalidParamsErrorKind::MissingField => write!(f, "a required field was missing: `{}`", self.field),
403            InvalidParamsErrorKind::InvalidValue { message } => write!(f, "invalid value for field: `{}` - {}", self.field, message),
404        }
405    }
406}
407
408impl std::error::Error for InvalidParams {}
409
410mod internals;