mirror of
https://git.proxmox.com/git/proxmox
synced 2025-08-06 11:34:25 +00:00
proxmox-api: improve router docs
This commit is contained in:
parent
f02462039d
commit
ed426cd99b
@ -47,7 +47,7 @@ pub use error::HttpError;
|
|||||||
/// info: &ApiMethod,
|
/// info: &ApiMethod,
|
||||||
/// rpcenv: &mut dyn RpcEnvironment,
|
/// rpcenv: &mut dyn RpcEnvironment,
|
||||||
/// ) -> Result<Value, Error> {
|
/// ) -> Result<Value, Error> {
|
||||||
/// Ok(json!("hello world!"))
|
/// Ok(json!("Hello world!"))
|
||||||
/// }
|
/// }
|
||||||
///
|
///
|
||||||
/// const API_METHOD_HELLO: ApiMethod = ApiMethod::new(
|
/// const API_METHOD_HELLO: ApiMethod = ApiMethod::new(
|
||||||
|
@ -77,6 +77,25 @@ pub struct Router {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Router {
|
impl Router {
|
||||||
|
/// Create a new Router.
|
||||||
|
///
|
||||||
|
/// Routers are meant to be build a compile time, and you can use
|
||||||
|
/// all `const fn(mut self, ..)` methods to configure them.
|
||||||
|
///
|
||||||
|
///```
|
||||||
|
/// # use failure::*;
|
||||||
|
/// # use serde_json::{json, Value};
|
||||||
|
/// # use proxmox_api::{*, schema::*};
|
||||||
|
/// #
|
||||||
|
/// const API_METHOD_HELLO: ApiMethod = ApiMethod::new(
|
||||||
|
/// &ApiHandler::Sync(&|_, _, _| {
|
||||||
|
/// Ok(json!("Hello world!"))
|
||||||
|
/// }),
|
||||||
|
/// &ObjectSchema::new("Hello World Example", &[])
|
||||||
|
/// );
|
||||||
|
/// const ROUTER: Router = Router::new()
|
||||||
|
/// .get(&API_METHOD_HELLO);
|
||||||
|
///```
|
||||||
pub const fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
get: None,
|
get: None,
|
||||||
@ -87,54 +106,67 @@ impl Router {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure a static map as `subroute`.
|
||||||
pub const fn subdirs(mut self, map: SubdirMap) -> Self {
|
pub const fn subdirs(mut self, map: SubdirMap) -> Self {
|
||||||
self.subroute = Some(SubRoute::Map(map));
|
self.subroute = Some(SubRoute::Map(map));
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure a `SubRoute::MatchAll` as `subroute`.
|
||||||
pub const fn match_all(mut self, param_name: &'static str, router: &'static Router) -> Self {
|
pub const fn match_all(mut self, param_name: &'static str, router: &'static Router) -> Self {
|
||||||
self.subroute = Some(SubRoute::MatchAll { router, param_name });
|
self.subroute = Some(SubRoute::MatchAll { router, param_name });
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure the GET method.
|
||||||
pub const fn get(mut self, m: &'static ApiMethod) -> Self {
|
pub const fn get(mut self, m: &'static ApiMethod) -> Self {
|
||||||
self.get = Some(m);
|
self.get = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure the PUT method.
|
||||||
pub const fn put(mut self, m: &'static ApiMethod) -> Self {
|
pub const fn put(mut self, m: &'static ApiMethod) -> Self {
|
||||||
self.put = Some(m);
|
self.put = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure the POST method.
|
||||||
pub const fn post(mut self, m: &'static ApiMethod) -> Self {
|
pub const fn post(mut self, m: &'static ApiMethod) -> Self {
|
||||||
self.post = Some(m);
|
self.post = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as post, buth async (fixme: expect Async)
|
/// Same as `post`, but expects an `AsyncHttp` handler.
|
||||||
pub const fn upload(mut self, m: &'static ApiMethod) -> Self {
|
pub const fn upload(mut self, m: &'static ApiMethod) -> Self {
|
||||||
|
// fixme: expect AsyncHttp
|
||||||
self.post = Some(m);
|
self.post = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as get, but async (fixme: expect Async)
|
/// Same as `get`, but expects an `AsyncHttp` handler.
|
||||||
pub const fn download(mut self, m: &'static ApiMethod) -> Self {
|
pub const fn download(mut self, m: &'static ApiMethod) -> Self {
|
||||||
|
// fixme: expect AsyncHttp
|
||||||
self.get = Some(m);
|
self.get = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Same as get, but async (fixme: expect Async)
|
/// Same as `get`, but expects an `AsyncHttp` handler.
|
||||||
pub const fn upgrade(mut self, m: &'static ApiMethod) -> Self {
|
pub const fn upgrade(mut self, m: &'static ApiMethod) -> Self {
|
||||||
|
// fixme: expect AsyncHttp
|
||||||
self.get = Some(m);
|
self.get = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Configure the DELETE method
|
||||||
pub const fn delete(mut self, m: &'static ApiMethod) -> Self {
|
pub const fn delete(mut self, m: &'static ApiMethod) -> Self {
|
||||||
self.delete = Some(m);
|
self.delete = Some(m);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Find the router for a specic path.
|
||||||
|
///
|
||||||
|
/// - `components`: Path, split into individual components.
|
||||||
|
/// - `uri_param`: Mutable hash map to store paramater from `MatchAll` router.
|
||||||
pub fn find_route(
|
pub fn find_route(
|
||||||
&self,
|
&self,
|
||||||
components: &[&str],
|
components: &[&str],
|
||||||
@ -165,6 +197,10 @@ impl Router {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Lookup the API method for a specific path.
|
||||||
|
/// - `components`: Path, split into individual components.
|
||||||
|
/// - `method`: The HTTP method.
|
||||||
|
/// - `uri_param`: Mutable hash map to store paramater from `MatchAll` router.
|
||||||
pub fn find_method(
|
pub fn find_method(
|
||||||
&self,
|
&self,
|
||||||
components: &[&str],
|
components: &[&str],
|
||||||
|
Loading…
Reference in New Issue
Block a user