more cleanups

This commit is contained in:
Dietmar Maurer 2018-12-06 10:41:57 +01:00
parent d3369f819d
commit a4b7c3f2df
2 changed files with 18 additions and 14 deletions

View File

@ -3,12 +3,13 @@ use failure::*;
use crate::api::schema::*; use crate::api::schema::*;
use serde_json::{Value}; use serde_json::{Value};
use std::collections::HashMap; use std::collections::HashMap;
use std::sync::Arc;
type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>; type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>;
pub struct ApiMethod { pub struct ApiMethod {
pub parameters: ObjectSchema, pub parameters: ObjectSchema,
pub returns: Schema, pub returns: Arc<Schema>,
pub handler: ApiHandlerFn, pub handler: ApiHandlerFn,
} }
@ -18,10 +19,17 @@ impl ApiMethod {
Self { Self {
parameters, parameters,
handler, handler,
returns: Schema::Null, returns: Arc::new(Schema::Null),
} }
} }
pub fn returns<S: Into<Arc<Schema>>>(mut self, schema: S) -> Self {
self.returns = schema.into();
self
}
} }
pub enum SubRoute { pub enum SubRoute {

View File

@ -38,19 +38,17 @@ fn get_version(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
})) }))
} }
pub fn router() -> Router { pub fn router() -> Router {
let route4 = Router::new() let route4 = Router::new()
.get(ApiMethod { .get(ApiMethod::new(
parameters: ObjectSchema::new("Another Endpoint."), |param, _info| {
returns: Schema::Null,
handler: |param, _info| {
println!("This is a clousure handler: {}", param); println!("This is a clousure handler: {}", param);
Ok(json!(null)) Ok(json!(null))
}, },
}); ObjectSchema::new("Another Endpoint."))
.returns(Schema::Null));
let nodeinfo = Router::new() let nodeinfo = Router::new()
@ -70,11 +68,9 @@ pub fn router() -> Router {
ObjectSchema::new("Proxmox Backup Server API version."))); ObjectSchema::new("Proxmox Backup Server API version.")));
let route = Router::new() let route = Router::new()
.get(ApiMethod { .get(ApiMethod::new(
handler: get_version, get_version,
parameters: ObjectSchema::new("Directory index."), ObjectSchema::new("Directory index.")))
returns: Schema::Null,
})
.subdir("version", version) .subdir("version", version)
.subdir("nodes", nodes); .subdir("nodes", nodes);