From c1582dcf39e8073fd8120a9fdd211b11fca83c74 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Mon, 14 Jan 2019 12:26:04 +0100 Subject: [PATCH] api/router.rs: allow different types of api methods Added a prototype for file/backup uploads. --- src/server/rest.rs | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/src/server/rest.rs b/src/server/rest.rs index d9475d1a..5d3f23c3 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -82,8 +82,6 @@ impl Service for ApiService { } } -type BoxFut = Box, Error = failure::Error> + Send>; - #[derive(Debug, Fail)] pub struct HttpError { pub code: StatusCode, @@ -180,6 +178,31 @@ fn handle_sync_api_request( Box::new(resp) } +fn handle_upload_api_request( + info: &'static ApiUploadMethod, + formatter: &'static OutputFormatter, + parts: Parts, + req_body: Body, + uri_param: HashMap, +) -> BoxFut +{ + // fixme: convert parameters to Json + let mut param_list: Vec<(String, String)> = vec![]; + + for (k, v) in uri_param { + param_list.push((k.clone(), v.clone())); + } + + let params = match parse_parameter_strings(¶m_list, &info.parameters, true) { + Ok(v) => v, + Err(err) => { + return Box::new(future::err(err.into())); + } + }; + + (info.handler)(req_body, params, info) +} + fn get_index() -> BoxFut { let nodename = "unknown"; @@ -362,9 +385,15 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { let mut uri_param = HashMap::new(); - if let Some(api_method) = api.find_method(&components[2..], method, &mut uri_param) { - // fixme: handle auth - return handle_sync_api_request(api_method, formatter, parts, body, uri_param); + // fixme: handle auth + match api.find_method(&components[2..], method, &mut uri_param) { + MethodDefinition::None => {} + MethodDefinition::Simple(api_method) => { + return handle_sync_api_request(api_method, formatter, parts, body, uri_param); + } + MethodDefinition::Upload(upload_method) => { + return handle_upload_api_request(upload_method, formatter, parts, body, uri_param); + } } } } else {