You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
2.3 KiB

4 years ago
#[allow(non_upper_case_globals)]
4 years ago
use crate::{
config::{MASTER_PORT, MASTER_SERVER},
messaging::{AsMsg, BaseMessage},
models::*,
utils::opt_to_string,
UError, UResult,
};
use reqwest::{Client, RequestBuilder, Url};
use std::{net::Ipv4Addr, str::FromStr};
use u_api_proc_macro::api_route;
use uuid::Uuid;
4 years ago
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)
}
//
// get jobs for client
#[api_route("GET")]
fn get_agent_jobs(&self, url_param: Option<Uuid>) -> Vec<AssignedJob> {}
//
// send something to server
#[api_route("POST")]
fn report<M: AsMsg>(&self, payload: &M) {}
//#/////////#// Admin area //#////////#//
/// client listing
#[api_route("GET")]
fn get_agents(&self, url_param: Option<Uuid>) -> Vec<Agent> {}
//
// get all available jobs
#[api_route("GET")]
fn get_jobs(&self, url_param: Option<Uuid>) -> Vec<JobMeta> {}
//
// create and upload job
#[api_route("POST")]
fn upload_jobs(&self, payload: &Vec<JobMeta>) {}
//
// delete something
#[api_route("GET")]
fn del(&self, url_param: Option<Uuid>) -> String {}
//
// set jobs for client
#[api_route("POST")]
fn set_jobs(&self, url_param: Option<Uuid>, payload: &Vec<Uuid>) {}
}