aws_smithy_runtime/client/test_util/
deserializer.rs

1/*
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6use aws_smithy_runtime_api::client::interceptors::context::{Error, Output};
7use aws_smithy_runtime_api::client::orchestrator::{HttpResponse, OrchestratorError};
8use aws_smithy_runtime_api::client::runtime_plugin::RuntimePlugin;
9use aws_smithy_runtime_api::client::ser_de::{DeserializeResponse, SharedResponseDeserializer};
10use aws_smithy_types::config_bag::{FrozenLayer, Layer};
11use std::sync::Mutex;
12
13/// Test response deserializer that always returns the same canned response.
14#[derive(Default, Debug)]
15pub struct CannedResponseDeserializer {
16    inner: Mutex<Option<Result<Output, OrchestratorError<Error>>>>,
17}
18
19impl CannedResponseDeserializer {
20    /// Creates a new `CannedResponseDeserializer` with the given canned response.
21    pub fn new(output: Result<Output, OrchestratorError<Error>>) -> Self {
22        Self {
23            inner: Mutex::new(Some(output)),
24        }
25    }
26
27    fn take(&self) -> Option<Result<Output, OrchestratorError<Error>>> {
28        match self.inner.lock() {
29            Ok(mut guard) => guard.take(),
30            Err(_) => None,
31        }
32    }
33}
34
35impl DeserializeResponse for CannedResponseDeserializer {
36    fn deserialize_nonstreaming(
37        &self,
38        _response: &HttpResponse,
39    ) -> Result<Output, OrchestratorError<Error>> {
40        self.take()
41            .ok_or("CannedResponseDeserializer's inner value has already been taken.")
42            .unwrap()
43    }
44}
45
46impl RuntimePlugin for CannedResponseDeserializer {
47    fn config(&self) -> Option<FrozenLayer> {
48        let mut cfg = Layer::new("CannedResponse");
49        cfg.store_put(SharedResponseDeserializer::new(Self {
50            inner: Mutex::new(self.take()),
51        }));
52        Some(cfg.freeze())
53    }
54}