From d20a822ca23ae797317ae6da8e6762e18248ecf1 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Thu, 19 Nov 2020 10:20:58 -0500 Subject: [PATCH] refine pluralkit client --- backend/src/bin/pluralkit_read.rs | 20 +++++++++++++++ backend/src/web/pluralkit.rs | 42 ++++++++++++++++++++++++++++--- 2 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 backend/src/bin/pluralkit_read.rs diff --git a/backend/src/bin/pluralkit_read.rs b/backend/src/bin/pluralkit_read.rs new file mode 100644 index 0000000..25ffb2e --- /dev/null +++ b/backend/src/bin/pluralkit_read.rs @@ -0,0 +1,20 @@ +use color_eyre::eyre::Result; +use mi::web::PluralKit; +use std::env; + +fn main() -> Result<()> { + color_eyre::install()?; + tracing_subscriber::fmt::init(); + + let system_id = env::args() + .skip(1) + .next() + .expect("usage: PLURALKIT_TOKEN= pluralkit_read "); + let token = env::var("PLURALKIT_TOKEN").expect("need PLURALKIT_TOKEN"); + let pk = PluralKit::new(token); + let status = pk.status(system_id)?; + + println!("{:#?}", status); + + Ok(()) +} diff --git a/backend/src/web/pluralkit.rs b/backend/src/web/pluralkit.rs index 77e2944..10c8d6b 100644 --- a/backend/src/web/pluralkit.rs +++ b/backend/src/web/pluralkit.rs @@ -1,13 +1,12 @@ use super::{Error, Result}; -use chrono::NaiveDateTime; use rocket::fairing::AdHoc; use serde::{Deserialize, Serialize}; use std::collections::BTreeMap; #[derive(Deserialize, Debug)] pub struct ProxyTag { - pub prefix: String, - pub suffix: String, + pub prefix: Option, + pub suffix: Option, } #[derive(Deserialize, Debug)] @@ -21,7 +20,7 @@ pub struct Member { pub birthday: Option, pub proxy_tags: Option>, pub keep_proxy: bool, - pub created: NaiveDateTime, + pub created: String, } #[derive(Serialize, Debug)] @@ -29,12 +28,25 @@ pub struct SwitchRequest { pub members: Vec, } +#[derive(Deserialize, Debug)] +pub struct SystemStatus { + pub timestamp: String, + pub members: Vec, +} + pub struct Client { api_token: String, member_mappings: BTreeMap, } impl Client { + pub fn new(token: String) -> Client { + Client { + api_token: token, + member_mappings: BTreeMap::new(), + } + } + pub fn fairing() -> AdHoc { AdHoc::on_attach("PluralKit client", |rocket| { let cfg = rocket.config(); @@ -55,6 +67,28 @@ impl Client { }) } + #[instrument(err, skip(self))] + pub fn status(&self, system_id: String) -> Result { + let resp = ureq::get(&format!( + "https://api.pluralkit.me/v1/s/{}/fronters", + system_id + )) + .set("Authorization", &self.api_token) + .set("User-Agent", crate::APPLICATION_NAME) + .call(); + + debug!("headers: {:?}", resp.headers_names()); + + if resp.ok() { + Ok(resp.into_json_deserialize()?) + } else { + Err(match resp.synthetic_error() { + Some(why) => Error::UReq(why.to_string()), + None => Error::HttpStatus(resp.status()), + }) + } + } + #[instrument(err, skip(self))] pub fn switch(&self, member_name: String) -> Result { let member = self