continue working on traveldings (get live checkin thing working maybe??)

This commit is contained in:
Schrottkatze 2024-09-10 21:37:18 +02:00
parent 258d4639d7
commit bd3674accf
Signed by: schrottkatze
SSH key fingerprint: SHA256:hXb3t1vINBFCiDCmhRABHX5ocdbLiKyCdKI4HK2Rbbc
5 changed files with 274 additions and 30 deletions

View file

@ -3,7 +3,7 @@ use std::{fmt, fs};
use model::{Container, Status};
use reqwest::{
header::{self, HeaderMap},
Client, ClientBuilder,
Client, ClientBuilder, StatusCode,
};
const KEY_PATH: &str = "/home/jade/Docs/traveldings-key";
@ -19,7 +19,6 @@ impl TraewellingClient {
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,
@ -33,16 +32,17 @@ impl TraewellingClient {
})
}
pub async fn get_active_checkin(&self) -> anyhow::Result<Status> {
let txt = self
pub async fn get_active_checkin(&self) -> Result<Status, RequestErr> {
let res = self
.client
.get(Self::fmt_url("user/statuses/active"))
.send()
.await?
.text()
.await?;
if res.status() != StatusCode::OK {
return Err(RequestErr::WithStatus(res.status()));
}
println!("{txt}");
let txt = res.text().await?;
let res: Container<Status> = serde_json::de::from_str(&txt)?;
Ok(res.data)
@ -53,4 +53,35 @@ impl TraewellingClient {
}
}
#[derive(thiserror::Error, Debug, PartialEq, Eq)]
pub enum RequestErr {
#[error("Couldn't deserialize the json :(")]
DeserializationError,
#[error("an error related to connect happened!!")]
RelatedToConnect,
#[error("error haz status: {0}")]
WithStatus(StatusCode),
#[error("fuck if i know what went wrong :333 am silly ")]
Other,
}
impl From<serde_json::Error> for RequestErr {
fn from(value: serde_json::Error) -> Self {
eprintln!("serde error: {value:?}");
Self::DeserializationError
}
}
impl From<reqwest::Error> for RequestErr {
fn from(value: reqwest::Error) -> Self {
if let Some(status) = value.status() {
Self::WithStatus(status)
} else if value.is_connect() {
Self::RelatedToConnect
} else {
Self::Other
}
}
}
pub mod model;