chiark / gitweb /
Move auth file handling out into a module.
authorSimon Tatham <anakin@pobox.com>
Sat, 23 Dec 2023 12:14:50 +0000 (12:14 +0000)
committerSimon Tatham <anakin@pobox.com>
Sat, 23 Dec 2023 12:14:50 +0000 (12:14 +0000)
It ought to be fallible, though.

src/auth.rs [new file with mode: 0644]
src/lib.rs
src/main.rs

diff --git a/src/auth.rs b/src/auth.rs
new file mode 100644 (file)
index 0000000..18e7406
--- /dev/null
@@ -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
+    }
+}
index cd408564ea089141dd830d4524e027b1064b33d7..65a863493f4438f559b4228b1703ca7681795a55 100644 (file)
@@ -1 +1,2 @@
 pub mod types;
+pub mod auth;
index fd695d6c80a57cc70bbb913ae98f3c74a8dfc5e0..f26d7007b01c8e4dff95b6d4b7cd53e28101022d 100644 (file)
@@ -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;