code cleanup
This commit is contained in:
parent
c578fcd9e2
commit
d3369f819d
@ -4,10 +4,24 @@ use crate::api::schema::*;
|
||||
use serde_json::{Value};
|
||||
use std::collections::HashMap;
|
||||
|
||||
type ApiHandlerFn = fn(Value, &ApiMethod) -> Result<Value, Error>;
|
||||
|
||||
pub struct ApiMethod {
|
||||
pub parameters: ObjectSchema,
|
||||
pub returns: Schema,
|
||||
pub handler: fn(Value, &ApiMethod) -> Result<Value, Error>,
|
||||
pub handler: ApiHandlerFn,
|
||||
}
|
||||
|
||||
impl ApiMethod {
|
||||
|
||||
pub fn new(handler: ApiHandlerFn, parameters: ObjectSchema) -> Self {
|
||||
Self {
|
||||
parameters,
|
||||
handler,
|
||||
returns: Schema::Null,
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub enum SubRoute {
|
||||
@ -36,13 +50,30 @@ impl Router {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn subdir<S: Into<String>>(mut self, subdir: S, router: Router) -> Self {
|
||||
if let SubRoute::None = self.subroute {
|
||||
self.subroute = SubRoute::Hash(HashMap::new());
|
||||
}
|
||||
match self.subroute {
|
||||
SubRoute::Hash(ref mut map) => {
|
||||
map.insert(subdir.into(), router);
|
||||
}
|
||||
_ => panic!("unexpected subroute type"),
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn subdirs(mut self, map: HashMap<String, Router>) -> Self {
|
||||
self.subroute = SubRoute::Hash(map);
|
||||
self
|
||||
}
|
||||
|
||||
pub fn match_all<S>(mut self, param_name: S, router: Router) -> Self where S: Into<String> {
|
||||
self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() };
|
||||
pub fn match_all<S: Into<String>>(mut self, param_name: S, router: Router) -> Self {
|
||||
if let SubRoute::None = self.subroute {
|
||||
self.subroute = SubRoute::MatchAll { router: Box::new(router), param_name: param_name.into() };
|
||||
} else {
|
||||
panic!("unexpected subroute type");
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
@ -50,17 +81,17 @@ impl Router {
|
||||
self.get = Some(m);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
pub fn put(mut self, m: ApiMethod) -> Self {
|
||||
self.put = Some(m);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
pub fn post(mut self, m: ApiMethod) -> Self {
|
||||
self.post = Some(m);
|
||||
self
|
||||
}
|
||||
|
||||
|
||||
pub fn delete(mut self, m: ApiMethod) -> Self {
|
||||
self.delete = Some(m);
|
||||
self
|
||||
|
33
src/api3.rs
33
src/api3.rs
@ -54,27 +54,20 @@ pub fn router() -> Router {
|
||||
|
||||
|
||||
let nodeinfo = Router::new()
|
||||
.get(ApiMethod {
|
||||
handler: test_sync_api_handler,
|
||||
parameters: ObjectSchema::new("This is a simple test.")
|
||||
.optional("force", BooleanSchema::new("Test for boolean options")),
|
||||
returns: Schema::Null,
|
||||
})
|
||||
.subdirs({
|
||||
let mut map = HashMap::new();
|
||||
map.insert("subdir3".into(), route4);
|
||||
map
|
||||
});
|
||||
.get(ApiMethod::new(
|
||||
test_sync_api_handler,
|
||||
ObjectSchema::new("This is a simple test.")
|
||||
.optional("force", BooleanSchema::new("Test for boolean options")))
|
||||
)
|
||||
.subdir("subdir3", route4);
|
||||
|
||||
let nodes = Router::new()
|
||||
.match_all("node", nodeinfo);
|
||||
|
||||
let version = Router::new()
|
||||
.get(ApiMethod {
|
||||
handler: get_version,
|
||||
parameters: ObjectSchema::new("Proxmox Backup Server API version."),
|
||||
returns: Schema::Null,
|
||||
});
|
||||
.get(ApiMethod::new(
|
||||
get_version,
|
||||
ObjectSchema::new("Proxmox Backup Server API version.")));
|
||||
|
||||
let route = Router::new()
|
||||
.get(ApiMethod {
|
||||
@ -82,12 +75,8 @@ pub fn router() -> Router {
|
||||
parameters: ObjectSchema::new("Directory index."),
|
||||
returns: Schema::Null,
|
||||
})
|
||||
.subdirs({
|
||||
let mut map = HashMap::new();
|
||||
map.insert("version".into(), version);
|
||||
map.insert("nodes".into(), nodes);
|
||||
map
|
||||
});
|
||||
.subdir("version", version)
|
||||
.subdir("nodes", nodes);
|
||||
|
||||
route
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user