api: replace handler() with call()

An `fn` type can be more annoying to produce in some generic
cases, and we haven't really needed it yet.

Signed-off-by: Wolfgang Bumiller <wry.git@bumiller.com>
This commit is contained in:
Wolfgang Bumiller 2019-06-23 11:19:49 +02:00
parent b4378755f0
commit c8e11115d2
7 changed files with 18 additions and 27 deletions

View File

@ -26,13 +26,12 @@ async fn route_request(request: Request<Body>) -> Result<http::Response<Body>, E
.lookup(path) .lookup(path)
.ok_or_else(|| format_err!("missing path: {}", path))?; .ok_or_else(|| format_err!("missing path: {}", path))?;
let handler = target target
.get .get
.as_ref() .as_ref()
.ok_or_else(|| format_err!("no GET method for: {}", path))? .ok_or_else(|| format_err!("no GET method for: {}", path))?
.handler(); .call(params.unwrap_or(Value::Null))
.await
Ok(handler(params.unwrap_or(Value::Null)).await?)
} }
type BoxFut = Box<dyn futures_01::Future<Item = Response<Body>, Error = hyper::Error> + Send>; type BoxFut = Box<dyn futures_01::Future<Item = Response<Body>, Error = hyper::Error> + Send>;

View File

@ -357,8 +357,8 @@ fn handle_function(
#fn_api_reload_timezone #fn_api_reload_timezone
} }
fn handler(&self) -> fn(::serde_json::Value) -> ::proxmox::api::ApiFuture<#body_type> { fn call(&self, params: ::serde_json::Value) -> ::proxmox::api::ApiFuture<#body_type> {
#struct_name::wrapped_api_handler #struct_name::wrapped_api_handler(params)
} }
} }
}); });

View File

@ -111,7 +111,7 @@ fn check_body(router: &Router<Bytes>, path: &str, expect: &'static str) {
.get .get
.as_ref() .as_ref()
.expect("expected GET method on router at path"); .expect("expected GET method on router at path");
let fut = method.handler()(parameters.unwrap_or(Value::Null)); let fut = method.call(parameters.unwrap_or(Value::Null));
let resp = futures::executor::block_on(fut) let resp = futures::executor::block_on(fut)
.expect("expected `GET` on test_body to return successfully"); .expect("expected `GET` on test_body to return successfully");
assert!(resp.status() == 200, "test response should have status 200"); assert!(resp.status() == 200, "test response should have status 200");

View File

@ -45,7 +45,7 @@ fn check_parameter(
.get .get
.as_ref() .as_ref()
.expect("expected GET method on router at path"); .expect("expected GET method on router at path");
let fut = method.handler()(parameters); let fut = method.call(parameters);
match (futures::executor::block_on(fut), expect) { match (futures::executor::block_on(fut), expect) {
(Ok(resp), Ok(exp)) => { (Ok(resp), Ok(exp)) => {
assert_eq!(resp.status(), 200, "test response should have status 200"); assert_eq!(resp.status(), 200, "test response should have status 200");

View File

@ -15,7 +15,7 @@ pub trait ApiMethodInfo {
fn return_type(&self) -> &'static TypeInfo; fn return_type(&self) -> &'static TypeInfo;
fn protected(&self) -> bool; fn protected(&self) -> bool;
fn reload_timezone(&self) -> bool; fn reload_timezone(&self) -> bool;
fn handler(&self) -> fn(Value) -> super::ApiFuture<Self::Body>; fn call(&self, params: Value) -> super::ApiFuture<Self::Body>;
} }
/// Shortcut to not having to type it out. This function signature is just a dummy and not yet /// Shortcut to not having to type it out. This function signature is just a dummy and not yet
@ -109,8 +109,8 @@ impl<Body> ApiMethodInfo for ApiMethod<Body> {
self.reload_timezone self.reload_timezone
} }
fn handler(&self) -> fn(Value) -> super::ApiFuture<Body> { fn call(&self, params: Value) -> super::ApiFuture<Body> {
self.handler (self.handler)(params)
} }
} }

View File

@ -165,14 +165,8 @@ where
} }
fn call(&self, params: Value) -> super::ApiFuture<Bytes> { fn call(&self, params: Value) -> super::ApiFuture<Bytes> {
//async fn real_handler(this: &Self, params: Value) -> ApiOutput<Bytes> {
// (Self::handler(this))(params)
// .await
// .map(|res| res.into())
//}
let handler = self.handler();
use futures::future::TryFutureExt; use futures::future::TryFutureExt;
Box::pin(handler(params).map_ok(|res| res.map(|body| body.into()))) Box::pin(ApiMethodInfo::call(self, params).map_ok(|res| res.map(|body| body.into())))
} }
} }

View File

@ -1,7 +1,5 @@
#![feature(async_await)] #![feature(async_await)]
use std::pin::Pin;
use bytes::Bytes; use bytes::Bytes;
use proxmox_api::Router; use proxmox_api::Router;
@ -52,12 +50,6 @@ fn check_with_matched_params(
param_name, param_name,
)); ));
let apifn = target
.get
.as_ref()
.expect(&format!("expected GET method on {}", path))
.handler();
let arg = params[param_name].as_str().expect(&format!( let arg = params[param_name].as_str().expect(&format!(
"expected lookup() to fill the '{}' parameter", "expected lookup() to fill the '{}' parameter",
param_name param_name
@ -69,7 +61,13 @@ fn check_with_matched_params(
path, param_name, param_value, path, param_name, param_value,
); );
let response = futures::executor::block_on(Pin::from(apifn(params))) let apifut = target
.get
.as_ref()
.expect(&format!("expected GET method on {}", path))
.call(params);
let response = futures::executor::block_on(apifut)
.expect("expected the simple test api function to be ready immediately"); .expect("expected the simple test api function to be ready immediately");
assert_eq!(response.status(), 200, "response status must be 200"); assert_eq!(response.status(), 200, "response status must be 200");