aws_smithy_runtime_api/client/
endpoint.rs

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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

//! APIs needed to configure endpoint resolution for clients.

use crate::box_error::BoxError;
use crate::client::runtime_components::sealed::ValidateConfig;
use crate::impl_shared_conversions;
use aws_smithy_types::config_bag::{Storable, StoreReplace};
use aws_smithy_types::endpoint::Endpoint;
use aws_smithy_types::type_erasure::TypeErasedBox;
use error::InvalidEndpointError;
use http_02x::uri::Authority;
use std::any::TypeId;
use std::collections::HashMap;
use std::fmt;
use std::str::FromStr;
use std::sync::Arc;

new_type_future! {
    #[doc = "Future for [`ResolveEndpoint::resolve_endpoint`]."]
    pub struct EndpointFuture<'a, Endpoint, BoxError>;
}

/// Parameters originating from the Smithy endpoint ruleset required for endpoint resolution.
///
/// The actual endpoint parameters are code generated from the Smithy model, and thus,
/// are not known to the runtime crates. Hence, this struct is really a new-type around
/// a [`TypeErasedBox`] that holds the actual concrete parameters in it.
///
/// This struct allows the caller to store and retrieve properties of arbitrary types.
/// These arbitrary properties are intended to be incorporated into the concrete parameters
/// by [`ResolveEndpoint::finalize_params`].
#[derive(Debug)]
pub struct EndpointResolverParams {
    inner: TypeErasedBox,
    property: HashMap<TypeId, TypeErasedBox>,
}

impl EndpointResolverParams {
    /// Creates a new [`EndpointResolverParams`] from a concrete parameters instance.
    pub fn new<T: fmt::Debug + Send + Sync + 'static>(params: T) -> Self {
        Self {
            inner: TypeErasedBox::new(params),
            property: HashMap::new(),
        }
    }

    /// Attempts to downcast the underlying concrete parameters to `T` and return it as a reference.
    pub fn get<T: fmt::Debug + Send + Sync + 'static>(&self) -> Option<&T> {
        self.inner.downcast_ref()
    }

    /// Attempts to downcast the underlying concrete parameters to `T` and return it as a mutable reference.
    pub fn get_mut<T: fmt::Debug + Send + Sync + 'static>(&mut self) -> Option<&mut T> {
        self.inner.downcast_mut()
    }

    /// Sets property of an arbitrary type `T` for the endpoint resolver params.
    pub fn set_property<T: fmt::Debug + Send + Sync + 'static>(&mut self, t: T) {
        self.property
            .insert(TypeId::of::<T>(), TypeErasedBox::new(t));
    }

    /// Attempts to retrieve a reference to property of a given type `T`.
    pub fn get_property<T: fmt::Debug + Send + Sync + 'static>(&self) -> Option<&T> {
        self.property
            .get(&TypeId::of::<T>())
            .and_then(|b| b.downcast_ref())
    }

    /// Attempts to retrieve a mutable reference to property of a given type `T`.
    pub fn get_property_mut<T: fmt::Debug + Send + Sync + 'static>(&mut self) -> Option<&mut T> {
        self.property
            .get_mut(&TypeId::of::<T>())
            .and_then(|b| b.downcast_mut())
    }
}

impl Storable for EndpointResolverParams {
    type Storer = StoreReplace<Self>;
}

/// Configurable endpoint resolver implementation.
pub trait ResolveEndpoint: Send + Sync + fmt::Debug {
    /// Asynchronously resolves an endpoint to use from the given endpoint parameters.
    fn resolve_endpoint<'a>(&'a self, params: &'a EndpointResolverParams) -> EndpointFuture<'a>;

    /// Finalize the service-specific concrete parameters in `_params`.
    ///
    /// The `EndpointResolverParams` may need to include additional data at a later point,
    /// after its creation in the `read_before_execution` method of an endpoint parameters interceptor.
    /// Modifying it directly within the [`ResolveEndpoint::resolve_endpoint`] method is not feasible,
    /// as `params` is passed by reference. This means that incorporating extra data would require
    /// cloning `params` within the method. However, the return type `EndpointFuture` has a lifetime
    /// tied to the input argument, making it impossible to return the cloned `params`, as its lifetime
    /// is scoped to the method.
    fn finalize_params<'a>(
        &'a self,
        _params: &'a mut EndpointResolverParams,
    ) -> Result<(), BoxError> {
        Ok(())
    }
}

/// Shared endpoint resolver.
///
/// This is a simple shared ownership wrapper type for the [`ResolveEndpoint`] trait.
#[derive(Clone, Debug)]
pub struct SharedEndpointResolver(Arc<dyn ResolveEndpoint>);

impl SharedEndpointResolver {
    /// Creates a new [`SharedEndpointResolver`].
    pub fn new(endpoint_resolver: impl ResolveEndpoint + 'static) -> Self {
        Self(Arc::new(endpoint_resolver))
    }
}

impl ResolveEndpoint for SharedEndpointResolver {
    fn resolve_endpoint<'a>(&'a self, params: &'a EndpointResolverParams) -> EndpointFuture<'a> {
        self.0.resolve_endpoint(params)
    }

    fn finalize_params<'a>(
        &'a self,
        params: &'a mut EndpointResolverParams,
    ) -> Result<(), BoxError> {
        self.0.finalize_params(params)
    }
}

impl ValidateConfig for SharedEndpointResolver {}

impl_shared_conversions!(convert SharedEndpointResolver from ResolveEndpoint using SharedEndpointResolver::new);

/// A special type that adds support for services that have special URL-prefixing rules.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EndpointPrefix(String);
impl EndpointPrefix {
    /// Create a new endpoint prefix from an `impl Into<String>`. If the prefix argument is invalid,
    /// a [`InvalidEndpointError`] will be returned.
    pub fn new(prefix: impl Into<String>) -> Result<Self, InvalidEndpointError> {
        let prefix = prefix.into();
        match Authority::from_str(&prefix) {
            Ok(_) => Ok(EndpointPrefix(prefix)),
            Err(err) => Err(InvalidEndpointError::failed_to_construct_authority(
                prefix, err,
            )),
        }
    }

    /// Get the `str` representation of this `EndpointPrefix`.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl Storable for EndpointPrefix {
    type Storer = StoreReplace<Self>;
}

/// Errors related to endpoint resolution and validation
pub mod error {
    use crate::box_error::BoxError;
    use std::error::Error as StdError;
    use std::fmt;

    /// Endpoint resolution failed
    #[derive(Debug)]
    pub struct ResolveEndpointError {
        message: String,
        source: Option<BoxError>,
    }

    impl ResolveEndpointError {
        /// Create an [`ResolveEndpointError`] with a message
        pub fn message(message: impl Into<String>) -> Self {
            Self {
                message: message.into(),
                source: None,
            }
        }

        /// Add a source to the error
        pub fn with_source(self, source: Option<BoxError>) -> Self {
            Self { source, ..self }
        }

        /// Create a [`ResolveEndpointError`] from a message and a source
        pub fn from_source(message: impl Into<String>, source: impl Into<BoxError>) -> Self {
            Self::message(message).with_source(Some(source.into()))
        }
    }

    impl fmt::Display for ResolveEndpointError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            write!(f, "{}", self.message)
        }
    }

    impl StdError for ResolveEndpointError {
        fn source(&self) -> Option<&(dyn StdError + 'static)> {
            self.source.as_ref().map(|err| err.as_ref() as _)
        }
    }

    #[derive(Debug)]
    pub(super) enum InvalidEndpointErrorKind {
        EndpointMustHaveScheme,
        FailedToConstructAuthority { authority: String, source: BoxError },
        FailedToConstructUri { source: BoxError },
    }

    /// An error that occurs when an endpoint is found to be invalid. This usually occurs due to an
    /// incomplete URI.
    #[derive(Debug)]
    pub struct InvalidEndpointError {
        pub(super) kind: InvalidEndpointErrorKind,
    }

    impl InvalidEndpointError {
        /// Construct a build error for a missing scheme
        pub fn endpoint_must_have_scheme() -> Self {
            Self {
                kind: InvalidEndpointErrorKind::EndpointMustHaveScheme,
            }
        }

        /// Construct a build error for an invalid authority
        pub fn failed_to_construct_authority(
            authority: impl Into<String>,
            source: impl Into<Box<dyn StdError + Send + Sync + 'static>>,
        ) -> Self {
            Self {
                kind: InvalidEndpointErrorKind::FailedToConstructAuthority {
                    authority: authority.into(),
                    source: source.into(),
                },
            }
        }

        /// Construct a build error for an invalid URI
        pub fn failed_to_construct_uri(
            source: impl Into<Box<dyn StdError + Send + Sync + 'static>>,
        ) -> Self {
            Self {
                kind: InvalidEndpointErrorKind::FailedToConstructUri {
                    source: source.into(),
                },
            }
        }
    }

    impl From<InvalidEndpointErrorKind> for InvalidEndpointError {
        fn from(kind: InvalidEndpointErrorKind) -> Self {
            Self { kind }
        }
    }

    impl fmt::Display for InvalidEndpointError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            use InvalidEndpointErrorKind as ErrorKind;
            match &self.kind {
            ErrorKind::EndpointMustHaveScheme => write!(f, "endpoint must contain a valid scheme"),
            ErrorKind::FailedToConstructAuthority { authority, source: _ } => write!(
                f,
                "endpoint must contain a valid authority when combined with endpoint prefix: {authority}"
            ),
            ErrorKind::FailedToConstructUri { .. } => write!(f, "failed to construct URI"),
        }
        }
    }

    impl StdError for InvalidEndpointError {
        fn source(&self) -> Option<&(dyn StdError + 'static)> {
            use InvalidEndpointErrorKind as ErrorKind;
            match &self.kind {
                ErrorKind::FailedToConstructUri { source } => Some(source.as_ref()),
                ErrorKind::FailedToConstructAuthority {
                    authority: _,
                    source,
                } => Some(source.as_ref()),
                ErrorKind::EndpointMustHaveScheme => None,
            }
        }
    }
}