rama_http_backend/client/proxy/layer/proxy_connector/layer.rs
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
use super::HttpProxyConnector;
use rama_core::Layer;
#[derive(Debug, Clone, Default)]
/// A [`Layer`] which wraps the given service with a [`HttpProxyConnector`].
///
/// See [`HttpProxyConnector`] for more information.
pub struct HttpProxyConnectorLayer {
required: bool,
}
impl HttpProxyConnectorLayer {
/// Create a new [`HttpProxyConnectorLayer`] which creates a [`HttpProxyConnector`]
/// which will only connect via an http proxy in case the [`ProxyAddress`] is available
/// in the [`Context`].
///
/// [`Context`]: rama_core::Context
/// [`ProxyAddress`]: rama_net::address::ProxyAddress
pub fn optional() -> Self {
Self { required: false }
}
/// Create a new [`HttpProxyConnectorLayer`] which creates a [`HttpProxyConnector`]
/// which will always connect via an http proxy, but fail in case the [`ProxyAddress`] is
/// not available in the [`Context`].
///
/// [`Context`]: rama_core::Context
/// [`ProxyAddress`]: rama_net::address::ProxyAddress
pub fn required() -> Self {
Self { required: true }
}
}
impl<S> Layer<S> for HttpProxyConnectorLayer {
type Service = HttpProxyConnector<S>;
fn layer(&self, inner: S) -> Self::Service {
HttpProxyConnector::new(inner, self.required)
}
}