Skip to main content

cloud_sdk/operation/
local_async.rs

1//! Local asynchronous prepared-request execution.
2
3use super::{CheckedResponseGuard, PreparedExecutionError, PreparedRequest};
4use crate::authentication::{LocalAsyncAuthenticatedTransport, drive_local_authenticated};
5use crate::transport::{AsyncExecutionError, BoundTransport, ResponseBuffer};
6
7impl<'request> PreparedRequest<'request> {
8    /// Verifies endpoint identity, executes once on a local async transport,
9    /// and validates the response.
10    ///
11    /// This method owns no executor and does not require the returned future
12    /// to be `Send`. Dropping it clears the response buffer while request
13    /// delivery remains conservatively possibly sent.
14    pub async fn execute_local_async<'transport, 'buffer, T>(
15        &'transport self,
16        transport: &'transport T,
17        response_storage: &'buffer mut [u8],
18        response_header_storage: &'buffer mut [u8],
19    ) -> Result<CheckedResponseGuard<'buffer>, PreparedExecutionError<T::Error>>
20    where
21        T: LocalAsyncAuthenticatedTransport + BoundTransport,
22        'request: 'transport,
23    {
24        let mut response = ResponseBuffer::new(
25            response_storage,
26            self.raw_response_policy().max_body_bytes(),
27            response_header_storage,
28        );
29        let actual = transport
30            .endpoint_identity()
31            .map_err(PreparedExecutionError::EndpointIdentity)?;
32        self.service()
33            .endpoint_policy()
34            .verify(actual)
35            .map_err(|_| PreparedExecutionError::EndpointMismatch)?;
36        drive_local_authenticated(transport, self.authenticated_request(), response.writer())
37            .await
38            .map_err(map_local_error)?;
39        self.validate_response(response)
40            .map_err(PreparedExecutionError::ResponsePolicy)
41    }
42}
43
44fn map_local_error<E>(error: AsyncExecutionError<E>) -> PreparedExecutionError<E> {
45    match error {
46        AsyncExecutionError::Transport(error) => PreparedExecutionError::Transport(error),
47        AsyncExecutionError::Response(error) => PreparedExecutionError::ResponseWriter(error),
48    }
49}