diff --git a/axum/src/docs/extract.md b/axum/src/docs/extract.md index 488032d2..e002a8b7 100644 --- a/axum/src/docs/extract.md +++ b/axum/src/docs/extract.md @@ -595,17 +595,19 @@ where type Rejection = Response; async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + // You can either call them directly... let TypedHeader(Authorization(token)) = TypedHeader::>::from_request_parts(parts, state) .await .map_err(|err| err.into_response())?; - let Extension(state): Extension = Extension::from_request_parts(parts, state) + // ... or use `extract` / `extract_with_state` from `RequestExt` / `RequestPartsExt` + use axum::RequestPartsExt; + let Extension(state) = parts.extract::>() .await .map_err(|err| err.into_response())?; - // actually perform the authorization... - unimplemented!() + unimplemented!("actually perform the authorization") } } @@ -710,12 +712,12 @@ Extractors can also be run from middleware: ```rust use axum::{ - Router, middleware::{self, Next}, extract::{TypedHeader, FromRequestParts}, http::{Request, StatusCode}, response::Response, headers::authorization::{Authorization, Bearer}, + RequestPartsExt, Router, }; async fn auth_middleware( @@ -729,7 +731,7 @@ where let (mut parts, body) = request.into_parts(); // `TypedHeader>` extracts the auth token - let auth = TypedHeader::>::from_request_parts(&mut parts, &()) + let auth: TypedHeader> = parts.extract() .await .map_err(|_| StatusCode::UNAUTHORIZED)?; diff --git a/examples/customize-extractor-error/src/custom_extractor.rs b/examples/customize-extractor-error/src/custom_extractor.rs index 10aa0f00..d9093bc4 100644 --- a/examples/customize-extractor-error/src/custom_extractor.rs +++ b/examples/customize-extractor-error/src/custom_extractor.rs @@ -6,10 +6,11 @@ //! - Complexity: Manually implementing `FromRequest` results on more complex code use axum::{ async_trait, - extract::{rejection::JsonRejection, FromRequest, FromRequestParts, MatchedPath}, + extract::{rejection::JsonRejection, FromRequest, MatchedPath}, http::Request, http::StatusCode, response::IntoResponse, + RequestPartsExt, }; use serde_json::{json, Value}; @@ -32,14 +33,13 @@ where async fn from_request(req: Request, state: &S) -> Result { let (mut parts, body) = req.into_parts(); - // We can use other extractors to provide better rejection - // messages. For example, here we are using - // `axum::extract::MatchedPath` to provide a better error - // message + // We can use other extractors to provide better rejection messages. + // For example, here we are using `axum::extract::MatchedPath` to + // provide a better error message. // - // Have to run that first since `Json::from_request` consumes - // the request - let path = MatchedPath::from_request_parts(&mut parts, state) + // Have to run that first since `Json` extraction consumes the request. + let path = parts + .extract::() .await .map(|path| path.as_str().to_owned()) .ok(); diff --git a/examples/handle-head-request/src/main.rs b/examples/handle-head-request/src/main.rs index aece6c25..9624835c 100644 --- a/examples/handle-head-request/src/main.rs +++ b/examples/handle-head-request/src/main.rs @@ -5,7 +5,7 @@ //! ``` use axum::response::{IntoResponse, Response}; -use axum::{http, routing::get, Router, RouterService}; +use axum::{http, routing::get, Router}; use std::net::SocketAddr; fn app() -> Router { diff --git a/examples/jwt/src/main.rs b/examples/jwt/src/main.rs index 3cef04c7..82633d96 100644 --- a/examples/jwt/src/main.rs +++ b/examples/jwt/src/main.rs @@ -13,7 +13,7 @@ use axum::{ http::{request::Parts, StatusCode}, response::{IntoResponse, Response}, routing::{get, post}, - Json, Router, + Json, RequestPartsExt, Router, }; use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, Validation}; use once_cell::sync::Lazy; @@ -128,12 +128,12 @@ where { type Rejection = AuthError; - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { + async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { // Extract the token from the authorization header - let TypedHeader(Authorization(bearer)) = - TypedHeader::>::from_request_parts(parts, state) - .await - .map_err(|_| AuthError::InvalidToken)?; + let TypedHeader(Authorization(bearer)) = parts + .extract::>>() + .await + .map_err(|_| AuthError::InvalidToken)?; // Decode the user data let token_data = decode::(bearer.token(), &KEYS.decoding, &Validation::default()) .map_err(|_| AuthError::InvalidToken)?; diff --git a/examples/oauth/src/main.rs b/examples/oauth/src/main.rs index 079c65eb..67327184 100644 --- a/examples/oauth/src/main.rs +++ b/examples/oauth/src/main.rs @@ -17,7 +17,7 @@ use axum::{ http::{header::SET_COOKIE, HeaderMap}, response::{IntoResponse, Redirect, Response}, routing::get, - Router, + RequestPartsExt, Router, }; use http::{header, request::Parts}; use oauth2::{ @@ -234,7 +234,8 @@ where async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { let store = MemoryStore::from_ref(state); - let cookies = TypedHeader::::from_request_parts(parts, state) + let cookies = parts + .extract::>() .await .map_err(|e| match *e.name() { header::COOKIE => match e.reason() { diff --git a/examples/sessions/src/main.rs b/examples/sessions/src/main.rs index 05d676fa..60ebfd21 100644 --- a/examples/sessions/src/main.rs +++ b/examples/sessions/src/main.rs @@ -17,7 +17,7 @@ use axum::{ }, response::IntoResponse, routing::get, - Router, + RequestPartsExt, Router, }; use serde::{Deserialize, Serialize}; use std::fmt::Debug; @@ -91,9 +91,7 @@ where async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { let store = MemoryStore::from_ref(state); - let cookie = Option::>::from_request_parts(parts, state) - .await - .unwrap(); + let cookie: Option> = parts.extract().await.unwrap(); let session_cookie = cookie .as_ref() diff --git a/examples/versioning/src/main.rs b/examples/versioning/src/main.rs index 2f67e335..6948eb1d 100644 --- a/examples/versioning/src/main.rs +++ b/examples/versioning/src/main.rs @@ -10,7 +10,7 @@ use axum::{ http::{request::Parts, StatusCode}, response::{IntoResponse, Response}, routing::get, - Router, + RequestPartsExt, Router, }; use std::{collections::HashMap, net::SocketAddr}; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; @@ -54,10 +54,9 @@ where { type Rejection = Response; - async fn from_request_parts(parts: &mut Parts, state: &S) -> Result { - let params = Path::>::from_request_parts(parts, state) - .await - .map_err(IntoResponse::into_response)?; + async fn from_request_parts(parts: &mut Parts, _state: &S) -> Result { + let params: Path> = + parts.extract().await.map_err(IntoResponse::into_response)?; let version = params .get("version")