pub trait MapRequest {
    type Error: Into<Box<dyn Error + Send + Sync>>;

    // Required methods
    fn name(&self) -> &'static str;
    fn apply(&self, request: Request) -> Result<Request, Self::Error>;
}
Expand description

MapRequest defines a synchronous middleware that transforms an operation::Request.

Typically, these middleware will read configuration from the PropertyBag and use it to augment the request. Most fundamental middleware is expressed as MapRequest, including signing & endpoint resolution.

Examples

use http::header::{HeaderName, HeaderValue};

/// Signaling struct added to the request property bag if a header should be added
struct NeedsHeader;

struct AddHeader(HeaderName, HeaderValue);

impl MapRequest for AddHeader {
    type Error = Infallible;

    fn name(&self) -> &'static str {
        "add_header"
    }

    fn apply(&self, request: operation::Request) -> Result<operation::Request, Self::Error> {
        request.augment(|mut request, properties| {
            if properties.get::<NeedsHeader>().is_some() {
                request.headers_mut().append(self.0.clone(), self.1.clone());
            }
            Ok(request)
        })
    }
}

Required Associated Types§

source

type Error: Into<Box<dyn Error + Send + Sync>>

The Error type returned by this operation.

If this middleware never fails use std::convert::Infallible or similar.

Required Methods§

source

fn name(&self) -> &'static str

Returns the name of this map request operation for inclusion in a tracing span.

source

fn apply(&self, request: Request) -> Result<Request, Self::Error>

Apply this middleware to a request.

Typically, implementations will use request.augment to be able to transform an owned http::Request.

Implementors§