1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

use aws_smithy_runtime_api::client::interceptors::context::{Error, Output};
use aws_smithy_runtime_api::client::orchestrator::{HttpResponse, OrchestratorError};
use aws_smithy_runtime_api::client::runtime_plugin::RuntimePlugin;
use aws_smithy_runtime_api::client::ser_de::{DeserializeResponse, SharedResponseDeserializer};
use aws_smithy_types::config_bag::{FrozenLayer, Layer};
use std::sync::Mutex;

/// Test response deserializer that always returns the same canned response.
#[derive(Default, Debug)]
pub struct CannedResponseDeserializer {
    inner: Mutex<Option<Result<Output, OrchestratorError<Error>>>>,
}

impl CannedResponseDeserializer {
    /// Creates a new `CannedResponseDeserializer` with the given canned response.
    pub fn new(output: Result<Output, OrchestratorError<Error>>) -> Self {
        Self {
            inner: Mutex::new(Some(output)),
        }
    }

    fn take(&self) -> Option<Result<Output, OrchestratorError<Error>>> {
        match self.inner.lock() {
            Ok(mut guard) => guard.take(),
            Err(_) => None,
        }
    }
}

impl DeserializeResponse for CannedResponseDeserializer {
    fn deserialize_nonstreaming(
        &self,
        _response: &HttpResponse,
    ) -> Result<Output, OrchestratorError<Error>> {
        self.take()
            .ok_or("CannedResponseDeserializer's inner value has already been taken.")
            .unwrap()
    }
}

impl RuntimePlugin for CannedResponseDeserializer {
    fn config(&self) -> Option<FrozenLayer> {
        let mut cfg = Layer::new("CannedResponse");
        cfg.store_put(SharedResponseDeserializer::new(Self {
            inner: Mutex::new(self.take()),
        }));
        Some(cfg.freeze())
    }
}