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