Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Implement path extractor. #124

Merged
merged 1 commit into from
Aug 6, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use `pin-project-lite` instead of `pin-project`. ([#95](https://github.com/tokio-rs/axum/pull/95))
- Re-export `http` crate and `hyper::Server`. ([#110](https://github.com/tokio-rs/axum/pull/110))
- Fix `Query` and `Form` extractors giving bad request error when query string is empty. ([#117](https://github.com/tokio-rs/axum/pull/117))
- Add `Path` extractor. ([#124](https://github.com/tokio-rs/axum/pull/124))

## Breaking changes

Expand Down
4 changes: 2 additions & 2 deletions examples/error_handling_and_dependency_injection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use axum::{
async_trait,
extract::{Extension, Json, UrlParams},
extract::{Extension, Json, Path},
prelude::*,
response::IntoResponse,
AddExtensionLayer,
Expand Down Expand Up @@ -56,7 +56,7 @@ async fn main() {
/// are automatically converted into `AppError` which implements `IntoResponse`
/// so it can be returned from handlers directly.
async fn users_show(
UrlParams((user_id,)): UrlParams<(Uuid,)>,
Path(user_id): Path<Uuid>,
Extension(user_repo): Extension<DynUserRepo>,
) -> Result<response::Json<User>, AppError> {
let user = user_repo.find(user_id).await?;
Expand Down
11 changes: 4 additions & 7 deletions examples/key_value_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
use axum::{
async_trait,
extract::{extractor_middleware, ContentLengthLimit, Extension, RequestParts, UrlParams},
extract::{extractor_middleware, ContentLengthLimit, Extension, Path, RequestParts},
prelude::*,
response::IntoResponse,
routing::BoxRoute,
Expand Down Expand Up @@ -79,7 +79,7 @@ struct State {
}

async fn kv_get(
UrlParams((key,)): UrlParams<(String,)>,
Path(key): Path<String>,
Extension(state): Extension<SharedState>,
) -> Result<Bytes, StatusCode> {
let db = &state.read().unwrap().db;
Expand All @@ -92,7 +92,7 @@ async fn kv_get(
}

async fn kv_set(
UrlParams((key,)): UrlParams<(String,)>,
Path(key): Path<String>,
ContentLengthLimit(bytes): ContentLengthLimit<Bytes, { 1024 * 5_000 }>, // ~5mb
Extension(state): Extension<SharedState>,
) {
Expand All @@ -113,10 +113,7 @@ fn admin_routes() -> BoxRoute<hyper::Body> {
state.write().unwrap().db.clear();
}

async fn remove_key(
UrlParams((key,)): UrlParams<(String,)>,
Extension(state): Extension<SharedState>,
) {
async fn remove_key(Path(key): Path<String>, Extension(state): Extension<SharedState>) {
state.write().unwrap().db.remove(&key);
}

Expand Down
8 changes: 1 addition & 7 deletions examples/templates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ async fn main() {
.unwrap();
}

async fn greet(params: extract::UrlParamsMap) -> impl IntoResponse {
let name = params
.get("name")
.expect("`name` will be there if route was matched")
.to_string();

async fn greet(extract::Path(name): extract::Path<String>) -> impl IntoResponse {
let template = HelloTemplate { name };

HtmlTemplate(template)
}

Expand Down
9 changes: 3 additions & 6 deletions examples/todos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
//! ```
use axum::{
extract::{Extension, Json, Query, UrlParams},
extract::{Extension, Json, Path, Query},
prelude::*,
response::IntoResponse,
service::ServiceExt,
Expand Down Expand Up @@ -129,7 +129,7 @@ struct UpdateTodo {
}

async fn todos_update(
UrlParams((id,)): UrlParams<(Uuid,)>,
Path(id): Path<Uuid>,
Json(input): Json<UpdateTodo>,
Extension(db): Extension<Db>,
) -> Result<impl IntoResponse, StatusCode> {
Expand All @@ -153,10 +153,7 @@ async fn todos_update(
Ok(response::Json(todo))
}

async fn todos_delete(
UrlParams((id,)): UrlParams<(Uuid,)>,
Extension(db): Extension<Db>,
) -> impl IntoResponse {
async fn todos_delete(Path(id): Path<Uuid>, Extension(db): Extension<Db>) -> impl IntoResponse {
if db.write().unwrap().remove(&id).is_some() {
StatusCode::NO_CONTENT
} else {
Expand Down
5 changes: 3 additions & 2 deletions examples/versioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use axum::{
};
use http::Response;
use http::StatusCode;
use std::collections::HashMap;
use std::net::SocketAddr;

#[tokio::main]
Expand Down Expand Up @@ -53,15 +54,15 @@ where
type Rejection = Response<Body>;

async fn from_request(req: &mut RequestParts<B>) -> Result<Self, Self::Rejection> {
let params = extract::UrlParamsMap::from_request(req)
let params = extract::Path::<HashMap<String, String>>::from_request(req)
.await
.map_err(IntoResponse::into_response)?;

let version = params
.get("version")
.ok_or_else(|| (StatusCode::NOT_FOUND, "version param missing").into_response())?;

match version {
match version.as_str() {
"v1" => Ok(Version::V1),
"v2" => Ok(Version::V2),
"v3" => Ok(Version::V3),
Expand Down
2 changes: 2 additions & 0 deletions src/extract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ mod content_length_limit;
mod extension;
mod form;
mod json;
mod path;
mod query;
mod raw_query;
mod request_parts;
Expand All @@ -273,6 +274,7 @@ pub use self::{
extractor_middleware::extractor_middleware,
form::Form,
json::Json,
path::Path,
query::Query,
raw_query::RawQuery,
request_parts::{Body, BodyStream},
Expand Down
Loading