#[allow(unused_imports)] use progenitor_client::{encode_path, RequestBuilderExt}; pub use progenitor_client::{ByteStream, Error, ResponseValue}; #[allow(unused_imports)] use reqwest::header::{HeaderMap, HeaderValue}; pub mod types { use serde::{Deserialize, Serialize}; #[allow(unused_imports)] use std::convert::TryFrom; #[derive(Clone, Debug, Deserialize, Serialize)] pub struct EnrolBody { pub host: String, pub key: String, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct GlobalJobsResult { pub summary: Vec, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct OutputRecord { pub msg: String, pub stream: String, pub time: chrono::DateTime, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct PingResult { pub host: String, pub ok: bool, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ReportFinishBody { pub duration_millis: i32, pub end_time: chrono::DateTime, pub exit_status: i32, pub id: ReportId, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ReportId { pub host: String, pub job: String, pub pid: u64, pub time: chrono::DateTime, pub uuid: String, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ReportOutputBody { pub id: ReportId, pub record: OutputRecord, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ReportResult { pub existed_already: bool, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ReportStartBody { pub id: ReportId, pub script: String, pub start_time: chrono::DateTime, } #[derive(Clone, Debug, Deserialize, Serialize)] pub struct ReportSummary { pub age_seconds: i32, pub duration_seconds: i32, pub host: String, pub job: String, pub status: i32, pub when: chrono::DateTime, } } #[derive(Clone, Debug)] ///Client for Keeper API /// ///report execution of cron jobs through a mechanism other than mail pub struct Client { pub(crate) baseurl: String, pub(crate) client: reqwest::Client, } impl Client { /// Create a new client. /// /// `baseurl` is the base URL provided to the internal /// `reqwest::Client`, and should include a scheme and hostname, /// as well as port and a path stem if applicable. pub fn new(baseurl: &str) -> Self { let dur = std::time::Duration::from_secs(15); let client = reqwest::ClientBuilder::new() .connect_timeout(dur) .timeout(dur) .build() .unwrap(); Self::new_with_client(baseurl, client) } /// Construct a new client with an existing `reqwest::Client`, /// allowing more control over its configuration. /// /// `baseurl` is the base URL provided to the internal /// `reqwest::Client`, and should include a scheme and hostname, /// as well as port and a path stem if applicable. pub fn new_with_client(baseurl: &str, client: reqwest::Client) -> Self { Self { baseurl: baseurl.to_string(), client, } } /// Return the base URL to which requests are made. pub fn baseurl(&self) -> &String { &self.baseurl } /// Return the internal `reqwest::Client` used to make requests. pub fn client(&self) -> &reqwest::Client { &self.client } } impl Client { ///Sends a `POST` request to `/enrol` /// ///Arguments: /// - `authorization`: Authorization header (bearer token) /// - `body` pub async fn enrol<'a>( &'a self, authorization: &'a str, body: &'a types::EnrolBody, ) -> Result, Error<()>> { let url = format!("{}/enrol", self.baseurl,); let mut header_map = HeaderMap::with_capacity(1usize); header_map.append("Authorization", HeaderValue::try_from(authorization)?); let request = self .client .post(url) .json(&body) .headers(header_map) .build()?; let result = self.client.execute(request).await; let response = result?; match response.status().as_u16() { 201u16 => Ok(ResponseValue::empty(response)), _ => Err(Error::UnexpectedResponse(response)), } } ///Sends a `GET` request to `/global/jobs` /// ///Arguments: /// - `authorization`: Authorization header (bearer token) pub async fn global_jobs<'a>( &'a self, authorization: &'a str, ) -> Result, Error<()>> { let url = format!("{}/global/jobs", self.baseurl,); let mut header_map = HeaderMap::with_capacity(1usize); header_map.append("Authorization", HeaderValue::try_from(authorization)?); let request = self.client.get(url).headers(header_map).build()?; let result = self.client.execute(request).await; let response = result?; match response.status().as_u16() { 201u16 => ResponseValue::from_response(response).await, _ => Err(Error::UnexpectedResponse(response)), } } ///Sends a `GET` request to `/ping` /// ///Arguments: /// - `authorization`: Authorization header (bearer token) pub async fn ping<'a>( &'a self, authorization: &'a str, ) -> Result, Error<()>> { let url = format!("{}/ping", self.baseurl,); let mut header_map = HeaderMap::with_capacity(1usize); header_map.append("Authorization", HeaderValue::try_from(authorization)?); let request = self.client.get(url).headers(header_map).build()?; let result = self.client.execute(request).await; let response = result?; match response.status().as_u16() { 201u16 => ResponseValue::from_response(response).await, _ => Err(Error::UnexpectedResponse(response)), } } ///Sends a `POST` request to `/report/finish` /// ///Arguments: /// - `authorization`: Authorization header (bearer token) /// - `body` pub async fn report_finish<'a>( &'a self, authorization: &'a str, body: &'a types::ReportFinishBody, ) -> Result, Error<()>> { let url = format!("{}/report/finish", self.baseurl,); let mut header_map = HeaderMap::with_capacity(1usize); header_map.append("Authorization", HeaderValue::try_from(authorization)?); let request = self .client .post(url) .json(&body) .headers(header_map) .build()?; let result = self.client.execute(request).await; let response = result?; match response.status().as_u16() { 201u16 => ResponseValue::from_response(response).await, _ => Err(Error::UnexpectedResponse(response)), } } ///Sends a `POST` request to `/report/output` /// ///Arguments: /// - `authorization`: Authorization header (bearer token) /// - `body` pub async fn report_output<'a>( &'a self, authorization: &'a str, body: &'a types::ReportOutputBody, ) -> Result, Error<()>> { let url = format!("{}/report/output", self.baseurl,); let mut header_map = HeaderMap::with_capacity(1usize); header_map.append("Authorization", HeaderValue::try_from(authorization)?); let request = self .client .post(url) .json(&body) .headers(header_map) .build()?; let result = self.client.execute(request).await; let response = result?; match response.status().as_u16() { 201u16 => ResponseValue::from_response(response).await, _ => Err(Error::UnexpectedResponse(response)), } } ///Sends a `POST` request to `/report/start` /// ///Arguments: /// - `authorization`: Authorization header (bearer token) /// - `body` pub async fn report_start<'a>( &'a self, authorization: &'a str, body: &'a types::ReportStartBody, ) -> Result, Error<()>> { let url = format!("{}/report/start", self.baseurl,); let mut header_map = HeaderMap::with_capacity(1usize); header_map.append("Authorization", HeaderValue::try_from(authorization)?); let request = self .client .post(url) .json(&body) .headers(header_map) .build()?; let result = self.client.execute(request).await; let response = result?; match response.status().as_u16() { 201u16 => ResponseValue::from_response(response).await, _ => Err(Error::UnexpectedResponse(response)), } } } pub mod prelude { pub use super::Client; }