|
|
@ -2,7 +2,8 @@ use crate::{ |
|
|
|
MASTER_SERVER, |
|
|
|
MASTER_SERVER, |
|
|
|
MASTER_PORT, |
|
|
|
MASTER_PORT, |
|
|
|
contracts::*, |
|
|
|
contracts::*, |
|
|
|
UResult |
|
|
|
UResult, |
|
|
|
|
|
|
|
UError |
|
|
|
}; |
|
|
|
}; |
|
|
|
use reqwest::{ |
|
|
|
use reqwest::{ |
|
|
|
Client, |
|
|
|
Client, |
|
|
@ -12,8 +13,7 @@ use reqwest::{ |
|
|
|
}; |
|
|
|
}; |
|
|
|
use std::{ |
|
|
|
use std::{ |
|
|
|
net::Ipv4Addr, |
|
|
|
net::Ipv4Addr, |
|
|
|
str::FromStr, |
|
|
|
str::FromStr |
|
|
|
any::Any |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
pub struct Paths; |
|
|
|
pub struct Paths; |
|
|
@ -71,7 +71,7 @@ impl ClientHandler { |
|
|
|
|
|
|
|
|
|
|
|
pub async fn init(&self, param: &ClientInfo) -> UResult<RawMsg> { |
|
|
|
pub async fn init(&self, param: &ClientInfo) -> UResult<RawMsg> { |
|
|
|
let response: Response = self.build_post(Paths::NEW) |
|
|
|
let response: Response = self.build_post(Paths::NEW) |
|
|
|
.json(¶m.as_message()) |
|
|
|
.json::<Message<'_, ClientInfo>>(¶m.as_message()) |
|
|
|
.send() |
|
|
|
.send() |
|
|
|
.await?; |
|
|
|
.await?; |
|
|
|
let msg = response |
|
|
|
let msg = response |
|
|
@ -96,33 +96,42 @@ impl ClientHandler { |
|
|
|
}*/ |
|
|
|
}*/ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
|
|
|
macro_rules! epilogue { |
|
|
|
|
|
|
|
( () ) => ( |_| async Ok(()) ); |
|
|
|
|
|
|
|
( $result:ty ) => { |
|
|
|
|
|
|
|
|response: Response| async { |
|
|
|
|
|
|
|
response |
|
|
|
|
|
|
|
.json::<Message<$result>>() |
|
|
|
|
|
|
|
.await |
|
|
|
|
|
|
|
.map(|msg| msg.into_item().into_owned()) |
|
|
|
|
|
|
|
.map_err(|e| UError::from(e)) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg]
|
|
|
|
// build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg]
|
|
|
|
// param and result must impl ToMsg
|
|
|
|
// param and result must impl ToMsg
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
macro_rules! build_handler { |
|
|
|
macro_rules! build_handler { |
|
|
|
( |
|
|
|
( |
|
|
|
path = $path:tt, |
|
|
|
path = $path:tt, |
|
|
|
method = $method:literal, |
|
|
|
method = $method:literal, |
|
|
|
param = $param:ty, |
|
|
|
param = $($param:ty)?, |
|
|
|
result = $result:ty |
|
|
|
result = $result:ty |
|
|
|
) => { |
|
|
|
) => { |
|
|
|
impl ClientHandler { |
|
|
|
impl ClientHandler { |
|
|
|
pub async fn $path(&self, param: &$param) -> UResult<$result> { |
|
|
|
pub async fn $path(&self $(, param: &$param)?) -> UResult<$result> { |
|
|
|
let builder = match $method { |
|
|
|
let builder = match $method { |
|
|
|
"post" => ClientHandler::build_post, |
|
|
|
"post" => ClientHandler::build_post, |
|
|
|
"get" => ClientHandler::build_get, |
|
|
|
"get" => ClientHandler::build_get, |
|
|
|
_ => panic!("Method '{}' is not allowed", $method) |
|
|
|
_ => panic!("Method '{}' is not allowed", $method) |
|
|
|
}; |
|
|
|
}; |
|
|
|
let base_request = builder(self, stringify!($path)); |
|
|
|
let mut request = builder(self, stringify!($path)); |
|
|
|
let request = if let Some(p) = (param as &dyn Any).downcast_ref::<EmptyMsg>() { |
|
|
|
request = request |
|
|
|
base_request |
|
|
|
$(.json::<Message<'_, $param>>(¶m.as_message()))?; |
|
|
|
} else { |
|
|
|
|
|
|
|
base_request.json(¶m.as_message()) |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
let response = request.send().await?; |
|
|
|
let response = request.send().await?; |
|
|
|
let msg = response |
|
|
|
($crate::epilogue!($result)(response)).await |
|
|
|
.json::<Message<$result>>() |
|
|
|
|
|
|
|
.await?; |
|
|
|
|
|
|
|
Ok(msg.into_item().into_owned()) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -135,7 +144,7 @@ macro_rules! build_handler { |
|
|
|
path = $path:tt, |
|
|
|
path = $path:tt, |
|
|
|
method = $method:literal |
|
|
|
method = $method:literal |
|
|
|
) => ( |
|
|
|
) => ( |
|
|
|
build_handler!(path = $path, method = $method, param = EmptyMsg, result = EmptyMsg); |
|
|
|
build_handler!(path = $path, method = $method, param = , result = ()); |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
( |
|
|
|
( |
|
|
@ -143,7 +152,7 @@ macro_rules! build_handler { |
|
|
|
method = $method:literal, |
|
|
|
method = $method:literal, |
|
|
|
param = $param:ty |
|
|
|
param = $param:ty |
|
|
|
) => ( |
|
|
|
) => ( |
|
|
|
build_handler!(path = $path, method = $method, param = $param, result = EmptyMsg); |
|
|
|
build_handler!(path = $path, method = $method, param = $param, result = ()); |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
( |
|
|
|
( |
|
|
@ -151,7 +160,7 @@ macro_rules! build_handler { |
|
|
|
method = $method:literal, |
|
|
|
method = $method:literal, |
|
|
|
result = $result:ty |
|
|
|
result = $result:ty |
|
|
|
) => ( |
|
|
|
) => ( |
|
|
|
build_handler!(path = $path, method = $method, param = EmptyMsg, result = $result); |
|
|
|
build_handler!(path = $path, method = $method, param = , result = $result); |
|
|
|
); |
|
|
|
); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|