refine pluralkit client

This commit is contained in:
Cadey Ratio 2020-11-19 10:20:58 -05:00
parent 3a428a155d
commit d20a822ca2
2 changed files with 58 additions and 4 deletions

View File

@ -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=<token> pluralkit_read <system_id>");
let token = env::var("PLURALKIT_TOKEN").expect("need PLURALKIT_TOKEN");
let pk = PluralKit::new(token);
let status = pk.status(system_id)?;
println!("{:#?}", status);
Ok(())
}

View File

@ -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<String>,
pub suffix: Option<String>,
}
#[derive(Deserialize, Debug)]
@ -21,7 +20,7 @@ pub struct Member {
pub birthday: Option<String>,
pub proxy_tags: Option<Vec<ProxyTag>>,
pub keep_proxy: bool,
pub created: NaiveDateTime,
pub created: String,
}
#[derive(Serialize, Debug)]
@ -29,12 +28,25 @@ pub struct SwitchRequest {
pub members: Vec<String>,
}
#[derive(Deserialize, Debug)]
pub struct SystemStatus {
pub timestamp: String,
pub members: Vec<Member>,
}
pub struct Client {
api_token: String,
member_mappings: BTreeMap<String, String>,
}
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<SystemStatus> {
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