83 lines
1.9 KiB
Rust
83 lines
1.9 KiB
Rust
use super::{Error, Result};
|
|
use rocket::fairing::AdHoc;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Serialize, Debug)]
|
|
pub struct FrontAsk {
|
|
pub command: String,
|
|
}
|
|
|
|
impl Default for FrontAsk {
|
|
fn default() -> Self {
|
|
FrontAsk {
|
|
command: "switch".to_string(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Serialize, Debug)]
|
|
pub struct SwitchCommand {
|
|
pub command: String,
|
|
pub member_name: String,
|
|
}
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
pub struct Status {
|
|
pub member_name: String,
|
|
pub started_at: Option<String>,
|
|
}
|
|
|
|
pub struct Client {
|
|
webhook_url: String,
|
|
}
|
|
|
|
impl Client {
|
|
pub fn new(webhook_url: String) -> Self {
|
|
Client {
|
|
webhook_url: webhook_url,
|
|
}
|
|
}
|
|
|
|
pub fn fairing() -> AdHoc {
|
|
AdHoc::on_attach("Switch Counter API", |rocket| {
|
|
let webhook_url = rocket.config().get_string("switchcounter_webhook").unwrap();
|
|
Ok(rocket.manage(Client::new(webhook_url)))
|
|
})
|
|
}
|
|
|
|
#[instrument(err, skip(self))]
|
|
pub fn status(&self) -> Result<Status> {
|
|
#[derive(Serialize, Debug)]
|
|
struct Wrapper {
|
|
pub webhook: FrontAsk,
|
|
}
|
|
|
|
let resp = ureq::post(&self.webhook_url)
|
|
.send_json(serde_json::to_value(Wrapper {
|
|
webhook: FrontAsk::default(),
|
|
})?)
|
|
.map_err(Error::UReq)?;
|
|
|
|
Ok(resp.into_json()?)
|
|
}
|
|
|
|
#[instrument(err, skip(self))]
|
|
pub fn switch(&self, member_name: String) -> Result<Status> {
|
|
#[derive(Serialize, Debug)]
|
|
struct Wrapper {
|
|
pub webhook: SwitchCommand,
|
|
}
|
|
|
|
let resp = ureq::post(&self.webhook_url)
|
|
.send_json(serde_json::to_value(Wrapper {
|
|
webhook: SwitchCommand {
|
|
command: "switch".to_string(),
|
|
member_name: member_name,
|
|
},
|
|
})?)
|
|
.map_err(Error::UReq)?;
|
|
|
|
Ok(resp.into_json()?)
|
|
}
|
|
}
|