refine pluralkit client
This commit is contained in:
parent
3a428a155d
commit
d20a822ca2
|
@ -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(())
|
||||||
|
}
|
|
@ -1,13 +1,12 @@
|
||||||
use super::{Error, Result};
|
use super::{Error, Result};
|
||||||
use chrono::NaiveDateTime;
|
|
||||||
use rocket::fairing::AdHoc;
|
use rocket::fairing::AdHoc;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::collections::BTreeMap;
|
use std::collections::BTreeMap;
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
pub struct ProxyTag {
|
pub struct ProxyTag {
|
||||||
pub prefix: String,
|
pub prefix: Option<String>,
|
||||||
pub suffix: String,
|
pub suffix: Option<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Debug)]
|
#[derive(Deserialize, Debug)]
|
||||||
|
@ -21,7 +20,7 @@ pub struct Member {
|
||||||
pub birthday: Option<String>,
|
pub birthday: Option<String>,
|
||||||
pub proxy_tags: Option<Vec<ProxyTag>>,
|
pub proxy_tags: Option<Vec<ProxyTag>>,
|
||||||
pub keep_proxy: bool,
|
pub keep_proxy: bool,
|
||||||
pub created: NaiveDateTime,
|
pub created: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Debug)]
|
#[derive(Serialize, Debug)]
|
||||||
|
@ -29,12 +28,25 @@ pub struct SwitchRequest {
|
||||||
pub members: Vec<String>,
|
pub members: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Debug)]
|
||||||
|
pub struct SystemStatus {
|
||||||
|
pub timestamp: String,
|
||||||
|
pub members: Vec<Member>,
|
||||||
|
}
|
||||||
|
|
||||||
pub struct Client {
|
pub struct Client {
|
||||||
api_token: String,
|
api_token: String,
|
||||||
member_mappings: BTreeMap<String, String>,
|
member_mappings: BTreeMap<String, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Client {
|
impl Client {
|
||||||
|
pub fn new(token: String) -> Client {
|
||||||
|
Client {
|
||||||
|
api_token: token,
|
||||||
|
member_mappings: BTreeMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn fairing() -> AdHoc {
|
pub fn fairing() -> AdHoc {
|
||||||
AdHoc::on_attach("PluralKit client", |rocket| {
|
AdHoc::on_attach("PluralKit client", |rocket| {
|
||||||
let cfg = rocket.config();
|
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))]
|
#[instrument(err, skip(self))]
|
||||||
pub fn switch(&self, member_name: String) -> Result {
|
pub fn switch(&self, member_name: String) -> Result {
|
||||||
let member = self
|
let member = self
|
||||||
|
|
Loading…
Reference in New Issue