aws_smithy_runtime/client/
endpoint.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6//! Code for applying endpoints to a request.
7
8use aws_smithy_runtime_api::client::endpoint::{error::InvalidEndpointError, EndpointPrefix};
9use std::borrow::Cow;
10use std::result::Result as StdResult;
11use std::str::FromStr;
12
13/// Apply `endpoint` to `uri`
14///
15/// This method mutates `uri` by setting the `endpoint` on it
16#[deprecated(
17    since = "1.8.0",
18    note = "Depends on pre 1.x http types. May be removed or feature gated in a future minor version."
19)]
20pub fn apply_endpoint(
21    uri: &mut http_02x::Uri,
22    endpoint: &http_02x::Uri,
23    prefix: Option<&EndpointPrefix>,
24) -> StdResult<(), InvalidEndpointError> {
25    let prefix = prefix.map(EndpointPrefix::as_str).unwrap_or("");
26    let authority = endpoint
27        .authority()
28        .as_ref()
29        .map(|auth| auth.as_str())
30        .unwrap_or("");
31    let authority = if !prefix.is_empty() {
32        Cow::Owned(format!("{}{}", prefix, authority))
33    } else {
34        Cow::Borrowed(authority)
35    };
36    let authority = http_02x::uri::Authority::from_str(&authority).map_err(|err| {
37        InvalidEndpointError::failed_to_construct_authority(authority.into_owned(), err)
38    })?;
39    let scheme = *endpoint
40        .scheme()
41        .as_ref()
42        .ok_or_else(InvalidEndpointError::endpoint_must_have_scheme)?;
43    let new_uri = http_02x::Uri::builder()
44        .authority(authority)
45        .scheme(scheme.clone())
46        .path_and_query(merge_paths(endpoint, uri).as_ref())
47        .build()
48        .map_err(InvalidEndpointError::failed_to_construct_uri)?;
49    *uri = new_uri;
50    Ok(())
51}
52
53fn merge_paths<'a>(endpoint: &'a http_02x::Uri, uri: &'a http_02x::Uri) -> Cow<'a, str> {
54    if let Some(query) = endpoint.path_and_query().and_then(|pq| pq.query()) {
55        tracing::warn!(query = %query, "query specified in endpoint will be ignored during endpoint resolution");
56    }
57    let endpoint_path = endpoint.path();
58    let uri_path_and_query = uri.path_and_query().map(|pq| pq.as_str()).unwrap_or("");
59    if endpoint_path.is_empty() {
60        Cow::Borrowed(uri_path_and_query)
61    } else {
62        let ep_no_slash = endpoint_path.strip_suffix('/').unwrap_or(endpoint_path);
63        let uri_path_no_slash = uri_path_and_query
64            .strip_prefix('/')
65            .unwrap_or(uri_path_and_query);
66        Cow::Owned(format!("{}/{}", ep_no_slash, uri_path_no_slash))
67    }
68}