-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Sam Bonill
authored and
Sam Bonill
committed
Oct 14, 2022
1 parent
3847381
commit c41122f
Showing
5 changed files
with
178 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,123 @@ | ||
use std::collections::HashMap; | ||
|
||
use async_trait::async_trait; | ||
use axum::extract::{ | ||
rejection::QueryRejection, | ||
FromRef, FromRequest, | ||
}; | ||
pub use axum::http::Request; | ||
use axum::{ | ||
body::{Bytes, HttpBody}, | ||
extract::{rejection::{JsonRejection, FailedToDeserializeQueryString}, FromRef, FromRequest, Query, Path}, | ||
BoxError, Json, http::Extensions, | ||
}; | ||
use hyper::{HeaderMap, Method, Uri, Version}; | ||
use serde::de::DeserializeOwned; | ||
use serde_json::Value; | ||
|
||
use crate::Body; | ||
|
||
type HashMapRequest = HashMap<String, String>; | ||
|
||
#[derive(Debug)] | ||
pub struct Context<InnerState = (), B = Body> { | ||
params_map: HashMapRequest, | ||
query_map: HashMapRequest, | ||
req: Request<B>, | ||
state: InnerState, | ||
pub struct Context<InnerState = ()> { | ||
params_map: Option<HashMapRequest>, | ||
query_map: Option<HashMapRequest>, | ||
bytes: Bytes, | ||
inner_state: InnerState, | ||
headers: HeaderMap, | ||
method: Method, | ||
uri: Uri, | ||
version: Version, | ||
} | ||
|
||
// update context to get params and query | ||
// update context to get params and query implementar params y query genericos que no solo soporte maps si no tambien otros structs | ||
// Json | ||
|
||
impl<InnerState, B> Context<InnerState, B> { | ||
pub fn params(&self, key: &'static str) -> String { | ||
match self.params_map.get(key) { | ||
Some(value) => value.clone(), | ||
None => String::new(), | ||
} | ||
impl<InnerState> Context<InnerState> { | ||
async fn parse_query(&mut self) { | ||
// get query | ||
let query = self.uri.query().unwrap_or_default(); | ||
match serde_urlencoded::from_str(query) { | ||
Ok(value) => self.query_map = Some(value), | ||
Err(_) => (), | ||
}; | ||
} | ||
pub fn request(&self) -> &Request<B> { | ||
&self.req | ||
async fn parse_params(&mut self) { | ||
todo!() | ||
} | ||
pub fn params(&self, _key: &'static str) -> String { | ||
todo!() | ||
} | ||
|
||
pub fn body(&self) -> String { | ||
String::from_utf8(self.bytes.to_vec()).expect("") | ||
} | ||
pub fn bytes(&self) -> &Bytes { | ||
&self.bytes | ||
} | ||
pub fn state(&self) -> &InnerState { | ||
&self.state | ||
&self.inner_state | ||
} | ||
pub fn all_params(&self) -> &HashMapRequest { | ||
pub fn all_params(&self) -> &Option<HashMapRequest> { | ||
&self.params_map | ||
} | ||
pub fn query(&self, key: &'static str) -> String { | ||
match self.query_map.get(key) { | ||
Some(value) => value.clone(), | ||
None => String::new(), | ||
} | ||
todo!() | ||
} | ||
pub fn all_query(&self) -> &HashMapRequest { | ||
pub fn all_query(&self) -> &Option<HashMapRequest> { | ||
&self.query_map | ||
} | ||
|
||
pub async fn payload<T: DeserializeOwned + Default>(&self) -> Result<Json<T>, JsonRejection> { | ||
// forse parsing | ||
let request = Request::builder() | ||
.header("Content-Type", "application/json") | ||
.body(Body::from(self.bytes.clone())); | ||
|
||
let request = match request { | ||
Ok(value) => value, | ||
Err(_) => Request::default(), | ||
}; | ||
|
||
Json::from_request(request, &()).await | ||
} | ||
|
||
pub fn json(&self, payload: Value) -> Json<Value> { | ||
Json(payload) | ||
} | ||
|
||
pub fn send(value: &str) -> &str { | ||
value | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl<OuterState, InnerState, B> FromRequest<OuterState, B> for Context<InnerState, B> | ||
impl<OuterState, InnerState, B> FromRequest<OuterState, B> for Context<InnerState> | ||
where | ||
OuterState: Send + Sync + 'static, | ||
InnerState: FromRef<OuterState>, | ||
B: Send + 'static | ||
InnerState: FromRef<OuterState> + Send + Sync, | ||
B: HttpBody + Send + 'static, | ||
B::Data: Send, | ||
B::Error: Into<BoxError>, | ||
{ | ||
type Rejection = QueryRejection; | ||
type Rejection = JsonRejection; | ||
|
||
async fn from_request( | ||
req: axum::http::Request<B>, | ||
state: &OuterState, | ||
) -> Result<Self, Self::Rejection> { | ||
let inner_state = InnerState::from_ref(state); | ||
let headers = req.headers().clone(); | ||
let method = req.method().clone(); | ||
let uri = req.uri().clone(); | ||
let version = req.version(); | ||
let bytes = Bytes::from_request(req, state).await?; | ||
Ok(Context { | ||
req: req, | ||
state: inner_state, | ||
params_map: HashMap::new(), | ||
query_map: HashMap::new(), | ||
version, | ||
headers, | ||
method, | ||
uri, | ||
bytes, | ||
inner_state, | ||
params_map: None, | ||
query_map: None, | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters