nix-configs/programs/traveldings/src/traewelling.rs

57 lines
1.5 KiB
Rust
Raw Normal View History

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;