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