pdk-classy 1.9.1-alpha.2

PDK Classy
Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

/// An HTTP Response to be returned by Request filters.
#[derive(Clone, PartialEq, PartialOrd, Eq, Ord, Debug, Hash, Default)]
pub struct Response {
    status_code: u32,
    headers: Vec<(String, String)>,
    body: Vec<u8>,
}

impl Response {
    /// Creates a new response.
    pub fn new(status_code: u32) -> Self {
        Self {
            status_code,
            headers: Vec::new(),
            body: Vec::new(),
        }
    }

    /// Consumes the current [`Response`] and returns a new one with a list of `headers` setted.
    pub fn with_headers(mut self, headers: impl Into<Vec<(String, String)>>) -> Self {
        self.headers = headers.into();
        self
    }

    /// Consumes the current [`Response`] and returns a new one with a `body` setted.
    pub fn with_body(mut self, body: impl Into<Vec<u8>>) -> Self {
        self.body = body.into();
        self
    }

    /// Returns the status code.
    pub fn status_code(&self) -> u32 {
        self.status_code
    }

    /// Returns the headers as a list of pairs of name and value.
    pub fn headers(&self) -> Vec<(&str, &str)> {
        self.headers
            .iter()
            .map(|(k, v)| (k.as_str(), v.as_str()))
            .collect()
    }

    /// Returns the body if exists in binary format.
    pub fn body(&self) -> Option<&[u8]> {
        (!self.body.as_slice().is_empty()).then_some(self.body.as_slice())
    }
}