pub struct RequestMatcher {
pub router: Router,
}Expand description
if used as static, use tokio::sync::Mutex, this is very importment
use std::sync::Arc;
use axum::http::Method;
use once_cell::sync::Lazy;
use tokio::sync::Mutex;
use serde_json::Value;
use awesome_operates::router::RequestMatcher;
static REQUEST_MATCHER: Lazy<Arc<Mutex<RequestMatcher>>> = Lazy::new(||
{ Arc::new(Mutex::new(RequestMatcher::default())) });
#[tokio::test]
async fn matcher() {
let api = tokio::fs::read_to_string("api.json").await.unwrap();
let body = serde_json::from_str::<Value>(&api).unwrap();
let mut request_matcher = RequestMatcher::from_openapi(&body, "").unwrap();
// use directly
request_matcher.match_request_to_response(Method::GET, "/api/test", None).await.unwrap();
// or use global
*REQUEST_MATCHER.lock().await = request_matcher;
REQUEST_MATCHER.lock().await.match_request_to_response(Method::GET, "/api/test", None).await.unwrap();
}Fields§
§router: RouterImplementations§
Source§impl RequestMatcher
impl RequestMatcher
pub fn from_openapi(openapi: &Value, path_prefix: &str) -> Result<Self>
pub fn from_route_methods(route_methods: Vec<(String, MethodRouter)>) -> Self
Sourcepub fn openapi_route_handles(
openapi: &Value,
path_prefix: &str,
) -> Result<Vec<(String, MethodRouter)>>
pub fn openapi_route_handles( openapi: &Value, path_prefix: &str, ) -> Result<Vec<(String, MethodRouter)>>
path_prefix is like “/sys-layer”, “/api/v1”
openapi refer to src/test_files/openapi.json
pub fn api_component<'a>( openapi: &'a Value, component_path: Option<&Value>, ) -> Option<&'a Value>
pub async fn match_request_to_response( &mut self, method: Method, path: &str, body: Option<Body>, ) -> Result<Response>
pub async fn match_request_to_json_response( &mut self, method: Method, path: &str, body: Option<Body>, ) -> Result<Value>
pub fn build_request( method: Method, path: &str, body: Option<Body>, ) -> Request<Body>
Trait Implementations§
Source§impl Default for RequestMatcher
impl Default for RequestMatcher
Source§fn default() -> RequestMatcher
fn default() -> RequestMatcher
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for RequestMatcher
impl !RefUnwindSafe for RequestMatcher
impl Send for RequestMatcher
impl Sync for RequestMatcher
impl Unpin for RequestMatcher
impl !UnwindSafe for RequestMatcher
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more