From e82dad970088f4eb8363fb0f2ca9ee9962d1f6db Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Sun, 27 Jan 2019 10:18:52 +0100 Subject: [PATCH] also pass rpcenv to async handlers --- src/api/router.rs | 2 +- src/api2/admin/datastore/catar.rs | 16 ++++++++++++++-- src/server/rest.rs | 11 +++++++---- 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/api/router.rs b/src/api/router.rs index d19ced59..ef973b60 100644 --- a/src/api/router.rs +++ b/src/api/router.rs @@ -20,7 +20,7 @@ pub trait RpcEnvironment { type ApiHandlerFn = fn(Value, &ApiMethod, &mut dyn RpcEnvironment) -> Result; -type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod) -> Result; +type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod, &mut dyn RpcEnvironment) -> Result; pub struct ApiMethod { pub parameters: ObjectSchema, diff --git a/src/api2/admin/datastore/catar.rs b/src/api2/admin/datastore/catar.rs index 7fa8fe34..883cfbbb 100644 --- a/src/api2/admin/datastore/catar.rs +++ b/src/api2/admin/datastore/catar.rs @@ -47,7 +47,13 @@ impl Future for UploadCaTar { } } -fn upload_catar(parts: Parts, req_body: Body, param: Value, _info: &ApiAsyncMethod) -> Result { +fn upload_catar( + parts: Parts, + req_body: Body, + param: Value, + _info: &ApiAsyncMethod, + _rpcenv: &mut RpcEnvironment, +) -> Result { let store = tools::required_string_param(¶m, "store")?; let archive_name = tools::required_string_param(¶m, "archive_name")?; @@ -109,7 +115,13 @@ pub fn api_method_upload_catar() -> ApiAsyncMethod { ) } -fn download_catar(_parts: Parts, _req_body: Body, param: Value, _info: &ApiAsyncMethod) -> Result { +fn download_catar( + _parts: Parts, + _req_body: Body, + param: Value, + _info: &ApiAsyncMethod, + _rpcenv: &mut RpcEnvironment, +) -> Result { let store = tools::required_string_param(¶m, "store")?; let archive_name = tools::required_string_param(¶m, "archive_name")?; diff --git a/src/server/rest.rs b/src/server/rest.rs index 180455d3..8dc80d85 100644 --- a/src/server/rest.rs +++ b/src/server/rest.rs @@ -159,6 +159,7 @@ fn get_request_parameters_async( } fn handle_sync_api_request( + mut rpcenv: RestEnvironment, info: &'static ApiMethod, formatter: &'static OutputFormatter, parts: Parts, @@ -170,7 +171,6 @@ fn handle_sync_api_request( let resp = params .and_then(move |params| { - let mut rpcenv = RestEnvironment::new(); let resp = match (info.handler)(params, info, &mut rpcenv) { Ok(data) => (formatter.format_result)(data, &rpcenv), Err(err) => (formatter.format_error)(err), @@ -182,6 +182,7 @@ fn handle_sync_api_request( } fn handle_async_api_request( + mut rpcenv: RestEnvironment, info: &'static ApiAsyncMethod, formatter: &'static OutputFormatter, parts: Parts, @@ -211,7 +212,7 @@ fn handle_async_api_request( } }; - match (info.handler)(parts, req_body, params, info) { + match (info.handler)(parts, req_body, params, info, &mut rpcenv) { Ok(future) => future, Err(err) => { let resp = (formatter.format_error)(Error::from(err)); @@ -396,6 +397,8 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { println!("REQUEST {} {}", method, path); println!("COMPO {:?}", components); + let mut rpcenv = RestEnvironment::new(); + if comp_len >= 1 && components[0] == "api2" { println!("GOT API REQUEST"); if comp_len >= 2 { @@ -414,10 +417,10 @@ pub fn handle_request(api: Arc, req: Request) -> BoxFut { 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); + return handle_sync_api_request(rpcenv, api_method, formatter, parts, body, uri_param); } MethodDefinition::Async(async_method) => { - return handle_async_api_request(async_method, formatter, parts, body, uri_param); + return handle_async_api_request(rpcenv, async_method, formatter, parts, body, uri_param); } } }