Skip to main content

cloud_sdk_reqwest/asynchronous/
basic_config.rs

1use core::fmt;
2
3use crate::shared::{BasicCredential, BuildError, HttpsEndpoint, RequestTimeouts, UserAgent};
4
5use super::AsyncBasicClient;
6use super::config::configured_raw_client;
7
8/// Builder requiring a scoped Basic credential and complete transport limits.
9pub struct AsyncBasicClientBuilder {
10    endpoint: HttpsEndpoint,
11    credential: BasicCredential,
12    user_agent: UserAgent,
13    timeouts: RequestTimeouts,
14}
15
16impl AsyncBasicClientBuilder {
17    /// Creates a complete asynchronous Basic-client configuration.
18    #[must_use]
19    pub const fn new(
20        endpoint: HttpsEndpoint,
21        credential: BasicCredential,
22        user_agent: UserAgent,
23        timeouts: RequestTimeouts,
24    ) -> Self {
25        Self {
26            endpoint,
27            credential,
28            user_agent,
29            timeouts,
30        }
31    }
32
33    /// Builds a hardened HTTPS-only Basic-auth client.
34    pub fn build(self) -> Result<AsyncBasicClient, BuildError> {
35        self.build_inner(true)
36    }
37
38    fn build_inner(self, https_only: bool) -> Result<AsyncBasicClient, BuildError> {
39        if !self.credential.scope().matches_endpoint(&self.endpoint) {
40            return Err(BuildError::CredentialEndpointMismatch);
41        }
42        let client = configured_raw_client(
43            self.endpoint.clone(),
44            &self.user_agent,
45            self.timeouts,
46            https_only,
47        )?;
48        Ok(AsyncBasicClient::new(
49            client,
50            self.endpoint,
51            self.credential,
52            !https_only,
53        ))
54    }
55
56    #[cfg(test)]
57    pub(super) fn build_for_loopback(self) -> Result<AsyncBasicClient, BuildError> {
58        self.build_inner(false)
59    }
60}
61
62impl fmt::Debug for AsyncBasicClientBuilder {
63    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
64        formatter
65            .debug_struct("AsyncBasicClientBuilder")
66            .field("endpoint", &"[redacted]")
67            .field("credential", &"[redacted]")
68            .field("user_agent", &self.user_agent)
69            .field("timeouts", &self.timeouts)
70            .finish()
71    }
72}