Struct saphir::request::Request

source ·
pub struct Request<T = Body<Bytes>> { /* private fields */ }
Expand description

Struct that wraps a hyper request + some magic

Implementations§

source§

impl Request<Body<Bytes>>

source

pub async fn json<T>(&mut self) -> Result<T, SaphirError>where T: for<'a> Deserialize<'a> + Unpin + 'static,

Available on crate feature json only.
source§

impl Request<Body<Bytes>>

source

pub async fn form<T>(&mut self) -> Result<T, SaphirError>where T: for<'a> Deserialize<'a> + Unpin + 'static,

Available on crate feature form only.
source§

impl<T> Request<T>

source

pub fn peer_addr(&self) -> Option<&SocketAddr>

Return the Peer SocketAddr if one was available when receiving the request

source

pub fn operation_id(&self) -> &OperationId

Available on crate feature operation only.

Return the OperationId of the request

source

pub fn operation_id_mut(&mut self) -> &mut OperationId

Available on crate feature operation only.

Return the mutable OperationId of the request

source

pub fn peer_addr_mut(&mut self) -> Option<&mut SocketAddr>

source

pub fn cookies(&self) -> &CookieJar

Get the cookies sent by the browsers.

Before accessing cookies, you will need to parse them, it is done with the parse_cookies method

// Parse cookies
req.parse_cookies();
// then use cookies
let cookie = req.cookies().get("MyCookie");
source

pub fn cookies_mut(&mut self) -> &mut CookieJar

Get the cookies sent by the browsers in a mutable way

Before accessing cookies, you will need to parse them, it is done with the parse_cookies method

// Parse cookies
req.parse_cookies();
// then use cookies
let mut_cookie = req.cookies_mut().get("MyCookie");
source

pub fn captures(&self) -> &HashMap<String, String>

Access the captured variables from the request path. E.g. a path composed as /user/{user_id}/profile will store a capture named "user_id".

let user_id = req.captures().get("user_id");
// retrieve user by id
source

pub fn captures_mut(&mut self) -> &mut HashMap<String, String>

Access the captured variables from the request path, in a mutable way.

source

pub fn map<F, U>(self, f: F) -> Request<U>where F: FnOnce(T) -> U,

Convert a request of T in a request of U

// req is Request<Body>
let req: Request<String> = req.map(|_ignored_body| "New body".to_string());
source

pub async fn async_map<F, Fut, U>(self, f: F) -> Request<U>where F: FnOnce(T) -> Fut, Fut: Future<Output = U>,

Convert a request of T in a request of U through a future

// req is Request<Body>
let req = req.async_map(|b| async {hyper::body::to_bytes(b).await});
source

pub fn into_body(self) -> T

Return body, dropping the request

// req is Request<Body<Bytes>>
let body = req.into_body();
source

pub fn parse_cookies(&mut self)

Parse cookies from the Cookie header

source§

impl<T: FromBytes + Unpin + 'static> Request<Body<T>>

source

pub async fn load_body(self) -> Result<Request<T::Out>, SaphirError>

Convert a request of T in a request of U through a future

// req is Request<Body<Bytes>>
let req = req.load_body().await.unwrap();
// req is now Request<Bytes>
source§

impl<T, E> Request<Result<T, E>>

source

pub fn transpose(self) -> Result<Request<T>, E>

Convert a request of Result<T, E> in a Result<Request, E>

// req is Request<Result<String, String>>
let res = req.transpose();
assert!(res.is_ok());
source§

impl<T> Request<Option<T>>

source

pub fn transpose(self) -> Option<Request<T>>

Convert a request of Option in a Option<Request, E>

// req is Request<Option<String>>
let opt = req.transpose();
assert!(opt.is_some());

Methods from Deref<Target = RawRequest<T>>§

source

pub fn method(&self) -> &Method

Returns a reference to the associated HTTP method.

Examples
let request: Request<()> = Request::default();
assert_eq!(*request.method(), Method::GET);
source

pub fn method_mut(&mut self) -> &mut Method

Returns a mutable reference to the associated HTTP method.

Examples
let mut request: Request<()> = Request::default();
*request.method_mut() = Method::PUT;
assert_eq!(*request.method(), Method::PUT);
source

pub fn uri(&self) -> &Uri

Returns a reference to the associated URI.

Examples
let request: Request<()> = Request::default();
assert_eq!(*request.uri(), *"/");
source

pub fn uri_mut(&mut self) -> &mut Uri

Returns a mutable reference to the associated URI.

Examples
let mut request: Request<()> = Request::default();
*request.uri_mut() = "/hello".parse().unwrap();
assert_eq!(*request.uri(), *"/hello");
source

pub fn version(&self) -> Version

Returns the associated version.

Examples
let request: Request<()> = Request::default();
assert_eq!(request.version(), Version::HTTP_11);
source

pub fn version_mut(&mut self) -> &mut Version

Returns a mutable reference to the associated version.

Examples
let mut request: Request<()> = Request::default();
*request.version_mut() = Version::HTTP_2;
assert_eq!(request.version(), Version::HTTP_2);
source

pub fn headers(&self) -> &HeaderMap<HeaderValue>

Returns a reference to the associated header field map.

Examples
let request: Request<()> = Request::default();
assert!(request.headers().is_empty());
source

pub fn headers_mut(&mut self) -> &mut HeaderMap<HeaderValue>

Returns a mutable reference to the associated header field map.

Examples
let mut request: Request<()> = Request::default();
request.headers_mut().insert(HOST, HeaderValue::from_static("world"));
assert!(!request.headers().is_empty());
source

pub fn extensions(&self) -> &Extensions

Returns a reference to the associated extensions.

Examples
let request: Request<()> = Request::default();
assert!(request.extensions().get::<i32>().is_none());
source

pub fn extensions_mut(&mut self) -> &mut Extensions

Returns a mutable reference to the associated extensions.

Examples
let mut request: Request<()> = Request::default();
request.extensions_mut().insert("hello");
assert_eq!(request.extensions().get(), Some(&"hello"));
source

pub fn body(&self) -> &T

Returns a reference to the associated HTTP body.

Examples
let request: Request<String> = Request::default();
assert!(request.body().is_empty());
source

pub fn body_mut(&mut self) -> &mut T

Returns a mutable reference to the associated HTTP body.

Examples
let mut request: Request<String> = Request::default();
request.body_mut().push_str("hello world");
assert!(!request.body().is_empty());

Trait Implementations§

source§

impl<T> Deref for Request<T>

§

type Target = Request<T>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl<T> DerefMut for Request<T>

source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
source§

impl<T> From<Request<T>> for RawRequest<T>

source§

fn from(request: Request<T>) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<T = Body<Bytes>> !RefUnwindSafe for Request<T>

§

impl<T> Send for Request<T>where T: Send,

§

impl<T> Sync for Request<T>where T: Sync,

§

impl<T> Unpin for Request<T>where T: Unpin,

§

impl<T = Body<Bytes>> !UnwindSafe for Request<T>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more