also pass rpcenv to async handlers

This commit is contained in:
Dietmar Maurer 2019-01-27 10:18:52 +01:00
parent d2ab5f19e2
commit e82dad9700
3 changed files with 22 additions and 7 deletions

View File

@ -20,7 +20,7 @@ pub trait RpcEnvironment {
type ApiHandlerFn = fn(Value, &ApiMethod, &mut dyn RpcEnvironment) -> Result<Value, Error>; type ApiHandlerFn = fn(Value, &ApiMethod, &mut dyn RpcEnvironment) -> Result<Value, Error>;
type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod) -> Result<BoxFut, Error>; type ApiAsyncHandlerFn = fn(Parts, Body, Value, &ApiAsyncMethod, &mut dyn RpcEnvironment) -> Result<BoxFut, Error>;
pub struct ApiMethod { pub struct ApiMethod {
pub parameters: ObjectSchema, pub parameters: ObjectSchema,

View File

@ -47,7 +47,13 @@ impl Future for UploadCaTar {
} }
} }
fn upload_catar(parts: Parts, req_body: Body, param: Value, _info: &ApiAsyncMethod) -> Result<BoxFut, Error> { fn upload_catar(
parts: Parts,
req_body: Body,
param: Value,
_info: &ApiAsyncMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<BoxFut, Error> {
let store = tools::required_string_param(&param, "store")?; let store = tools::required_string_param(&param, "store")?;
let archive_name = tools::required_string_param(&param, "archive_name")?; let archive_name = tools::required_string_param(&param, "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<BoxFut, Error> { fn download_catar(
_parts: Parts,
_req_body: Body,
param: Value,
_info: &ApiAsyncMethod,
_rpcenv: &mut RpcEnvironment,
) -> Result<BoxFut, Error> {
let store = tools::required_string_param(&param, "store")?; let store = tools::required_string_param(&param, "store")?;
let archive_name = tools::required_string_param(&param, "archive_name")?; let archive_name = tools::required_string_param(&param, "archive_name")?;

View File

@ -159,6 +159,7 @@ fn get_request_parameters_async(
} }
fn handle_sync_api_request( fn handle_sync_api_request(
mut rpcenv: RestEnvironment,
info: &'static ApiMethod, info: &'static ApiMethod,
formatter: &'static OutputFormatter, formatter: &'static OutputFormatter,
parts: Parts, parts: Parts,
@ -170,7 +171,6 @@ fn handle_sync_api_request(
let resp = params let resp = params
.and_then(move |params| { .and_then(move |params| {
let mut rpcenv = RestEnvironment::new();
let resp = match (info.handler)(params, info, &mut rpcenv) { let resp = match (info.handler)(params, info, &mut rpcenv) {
Ok(data) => (formatter.format_result)(data, &rpcenv), Ok(data) => (formatter.format_result)(data, &rpcenv),
Err(err) => (formatter.format_error)(err), Err(err) => (formatter.format_error)(err),
@ -182,6 +182,7 @@ fn handle_sync_api_request(
} }
fn handle_async_api_request( fn handle_async_api_request(
mut rpcenv: RestEnvironment,
info: &'static ApiAsyncMethod, info: &'static ApiAsyncMethod,
formatter: &'static OutputFormatter, formatter: &'static OutputFormatter,
parts: Parts, 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, Ok(future) => future,
Err(err) => { Err(err) => {
let resp = (formatter.format_error)(Error::from(err)); let resp = (formatter.format_error)(Error::from(err));
@ -396,6 +397,8 @@ pub fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> BoxFut {
println!("REQUEST {} {}", method, path); println!("REQUEST {} {}", method, path);
println!("COMPO {:?}", components); println!("COMPO {:?}", components);
let mut rpcenv = RestEnvironment::new();
if comp_len >= 1 && components[0] == "api2" { if comp_len >= 1 && components[0] == "api2" {
println!("GOT API REQUEST"); println!("GOT API REQUEST");
if comp_len >= 2 { if comp_len >= 2 {
@ -414,10 +417,10 @@ pub fn handle_request(api: Arc<ApiConfig>, req: Request<Body>) -> BoxFut {
match api.find_method(&components[2..], method, &mut uri_param) { match api.find_method(&components[2..], method, &mut uri_param) {
MethodDefinition::None => {} MethodDefinition::None => {}
MethodDefinition::Simple(api_method) => { 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) => { 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);
} }
} }
} }