|
|
|
@ -17,6 +17,7 @@ use std::{ |
|
|
|
|
net::Ipv4Addr, |
|
|
|
|
str::FromStr |
|
|
|
|
}; |
|
|
|
|
use uuid::Uuid; |
|
|
|
|
|
|
|
|
|
pub struct Paths; |
|
|
|
|
|
|
|
|
@ -34,20 +35,64 @@ macro_rules! get_result { |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
macro_rules! build_url_by_method { |
|
|
|
|
( |
|
|
|
|
POST $path:tt, |
|
|
|
|
pname = $($param_name:literal)?, |
|
|
|
|
ptype = $($param_type:ty)?, |
|
|
|
|
urlparam = $($url_param:ty)? |
|
|
|
|
) => { |
|
|
|
|
|instance: &ClientHandler $(, param: &$param_type)?| { |
|
|
|
|
let request = ClientHandler::build_post( |
|
|
|
|
instance, |
|
|
|
|
&format!("{}/{}", |
|
|
|
|
stringify!($path), |
|
|
|
|
String::new() $(+ stringify!($url_param))? |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
request |
|
|
|
|
$( .json::<Message<'_, $param_type>>(¶m.as_message()) )? |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
( |
|
|
|
|
GET $path:tt, |
|
|
|
|
pname = $($param_name:literal)?, |
|
|
|
|
ptype = $($param_type:ty)?, |
|
|
|
|
urlparam = $($url_param:ty)? |
|
|
|
|
) => { |
|
|
|
|
|instance: &ClientHandler $(, param: &$param_type)?| { |
|
|
|
|
let request = ClientHandler::build_get( |
|
|
|
|
instance, |
|
|
|
|
&format!("{}/{}", |
|
|
|
|
stringify!($path), |
|
|
|
|
String::new() $(+ stringify!($url_param))? |
|
|
|
|
) |
|
|
|
|
); |
|
|
|
|
request |
|
|
|
|
$( .query(&[(stringify!($param_name), param.to_string())]) )? |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// param and result must impl ToMsg
|
|
|
|
|
#[macro_export] |
|
|
|
|
macro_rules! build_handler { |
|
|
|
|
( $method:tt $path:tt($($param:ty)?) -> $result:ty ) => { |
|
|
|
|
( |
|
|
|
|
$method:tt |
|
|
|
|
$path:tt( |
|
|
|
|
$($param_name:literal:)? |
|
|
|
|
$($param_type:ty)? |
|
|
|
|
$(; $url_param:ty)? |
|
|
|
|
) -> $result:ty ) => { |
|
|
|
|
impl ClientHandler { |
|
|
|
|
pub async fn $path(&self $(, param: &$param)?) -> UResult<$result> { |
|
|
|
|
let builder = match stringify!($method) { |
|
|
|
|
"POST" => ClientHandler::build_post, |
|
|
|
|
"GET" => ClientHandler::build_get, |
|
|
|
|
_ => panic!("Method '{}' is not allowed", stringify!($method)) |
|
|
|
|
}; |
|
|
|
|
let mut request = builder(self, stringify!($path)); |
|
|
|
|
request = request |
|
|
|
|
$( .json::<Message<'_, $param>>(¶m.as_message()) )? ; |
|
|
|
|
pub async fn $path( |
|
|
|
|
&self $(, param: &$param_type)? $(, url_param: &$url_param)? |
|
|
|
|
) -> UResult<$result> { |
|
|
|
|
let request = $crate::build_url_by_method!( |
|
|
|
|
$method $path, |
|
|
|
|
pname = $($param_name)?, ptype = $($param_type)?, urlparam = $($url_param)? |
|
|
|
|
)(self $(, param as &$param_type)? ); |
|
|
|
|
let response = request.send().await?; |
|
|
|
|
($crate::get_result!($result)(response)).await |
|
|
|
|
} |
|
|
|
@ -103,15 +148,21 @@ impl ClientHandler { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// method basic_path(json/query param; additional_url_param) -> return value
|
|
|
|
|
// A - admin only
|
|
|
|
|
// client listing (A)
|
|
|
|
|
build_handler!(GET ls() -> CollectionWrapper<Vec<ClientInfo>>); |
|
|
|
|
build_handler!(GET ls() -> IterWrap<Vec<ClientInfo>>); |
|
|
|
|
// get jobs for client himself (A: id=client_id)
|
|
|
|
|
build_handler!(GET get_jobs() -> CollectionWrapper<JobMetaStorage>); |
|
|
|
|
build_handler!(GET get_jobs() -> IterWrap<JobMetaStorage>); |
|
|
|
|
// add client to server's db
|
|
|
|
|
build_handler!(POST init(ClientInfo) -> RawMsg); |
|
|
|
|
// ???
|
|
|
|
|
build_handler!(POST del() -> ()); |
|
|
|
|
// set jobs for client (A)
|
|
|
|
|
build_handler!(POST set_jobs(CollectionWrapper<JobMetaStorage>) -> ()); |
|
|
|
|
// get_results (A): user_id, job_id
|
|
|
|
|
// POST /set_jobs/Uuid json: JobMetaStorage
|
|
|
|
|
build_handler!(POST set_jobs(IterWrap<JobMetaStorage>; Uuid) -> ()); |
|
|
|
|
// get results (A)
|
|
|
|
|
// GET /get_job_results?job_id=Uuid
|
|
|
|
|
build_handler!(GET get_job_results("job_id":Uuid) -> IterWrap<Vec<JobResult>>); |
|
|
|
|
// report job result
|
|
|
|
|
build_handler!(POST report(IterWrap<Vec<JobResult>>) -> ()); |