|
|
|
@ -16,85 +16,6 @@ use std::{ |
|
|
|
|
str::FromStr |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
pub struct Paths; |
|
|
|
|
|
|
|
|
|
impl Paths { |
|
|
|
|
pub const LIST: &'static str = "list"; |
|
|
|
|
pub const NEW: &'static str = "new"; |
|
|
|
|
//pub const DEL: &'static str = "del";
|
|
|
|
|
pub const JOB_REQ: &'static str = "job_req"; |
|
|
|
|
pub const JOB_ANS: &'static str = "job_ans"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ClientHandler { |
|
|
|
|
base_url: Url, |
|
|
|
|
client: Client, |
|
|
|
|
password: Option<String> |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl ClientHandler { |
|
|
|
|
pub fn new(server: Option<String>) -> Self { |
|
|
|
|
let master_server = server |
|
|
|
|
.map(|s| Ipv4Addr::from_str(&s).unwrap()) |
|
|
|
|
.unwrap_or(MASTER_SERVER); |
|
|
|
|
Self { |
|
|
|
|
client: Client::new(), |
|
|
|
|
base_url: Url::parse( |
|
|
|
|
&format!("http://{}:{}", master_server, MASTER_PORT) |
|
|
|
|
).unwrap(), |
|
|
|
|
password: None |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn password(mut self, password: String) -> ClientHandler { |
|
|
|
|
self.password = Some(password); |
|
|
|
|
self |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn set_pwd(&self, rb: RequestBuilder) -> RequestBuilder { |
|
|
|
|
match &self.password { |
|
|
|
|
Some(p) => rb.bearer_auth(p), |
|
|
|
|
None => rb |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn build_get(&self, url: &str) -> RequestBuilder { |
|
|
|
|
let rb = self.client.get(self.base_url.join(url).unwrap()); |
|
|
|
|
self.set_pwd(rb) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn build_post(&self, url: &str) -> RequestBuilder { |
|
|
|
|
let rb = self.client.post(self.base_url.join(url).unwrap()); |
|
|
|
|
self.set_pwd(rb) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn init(&self, param: &ClientInfo) -> UResult<RawMsg> { |
|
|
|
|
let response: Response = self.build_post(Paths::NEW) |
|
|
|
|
.json::<Message<'_, ClientInfo>>(¶m.as_message()) |
|
|
|
|
.send() |
|
|
|
|
.await?; |
|
|
|
|
let msg = response |
|
|
|
|
.json::<Message<RawMsg>>() |
|
|
|
|
.await?; |
|
|
|
|
Ok(msg.into_item().into_owned()) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub async fn list(&self) -> UResult<Vec<ClientInfo>> { |
|
|
|
|
let response: Response = self.build_get(Paths::LIST) |
|
|
|
|
.send() |
|
|
|
|
.await?; |
|
|
|
|
let msg = response |
|
|
|
|
.json::<Message<Vec<ClientInfo>>>() |
|
|
|
|
.await?; |
|
|
|
|
Ok(msg.into_item().into_owned()) |
|
|
|
|
} |
|
|
|
|
/* |
|
|
|
|
pub async fn del(&self) -> UResult<()> { |
|
|
|
|
self.build_post(Paths::DEL).send().await?; |
|
|
|
|
Ok(()) |
|
|
|
|
}*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[macro_export] |
|
|
|
|
macro_rules! epilogue { |
|
|
|
@ -111,6 +32,7 @@ macro_rules! epilogue { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// build_handler![path = new, method = "post", param = ClientInfo, result = RawMsg]
|
|
|
|
|
// new syntax: build_handler!(POST new(ClientInfo) -> RawMsg)
|
|
|
|
|
// param and result must impl ToMsg
|
|
|
|
|
#[macro_export] |
|
|
|
|
macro_rules! build_handler { |
|
|
|
@ -164,4 +86,61 @@ macro_rules! build_handler { |
|
|
|
|
); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub struct Paths; |
|
|
|
|
|
|
|
|
|
impl Paths { |
|
|
|
|
pub const LIST: &'static str = "list"; |
|
|
|
|
pub const NEW: &'static str = "new"; |
|
|
|
|
//pub const DEL: &'static str = "del";
|
|
|
|
|
pub const JOB_REQ: &'static str = "job_req"; |
|
|
|
|
pub const JOB_ANS: &'static str = "job_ans"; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub struct ClientHandler { |
|
|
|
|
base_url: Url, |
|
|
|
|
client: Client, |
|
|
|
|
password: Option<String> |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl ClientHandler { |
|
|
|
|
pub fn new(server: Option<String>) -> Self { |
|
|
|
|
let master_server = server |
|
|
|
|
.map(|s| Ipv4Addr::from_str(&s).unwrap()) |
|
|
|
|
.unwrap_or(MASTER_SERVER); |
|
|
|
|
Self { |
|
|
|
|
client: Client::new(), |
|
|
|
|
base_url: Url::parse( |
|
|
|
|
&format!("http://{}:{}", master_server, MASTER_PORT) |
|
|
|
|
).unwrap(), |
|
|
|
|
password: None |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pub fn password(mut self, password: String) -> ClientHandler { |
|
|
|
|
self.password = Some(password); |
|
|
|
|
self |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn set_pwd(&self, rb: RequestBuilder) -> RequestBuilder { |
|
|
|
|
match &self.password { |
|
|
|
|
Some(p) => rb.bearer_auth(p), |
|
|
|
|
None => rb |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn build_get(&self, url: &str) -> RequestBuilder { |
|
|
|
|
let rb = self.client.get(self.base_url.join(url).unwrap()); |
|
|
|
|
self.set_pwd(rb) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn build_post(&self, url: &str) -> RequestBuilder { |
|
|
|
|
let rb = self.client.post(self.base_url.join(url).unwrap()); |
|
|
|
|
self.set_pwd(rb) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
build_handler!(path = init, method = "post", param = ClientInfo, result = RawMsg); |
|
|
|
|
build_handler!(path = ls, method = "get", result = Vec<ClientInfo>); |
|
|
|
|
build_handler!(path = del, method = "post"); |
|
|
|
|