summary refs log tree commit diff
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-08-25 20:25:33 +0300
committerVika <vika@fireburn.ru>2024-08-25 20:25:33 +0300
commit7f3c70998e6d4487a9d567c84aa2d0efac203fc7 (patch)
treebbd4a5962545acb54aeca0992e77bb68e6660af0
parentbc82c6a97e0b54876e14dc9a90b44da6a76bac1b (diff)
downloadbowl-7f3c70998e6d4487a9d567c84aa2d0efac203fc7.tar.zst
Use a provided SoupSession for Micropub
-rw-r--r--src/lib.rs10
-rw-r--r--src/micropub.rs12
2 files changed, 12 insertions, 10 deletions
diff --git a/src/lib.rs b/src/lib.rs
index 52a18ef..23b7be7 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -44,7 +44,7 @@ pub struct App {
 }
 
 impl App {
-    async fn get_login_state(schema: &libsecret::Schema) -> Result<Option<micropub::Client>, glib::Error> {
+    async fn get_login_state(schema: &libsecret::Schema, http: soup::Session) -> Result<Option<micropub::Client>, glib::Error> {
         let mut retrievables = libsecret::password_search_future(Some(schema), {
             let mut attrs = std::collections::HashMap::default();
             attrs.insert(crate::secrets::TOKEN_KIND, crate::secrets::ACCESS_TOKEN);
@@ -92,6 +92,7 @@ impl App {
                     };
 
                 let micropub = crate::micropub::Client::new(
+                    http.clone(),
                     micropub_uri,
                     retrievable.retrieve_secret_future().await
                         // SAFETY: see above
@@ -202,11 +203,14 @@ impl AsyncComponent for App {
         sender: AsyncComponentSender<Self>,
     ) -> AsyncComponentParts<Self> {
         let secret_schema = crate::secrets::get_schema();
-        let state = App::get_login_state(&secret_schema).await.unwrap();
         let http = soup::Session::builder()
             .user_agent(concat!(env!("CARGO_PKG_NAME"),"/",env!("CARGO_PKG_VERSION")," "))
             .build();
 
+        let state = App::get_login_state(
+            &secret_schema, http.clone()
+        ).await.unwrap();
+
         let model = App {
             submit_busy_guard: None,
 
@@ -284,7 +288,7 @@ impl AsyncComponent for App {
                 }
 
                 self.micropub = Some(crate::micropub::Client::new(
-                    data.micropub.clone(), data.access_token.clone()
+                    self.http.clone(), data.micropub.clone(), data.access_token.clone()
                 ));
             },
             Input::SubmitButtonPressed => {
diff --git a/src/micropub.rs b/src/micropub.rs
index e855459..dbd8f9a 100644
--- a/src/micropub.rs
+++ b/src/micropub.rs
@@ -6,7 +6,7 @@ pub struct Client {
     micropub: String,
     access_token: String,
 
-    //http: soup::Session,
+    http: soup::Session,
 }
 
 #[derive(Debug, thiserror::Error)]
@@ -22,12 +22,12 @@ pub enum Error {
 }
 
 impl Client {
-    pub fn new(uri: glib::Uri, token: String) -> Self {
+    pub fn new(http: soup::Session, uri: glib::Uri, token: String) -> Self {
         Self {
             micropub: uri.to_string(),
             access_token: token,
 
-            //http: soup::Session::new()
+            http,
         }
     }
 
@@ -42,8 +42,7 @@ impl Client {
         // TODO: create a SoupAuth subclass that allows pasting in a token
         headers.append("Authorization", &format!("Bearer {}", self.access_token));
 
-        let http = soup::Session::new();
-        let body = http.send_and_read_future(&exch, glib::Priority::DEFAULT).await?;
+        let body = self.http.send_and_read_future(&exch, glib::Priority::DEFAULT).await?;
 
         Ok(serde_json::from_slice(&body)?)
     }
@@ -58,8 +57,7 @@ impl Client {
             Some(&glib::Bytes::from_owned(serde_json::to_vec(&post).unwrap()))
         );
 
-        let http = soup::Session::new();
-        let body = http.send_and_read_future(&exch, glib::Priority::DEFAULT).await?;
+        let body = self.http.send_and_read_future(&exch, glib::Priority::DEFAULT).await?;
 
         match exch.status() {
             soup::Status::Created | soup::Status::Accepted => {