1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
56//! Serialization/deserialization for the orchestrator.
78use crate::box_error::BoxError;
9use crate::client::interceptors::context::{Error, Input, Output};
10use crate::client::orchestrator::{HttpRequest, HttpResponse, OrchestratorError};
11use crate::impl_shared_conversions;
12use aws_smithy_types::config_bag::{ConfigBag, Storable, StoreReplace};
13use std::fmt;
14use std::sync::Arc;
1516/// Serialization implementation that converts an [`Input`] into an [`HttpRequest`].
17pub trait SerializeRequest: Send + Sync + fmt::Debug {
18/// Serializes the input into an HTTP request.
19 ///
20 /// The type of the [`Input`] must be known ahead of time by the request serializer
21 /// implementation, and must be downcasted to get access to the information necessary
22 /// for serialization.
23 ///
24 /// The request serializer is generally added to the [`ConfigBag`] by the operation's
25 /// code generated runtime plugin, which is aware of the correct input/output/error types.
26fn serialize_input(&self, input: Input, cfg: &mut ConfigBag) -> Result<HttpRequest, BoxError>;
27}
2829/// A shared request serializer.
30///
31/// This is a simple shared ownership wrapper type for the [`SerializeRequest`] trait.
32#[derive(Clone, Debug)]
33pub struct SharedRequestSerializer(Arc<dyn SerializeRequest>);
3435impl SharedRequestSerializer {
36/// Creates a new shared request serializer.
37pub fn new(serializer: impl SerializeRequest + 'static) -> Self {
38Self(Arc::new(serializer))
39 }
40}
4142impl SerializeRequest for SharedRequestSerializer {
43fn serialize_input(&self, input: Input, cfg: &mut ConfigBag) -> Result<HttpRequest, BoxError> {
44self.0.serialize_input(input, cfg)
45 }
46}
4748impl Storable for SharedRequestSerializer {
49type Storer = StoreReplace<Self>;
50}
5152impl_shared_conversions!(convert SharedRequestSerializer from SerializeRequest using SharedRequestSerializer::new);
5354/// Deserialization implementation that converts an [`HttpResponse`] into an [`Output`] or [`Error`].
55pub trait DeserializeResponse: Send + Sync + fmt::Debug {
56/// For streaming requests, deserializes the response headers.
57 ///
58 /// The orchestrator will call `deserialize_streaming` first, and if it returns `None`,
59 /// then it will continue onto `deserialize_nonstreaming`. This method should only be
60 /// implemented for streaming requests where the streaming response body needs to be a part
61 /// of the deserialized output.
62fn deserialize_streaming(
63&self,
64 response: &mut HttpResponse,
65 ) -> Option<Result<Output, OrchestratorError<Error>>> {
66let _ = response;
67None
68}
6970/// Deserialize the entire response including its body into an output or error.
71fn deserialize_nonstreaming(
72&self,
73 response: &HttpResponse,
74 ) -> Result<Output, OrchestratorError<Error>>;
75}
7677/// Shared response deserializer.
78///
79/// This is a simple shared ownership wrapper type for the [`DeserializeResponse`] trait.
80#[derive(Debug)]
81pub struct SharedResponseDeserializer(Arc<dyn DeserializeResponse>);
8283impl SharedResponseDeserializer {
84/// Creates a new [`SharedResponseDeserializer`].
85pub fn new(serializer: impl DeserializeResponse + 'static) -> Self {
86Self(Arc::new(serializer))
87 }
88}
8990impl DeserializeResponse for SharedResponseDeserializer {
91fn deserialize_nonstreaming(
92&self,
93 response: &HttpResponse,
94 ) -> Result<Output, OrchestratorError<Error>> {
95self.0.deserialize_nonstreaming(response)
96 }
9798fn deserialize_streaming(
99&self,
100 response: &mut HttpResponse,
101 ) -> Option<Result<Output, OrchestratorError<Error>>> {
102self.0.deserialize_streaming(response)
103 }
104}
105106impl Storable for SharedResponseDeserializer {
107type Storer = StoreReplace<Self>;
108}
109110impl_shared_conversions!(convert SharedResponseDeserializer from DeserializeResponse using SharedResponseDeserializer::new);