1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-27 17:52:56 +01:00

Add ServiceRequest::extract (#2647)

Co-authored-by: Rob Ede <robjtede@icloud.com>
This commit is contained in:
Luca Palmieri 2022-04-02 19:46:26 +01:00 committed by GitHub
parent 2fed978597
commit de9e41484a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 2 deletions

View File

@ -1,6 +1,9 @@
# Changelog # Changelog
## Unreleased - 2021-xx-xx ## Unreleased - 2021-xx-xx
- Add `ServiceRequest::extract` to make it easier to use extractors when writing middlewares. [#2647]
[#2647]: https://github.com/actix/actix-web/pull/2647
## 4.0.1 - 2022-02-25 ## 4.0.1 - 2022-02-25

View File

@ -18,9 +18,11 @@ use crate::{dev::Payload, Error, HttpRequest};
/// A type that implements [`FromRequest`] is called an **extractor** and can extract data from /// A type that implements [`FromRequest`] is called an **extractor** and can extract data from
/// the request. Some types that implement this trait are: [`Json`], [`Header`], and [`Path`]. /// the request. Some types that implement this trait are: [`Json`], [`Header`], and [`Path`].
/// ///
/// Check out [`ServiceRequest::extract`](crate::dev::ServiceRequest::extract) if you want to
/// leverage extractors when implementing middlewares.
///
/// # Configuration /// # Configuration
/// An extractor can be customized by injecting the corresponding configuration with one of: /// An extractor can be customized by injecting the corresponding configuration with one of:
///
/// - [`App::app_data()`][crate::App::app_data] /// - [`App::app_data()`][crate::App::app_data]
/// - [`Scope::app_data()`][crate::Scope::app_data] /// - [`Scope::app_data()`][crate::Scope::app_data]
/// - [`Resource::app_data()`][crate::Resource::app_data] /// - [`Resource::app_data()`][crate::Resource::app_data]

View File

@ -24,7 +24,7 @@ use crate::{
guard::{Guard, GuardContext}, guard::{Guard, GuardContext},
info::ConnectionInfo, info::ConnectionInfo,
rmap::ResourceMap, rmap::ResourceMap,
Error, HttpRequest, HttpResponse, Error, FromRequest, HttpRequest, HttpResponse,
}; };
pub(crate) type BoxedHttpService = BoxService<ServiceRequest, ServiceResponse<BoxBody>, Error>; pub(crate) type BoxedHttpService = BoxService<ServiceRequest, ServiceResponse<BoxBody>, Error>;
@ -95,6 +95,31 @@ impl ServiceRequest {
(&mut self.req, &mut self.payload) (&mut self.req, &mut self.payload)
} }
/// Derives a type from this request using an [extractor](crate::FromRequest).
///
/// Returns the `T` extractor's `Future` type which can be `await`ed. This is particularly handy
/// when you want to use an extractor in a middleware implementation.
///
/// # Examples
/// ```
/// use actix_web::{
/// dev::{ServiceRequest, ServiceResponse},
/// web::Path, Error
/// };
///
/// async fn my_helper(mut srv_req: ServiceRequest) -> Result<ServiceResponse, Error> {
/// let path = srv_req.extract::<Path<(String, u32)>>().await?;
/// // [...]
/// # todo!()
/// }
/// ```
pub fn extract<T>(&mut self) -> <T as FromRequest>::Future
where
T: FromRequest,
{
T::from_request(&self.req, &mut self.payload)
}
/// Construct request from parts. /// Construct request from parts.
pub fn from_parts(req: HttpRequest, payload: Payload) -> Self { pub fn from_parts(req: HttpRequest, payload: Payload) -> Self {
#[cfg(debug_assertions)] #[cfg(debug_assertions)]