use const api definitions

This commit is contained in:
Dietmar Maurer 2019-11-21 09:36:41 +01:00
parent 40acfdf04c
commit 3de9361d12
2 changed files with 44 additions and 18 deletions

View File

@ -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 <E: RpcEnvironment + Clone> H2Service<E> {
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)
}
}
}
}
}

View File

@ -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<S: 'static + BuildHasher + Send>(
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::<str>(&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<Env: RpcEnvironment, S: 'static + BuildHasher + S
uri_param: HashMap<String, String, S>,
) -> 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<Env: RpcEnvironment, S: 'static + BuildHasher + S
let resp = Pin::from(params)
.and_then(move |params| {
let mut delay = false;
let resp = match (info.handler.as_ref().unwrap())(params, info, &mut rpcenv) {
let resp = match (handler)(params, info, &mut rpcenv) {
Ok(data) => (formatter.format_data)(data, &rpcenv),
Err(err) => {
if let Some(httperr) = err.downcast_ref::<HttpError>() {
@ -307,13 +317,20 @@ pub fn handle_sync_api_request<Env: RpcEnvironment, S: 'static + BuildHasher + S
pub fn handle_async_api_request<Env: RpcEnvironment>(
rpcenv: Env,
info: &'static ApiAsyncMethod,
info: &'static ApiMethod,
formatter: &'static OutputFormatter,
parts: Parts,
req_body: Body,
uri_param: HashMap<String, String>,
) -> 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<Env: RpcEnvironment>(
}
};
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<ApiConfig>, req: Request<Body>) -> 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 {