From 1b4f57a496b80c7148d4099c3f7806852a57d3c6 Mon Sep 17 00:00:00 2001 From: Sam Bonill Date: Thu, 17 Nov 2022 00:14:14 -0500 Subject: [PATCH] add ContextPart --- Cargo.toml | 2 +- src/app.rs | 2 +- src/http/request.rs | 143 ++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 4 files changed, 146 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4bfa0ef..82d1795 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "graphul" -version = "0.4.2" +version = "0.4.3" edition = "2021" license = "MIT" categories = ["asynchronous", "network-programming", "web-programming::http-server"] diff --git a/src/app.rs b/src/app.rs index ded6c07..634fecb 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1 +1 @@ -pub const VERSION: &str = "0.4.2"; +pub const VERSION: &str = "0.4.3"; diff --git a/src/http/request.rs b/src/http/request.rs index 31cfc1d..81e9bc1 100644 --- a/src/http/request.rs +++ b/src/http/request.rs @@ -194,3 +194,146 @@ where }) } } + +#[derive(Debug)] +pub struct ContextPart { + params_map: HashMapRequest, + query_map: HashMapRequest, + inner_state: InnerState, + headers: HeaderMap, + method: Method, + uri: Uri, + version: Version, +} + +// update context to get params and query implementar params y query genericos que no solo soporte maps si no tambien otros structs +// Json + +impl ContextPart { + pub fn headers(&self) -> &HeaderMap { + &self.headers + } + pub fn method(&self) -> &Method { + &self.method + } + pub fn version(&self) -> &Version { + &self.version + } + pub fn uri(&self) -> &Uri { + &self.uri + } + pub fn state(&self) -> &InnerState { + &self.inner_state + } + + pub async fn parse_params(&self) -> Result, JsonRejection> { + let value = match serde_json::to_string(&self.params_map) { + Ok(data) => data, + Err(_) => String::new(), + }; + let request = Request::builder() + .header("Content-Type", "application/json") + .body(Body::from(value)); + + let request = match request { + Ok(value) => value, + Err(_) => Request::default(), + }; + + Json::from_request(request, &()).await + } + pub fn all_params(&self) -> &HashMapRequest { + &self.params_map + } + pub fn params(&self, key: &'static str) -> String { + match self.params_map.get(key) { + Some(value) => value.clone(), + None => String::new(), + } + } + pub async fn parse_query(&self) -> Result, JsonRejection> { + let value = match serde_json::to_string(&self.query_map) { + Ok(data) => data, + Err(_) => String::new(), + }; + let request = Request::builder() + .header("Content-Type", "application/json") + .body(Body::from(value)); + + let request = match request { + Ok(value) => value, + Err(_) => Request::default(), + }; + + Json::from_request(request, &()).await + } + pub fn query(&self, key: &'static str) -> String { + match self.query_map.get(key) { + Some(value) => value.clone(), + None => String::new(), + } + } + pub fn all_query(&self) -> &HashMapRequest { + &self.query_map + } + + pub fn json(&self, payload: Value) -> Json { + Json(payload) + } + + pub fn send(value: &str) -> &str { + value + } +} + +#[async_trait] +impl FromRequestParts for ContextPart +where + OuterState: Send + Sync + 'static, + InnerState: FromRef + Send + Sync, +{ + type Rejection = JsonRejection; + + async fn from_request_parts( + parts: &mut Parts, + state: &OuterState, + ) -> Result { + let inner_state = InnerState::from_ref(state); + let headers = parts.headers.clone(); + let method = parts.method.clone(); + let uri = parts.uri.clone(); + let version = parts.version; + let mut params_map = HashMap::new(); + let mut query_map = HashMap::new(); + let result_params: Result, PathRejection> = + Path::from_request_parts(parts, &()).await; + + if let Ok(params) = result_params { + match params { + Path(parse_params) => { + params_map = parse_params; + } + } + } + + let result_query: Result, QueryRejection> = + Query::from_request_parts(parts, &()).await; + if let Ok(params) = result_query { + match params { + Query(parse_params) => { + query_map = parse_params; + } + } + } + + Ok(ContextPart { + version, + headers, + method, + uri, + inner_state, + params_map, + query_map, + }) + } +} diff --git a/src/lib.rs b/src/lib.rs index c1caae1..636cb6b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,6 +23,7 @@ use axum::routing::{delete, get, get_service, head, options, patch, post, put, t use axum::Router; pub use http::request::Context; +pub use http::request::ContextPart; use http::resource::Resource; use http::StatusCode; use hyper::service::Service;