-
-
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.
create Context Struct and update Cargo.toml
- Loading branch information
Sam Bonill
authored and
Sam Bonill
committed
Oct 10, 2022
1 parent
6e5090c
commit c70fabd
Showing
5 changed files
with
93 additions
and
17 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 |
---|---|---|
|
@@ -11,3 +11,4 @@ use crate::Body; | |
pub type Request = axum::http::Request<Body>; | ||
|
||
pub type StatusCode = hyper::StatusCode; | ||
|
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,26 +1,73 @@ | ||
use super::response::{Response}; | ||
use std::{collections::HashMap}; | ||
|
||
use async_trait::async_trait; | ||
use axum::extract::{FromRequest, rejection::{QueryRejection, PathRejection}, RequestParts, Query, Path}; | ||
|
||
use crate::Body; | ||
|
||
|
||
|
||
type HashMapRequest = HashMap<String, String>; | ||
|
||
#[derive(Debug)] | ||
pub struct Context { | ||
params_map: HashMapRequest, | ||
query_map: HashMapRequest, | ||
} | ||
|
||
// get context from request, in progressss...... | ||
impl Context { | ||
pub fn new() -> Self { | ||
Self { | ||
pub fn params(&self, key: &'static str) -> String { | ||
match self.params_map.get(key) { | ||
Some(value) => value.clone(), | ||
None => String::new() | ||
} | ||
} | ||
// part of request | ||
pub fn args(&self, _name: &str) -> Option<String> { | ||
None | ||
pub fn all_params(&self) -> HashMapRequest { | ||
self.params_map.clone() | ||
} | ||
pub fn params(&self, _name: &str) -> Option<&str> { | ||
None | ||
pub fn query(&self, key: &'static str) -> String { | ||
match self.query_map.get(key) { | ||
Some(value) => value.clone(), | ||
None => String::new() | ||
} | ||
} | ||
// part of response | ||
pub fn send(&self, _body: &'static str) -> Response { | ||
todo!() | ||
pub fn all_query(&self) -> HashMapRequest { | ||
self.query_map.clone() | ||
} | ||
pub fn json(&self, _body: serde_json::Value) -> Response { | ||
todo!() | ||
} | ||
|
||
#[async_trait] | ||
impl FromRequest<Body> for Context { | ||
type Rejection = QueryRejection; | ||
|
||
async fn from_request(req: &mut RequestParts<Body>) -> Result<Self, Self::Rejection> { | ||
let mut query_map: HashMapRequest = HashMap::new(); | ||
let mut params_map: HashMapRequest = HashMap::new(); | ||
// get query | ||
let result_query : Result<Query<HashMapRequest>, QueryRejection> = Query::from_request(req).await; | ||
match result_query { | ||
Ok(query) => { | ||
match query { | ||
Query(parse_query) => { | ||
query_map = parse_query; | ||
} | ||
} | ||
}, | ||
Err(_) => {} | ||
} | ||
// get params | ||
let result_params : Result<Path<HashMapRequest>, PathRejection> = Path::from_request(req).await; | ||
match result_params { | ||
Ok(params) => { | ||
match params { | ||
Path(parse_params) => { | ||
params_map = parse_params; | ||
} | ||
} | ||
}, | ||
Err(_) => {} | ||
} | ||
|
||
Ok(Context { params_map: params_map, query_map: query_map }) | ||
} | ||
} |
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