From 3de9361d125f7fc7ea10e14dd577e9225b7b2b97 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 21 Nov 2019 09:36:41 +0100 Subject: [PATCH] use const api definitions --- src/server/h2service.rs | 21 +++++++++++++-------- src/server/rest.rs | 41 +++++++++++++++++++++++++++++++---------- 2 files changed, 44 insertions(+), 18 deletions(-) diff --git a/src/server/h2service.rs b/src/server/h2service.rs index 065102b9..87d9d02a 100644 --- a/src/server/h2service.rs +++ b/src/server/h2service.rs @@ -8,6 +8,7 @@ use futures::*; use hyper::{Body, Request, Response, StatusCode}; use crate::tools; +use crate::api_schema::api_handler::*; use crate::api_schema::router::*; use crate::server::formatter::*; use crate::server::WorkerTask; @@ -52,17 +53,21 @@ impl H2Service { let formatter = &JSON_FORMATTER; match self.router.find_method(&components, method, &mut uri_param) { - MethodDefinition::None => { + None => { let err = http_err!(NOT_FOUND, "Path not found.".to_string()); Box::new(future::ok((formatter.format_error)(err))) } - MethodDefinition::Simple(api_method) => { - crate::server::rest::handle_sync_api_request( - self.rpcenv.clone(), api_method, formatter, parts, body, uri_param) - } - MethodDefinition::Async(async_method) => { - crate::server::rest::handle_async_api_request( - self.rpcenv.clone(), async_method, formatter, parts, body, uri_param) + Some(api_method) => { + match api_method.handler { + ApiHandler::Sync(_) => { + crate::server::rest::handle_sync_api_request( + self.rpcenv.clone(), api_method, formatter, parts, body, uri_param) + } + ApiHandler::Async(_) => { + crate::server::rest::handle_async_api_request( + self.rpcenv.clone(), api_method, formatter, parts, body, uri_param) + } + } } } } diff --git a/src/server/rest.rs b/src/server/rest.rs index d91b2621..707d59bb 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -18,6 +18,8 @@ use url::form_urlencoded; use super::environment::RestEnvironment; use super::formatter::*; +use crate::api_schema::rpc_environment::*; +use crate::api_schema::api_handler::*; use crate::api_schema::config::*; use crate::api_schema::router::*; use crate::api_schema::*; @@ -186,7 +188,7 @@ fn get_request_parameters_async( if is_json { let mut params: Value = serde_json::from_str(utf8)?; for (k, v) in uri_param { - if let Some((_optional, prop_schema)) = obj_schema.properties.get::(&k) { + if let Some((_optional, prop_schema)) = obj_schema.lookup(&k) { params[&k] = parse_simple_value(&v, prop_schema)?; } } @@ -269,6 +271,13 @@ pub fn handle_sync_api_request, ) -> BoxFut { + let handler = match info.handler { + ApiHandler::Async(_) => { + panic!("fixme"); + } + ApiHandler::Sync(handler) => handler, + }; + let params = get_request_parameters_async(info, parts, req_body, uri_param); let delay_unauth_time = std::time::Instant::now() + std::time::Duration::from_millis(3000); @@ -276,7 +285,8 @@ pub fn handle_sync_api_request (formatter.format_data)(data, &rpcenv), Err(err) => { if let Some(httperr) = err.downcast_ref::() { @@ -307,13 +317,20 @@ pub fn handle_sync_api_request( rpcenv: Env, - info: &'static ApiAsyncMethod, + info: &'static ApiMethod, formatter: &'static OutputFormatter, parts: Parts, req_body: Body, uri_param: HashMap, ) -> BoxFut { + let handler = match info.handler { + ApiHandler::Sync(_) => { + panic!("fixme"); + } + ApiHandler::Async(handler) => handler, + }; + // fixme: convert parameters to Json let mut param_list: Vec<(String, String)> = vec![]; @@ -336,7 +353,7 @@ pub fn handle_async_api_request( } }; - match (info.handler)(parts, req_body, params, info, Box::new(rpcenv)) { + match (handler)(parts, req_body, params, info, Box::new(rpcenv)) { Ok(future) => future, Err(err) => { let resp = (formatter.format_error)(err); @@ -594,20 +611,24 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { } match api.find_method(&components[2..], method, &mut uri_param) { - MethodDefinition::None => { + None => { let err = http_err!(NOT_FOUND, "Path not found.".to_string()); return Box::new(future::ok((formatter.format_error)(err))); } - MethodDefinition::Simple(api_method) => { + Some(api_method) => { if api_method.protected && env_type == RpcEnvironmentType::PUBLIC { return proxy_protected_request(api_method, parts, body); } else { - return handle_sync_api_request(rpcenv, api_method, formatter, parts, body, uri_param); + match api_method.handler { + ApiHandler::Sync(_) => { + return handle_sync_api_request(rpcenv, api_method, formatter, parts, body, uri_param); + } + ApiHandler::Async(_) => { + return handle_async_api_request(rpcenv, api_method, formatter, parts, body, uri_param); + } + } } } - MethodDefinition::Async(async_method) => { - return handle_async_api_request(rpcenv, async_method, formatter, parts, body, uri_param); - } } } } else {