From: Simon Tatham Date: Sat, 23 Dec 2023 12:14:50 +0000 (+0000) Subject: Move auth file handling out into a module. X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ian/git?a=commitdiff_plain;h=31a198e89ce15d9687fb3a63d0c4bf27a36ee1a1;p=mastodonochrome.git Move auth file handling out into a module. It ought to be fallible, though. --- diff --git a/src/auth.rs b/src/auth.rs new file mode 100644 index 0000000..18e7406 --- /dev/null +++ b/src/auth.rs @@ -0,0 +1,23 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Serialize, Deserialize, Debug)] +pub struct AuthConfig { + pub account_id: String, + pub username: String, + pub instance_url: String, + pub instance_domain: String, + pub client_id: String, + pub client_secret: String, + pub user_token: String, +} + +impl AuthConfig { + pub fn load() -> Self { + let xdg_dirs = xdg::BaseDirectories::with_prefix("mastodonochrome") + .unwrap(); + let authfile = xdg_dirs.get_config_file("auth"); + let auth = std::fs::read_to_string(authfile).unwrap(); + let auth: Self = serde_json::from_str(&auth).unwrap(); + auth + } +} diff --git a/src/lib.rs b/src/lib.rs index cd40856..65a8634 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,2 @@ pub mod types; +pub mod auth; diff --git a/src/main.rs b/src/main.rs index fd695d6..f26d700 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,18 +1,13 @@ // use mastodonochrome::types::*; +use mastodonochrome::auth::AuthConfig; use std::io::Read; fn main() { - let xdg_dirs = xdg::BaseDirectories::with_prefix("mastodonochrome") - .unwrap(); - let authfile = xdg_dirs.get_config_file("auth"); - let auth = std::fs::read_to_string(authfile).unwrap(); - let auth: serde_json::Value = serde_json::from_str(&auth).unwrap(); - let auth = &auth["user_token"].as_str().unwrap(); + let auth = AuthConfig::load(); let client = reqwest::blocking::Client::new(); - let mut req = client.get( - "https://hachyderm.io/api/v1/streaming/user") - .bearer_auth(auth) + let mut req = client.get(auth.instance_url + "/api/v1/streaming/user") + .bearer_auth(auth.user_token) .send().unwrap(); const BUFSIZE: usize = 4096;