pdk_classy/hl/
early_response.rs

1// Copyright (c) 2025, Salesforce, Inc.,
2// All rights reserved.
3// For full license text, see the LICENSE.txt file
4
5/// An HTTP Response to be returned by Request filters.
6#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, Default)]
7pub struct Response {
8    status_code: u32,
9    headers: Vec<(String, String)>,
10    body: Vec<u8>,
11}
12
13impl Response {
14    /// Creates a new response.
15    pub fn new(status_code: u32) -> Self {
16        Self {
17            status_code,
18            headers: Vec::new(),
19            body: Vec::new(),
20        }
21    }
22
23    /// Consumes the current [`Response`] and returns a new one with a list of `headers` setted.
24    pub fn with_headers(mut self, headers: impl Into<Vec<(String, String)>>) -> Self {
25        self.headers = headers.into();
26        self
27    }
28
29    /// Consumes the current [`Response`] and returns a new one with a `body` setted.
30    pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
31        self.body = body.into();
32        self
33    }
34
35    /// Returns the status code.
36    pub fn status_code(&self) -> u32 {
37        self.status_code
38    }
39
40    /// Returns the headers as a list of pairs of name and value.
41    pub fn headers(&self) -> Vec<(&str, &str)> {
42        self.headers
43            .iter()
44            .map(|(k, v)| (k.as_str(), v.as_str()))
45            .collect()
46    }
47
48    /// Returns the body if exists in binary format.
49    pub fn body(&self) -> Option<&[u8]> {
50        (!self.body.as_slice().is_empty()).then_some(self.body.as_slice())
51    }
52}