Trait axum::RequestPartsExt
source · pub trait RequestPartsExt: Sealed + Sized {
// Required methods
fn extract<E>(
&mut self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<()>>::Rejection>> + Send + '_, Global>>
where E: FromRequestParts<()> + 'static;
fn extract_with_state<E, S, 'a>(
&'a mut self,
state: &'a S
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<S>>::Rejection>> + Send + 'a, Global>>
where E: FromRequestParts<S> + 'static,
S: Send + Sync;
}
Expand description
Extension trait that adds additional methods to Parts
.
Required Methods§
sourcefn extract<E>(
&mut self
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<()>>::Rejection>> + Send + '_, Global>>where
E: FromRequestParts<()> + 'static,
fn extract<E>( &mut self ) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<()>>::Rejection>> + Send + '_, Global>>where E: FromRequestParts<()> + 'static,
Apply an extractor to this Parts
.
This is just a convenience for E::from_request_parts(parts, &())
.
Example
use axum::{
extract::{Query, TypedHeader, FromRequestParts},
response::{Response, IntoResponse},
headers::UserAgent,
http::request::Parts,
RequestPartsExt,
async_trait,
};
use std::collections::HashMap;
struct MyExtractor {
user_agent: String,
query_params: HashMap<String, String>,
}
#[async_trait]
impl<S> FromRequestParts<S> for MyExtractor
where
S: Send + Sync,
{
type Rejection = Response;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let user_agent = parts
.extract::<TypedHeader<UserAgent>>()
.await
.map(|user_agent| user_agent.as_str().to_owned())
.map_err(|err| err.into_response())?;
let query_params = parts
.extract::<Query<HashMap<String, String>>>()
.await
.map(|Query(params)| params)
.map_err(|err| err.into_response())?;
Ok(MyExtractor { user_agent, query_params })
}
}
sourcefn extract_with_state<E, S, 'a>(
&'a mut self,
state: &'a S
) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<S>>::Rejection>> + Send + 'a, Global>>where
E: FromRequestParts<S> + 'static,
S: Send + Sync,
fn extract_with_state<E, S, 'a>( &'a mut self, state: &'a S ) -> Pin<Box<dyn Future<Output = Result<E, <E as FromRequestParts<S>>::Rejection>> + Send + 'a, Global>>where E: FromRequestParts<S> + 'static, S: Send + Sync,
Apply an extractor that requires some state to this Parts
.
This is just a convenience for E::from_request_parts(parts, state)
.
Example
use axum::{
extract::{FromRef, FromRequestParts},
response::{Response, IntoResponse},
http::request::Parts,
RequestPartsExt,
async_trait,
};
struct MyExtractor {
requires_state: RequiresState,
}
#[async_trait]
impl<S> FromRequestParts<S> for MyExtractor
where
String: FromRef<S>,
S: Send + Sync,
{
type Rejection = std::convert::Infallible;
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
let requires_state = parts
.extract_with_state::<RequiresState, _>(state)
.await?;
Ok(MyExtractor { requires_state })
}
}
struct RequiresState { /* ... */ }
// some extractor that requires a `String` in the state
#[async_trait]
impl<S> FromRequestParts<S> for RequiresState
where
String: FromRef<S>,
S: Send + Sync,
{
// ...
}