product_os_router/extractors.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
use core::future::Future;
use std::prelude::v1::*;
use std::convert::Infallible;
use axum::Extension;
use axum::extract::FromRequestParts;
use product_os_request::Method;
// use product_os_http::{request::Parts, Request};
use axum::http::{request::Parts, Request};
use async_trait::async_trait;
use product_os_request::Method::PUT;
#[derive(Debug, Clone)]
pub struct RequestMethod(pub Method);
#[async_trait]
impl<S> FromRequestParts<S> for RequestMethod
where
S: Send + Sync,
{
type Rejection = Infallible;
fn from_request_parts(parts: &mut Parts, state: &S) -> impl Future<Output = Result<Self, Self::Rejection>> + Send {
let method = match parts.method.to_owned() {
axum::http::method::Method::GET => Method::GET,
axum::http::method::Method::POST => Method::POST,
axum::http::method::Method::PATCH => Method::PATCH,
axum::http::method::Method::PUT => Method::PUT,
axum::http::method::Method::DELETE => Method::DELETE,
axum::http::method::Method::TRACE => Method::TRACE,
axum::http::method::Method::HEAD => Method::HEAD,
axum::http::method::Method::OPTIONS => Method::OPTIONS,
axum::http::method::Method::CONNECT => Method::CONNECT,
_ => Method::ANY
};
async {
Ok(RequestMethod(method))
}
}
}
/*
use axum::{
extract::{Json, TypedHeader, Path, Extension, Query},
routing::post,
http::{Request, header::HeaderMap},
body::{Bytes, Body},
Router,
};
use serde_json::Value;
use headers::UserAgent;
use std::collections::BTreeMap;
// `Path` gives you the path parameters and deserializes them. See its docs for
// more details
async fn path(Path(path): Path<u32>) {}
// `Query` gives you the query parameters and deserializes them.
async fn query(Query(params): Query<BTreeMap<String, String>>) {}
// `HeaderMap` gives you all the headers
async fn headers(headers: HeaderMap) {}
// `TypedHeader` can be used to extract a single header
// note this requires you've enabled axum's `headers` feature
async fn user_agent(TypedHeader(user_agent): TypedHeader<UserAgent>) {}
// `String` consumes the request body and ensures it is valid utf-8
async fn string(body: String) {}
// `Bytes` gives you the raw request body
async fn bytes(body: Bytes) {}
// We've already seen `Json` for parsing the request body as json
async fn json(Json(payload): Json<Value>) {}
// `Request` gives you the whole request for maximum control
async fn request(request: Request<Body>) {}
// `Extension` extracts data from "request extensions"
// This is commonly used to share state with handlers
async fn extension(Extension(state): Extension<State>) {}
*/