start working on bar pinger and traewelling client
This commit is contained in:
parent
867514362a
commit
718fe00b46
13 changed files with 1062 additions and 48 deletions
56
programs/traveldings/src/traewelling.rs
Normal file
56
programs/traveldings/src/traewelling.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use std::{fmt, fs};
|
||||
|
||||
use model::{Container, Status};
|
||||
use reqwest::{
|
||||
header::{self, HeaderMap},
|
||||
Client, ClientBuilder,
|
||||
};
|
||||
|
||||
const KEY_PATH: &str = "/home/jade/Docs/traveldings-key";
|
||||
const USER_AGENT: &str = "s10e/traveldings";
|
||||
const TRAEWELLING_API_URL: &str = "https://traewelling.de/api/v1";
|
||||
|
||||
pub struct TraewellingClient {
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl TraewellingClient {
|
||||
pub fn new() -> anyhow::Result<Self> {
|
||||
let mut headers = HeaderMap::new();
|
||||
let token = fs::read_to_string(KEY_PATH)?;
|
||||
let key = header::HeaderValue::from_str(&format!("Bearer {token}"))?;
|
||||
println!("meow");
|
||||
headers.insert("Authorization", key);
|
||||
headers.insert(
|
||||
header::ACCEPT,
|
||||
header::HeaderValue::from_static("application/json"),
|
||||
);
|
||||
Ok(Self {
|
||||
client: ClientBuilder::new()
|
||||
.user_agent("s10e/traveldings")
|
||||
.default_headers(headers)
|
||||
.build()?,
|
||||
})
|
||||
}
|
||||
|
||||
pub async fn get_active_checkin(&self) -> anyhow::Result<Status> {
|
||||
let txt = self
|
||||
.client
|
||||
.get(Self::fmt_url("user/statuses/active"))
|
||||
.send()
|
||||
.await?
|
||||
.text()
|
||||
.await?;
|
||||
|
||||
println!("{txt}");
|
||||
|
||||
let res: Container<Status> = serde_json::de::from_str(&txt)?;
|
||||
Ok(res.data)
|
||||
}
|
||||
|
||||
fn fmt_url(path: impl fmt::Display) -> String {
|
||||
format!("{TRAEWELLING_API_URL}/{path}")
|
||||
}
|
||||
}
|
||||
|
||||
pub mod model;
|
Loading…
Add table
Add a link
Reference in a new issue