chiark / gitweb /
Make execute_and_log_request a method of Client.
authorSimon Tatham <anakin@pobox.com>
Sun, 4 Feb 2024 14:57:10 +0000 (14:57 +0000)
committerSimon Tatham <anakin@pobox.com>
Sun, 4 Feb 2024 14:58:35 +0000 (14:58 +0000)
All the calls to it happen inside methods of Client, so there's no
reason this is difficult. The only reason it was a separate function
in the first place was so that the old version of login.rs could use
it, which did login outside the full TUI via a separate network
interaction module. But now all the functions of that module have been
moved into Client, execute_and_log_request is fine as it is.

It also doesn't need to be pub any more.

src/client.rs

index 8de75dcb221b5ef7b4434862d1a0ea25210b64ce..fed32029e941d789ad5c4b8083035f354f3dd88f 100644 (file)
@@ -517,35 +517,6 @@ impl TransactionLogEntry {
     }
 }
 
-pub fn execute_and_log_request(
-    client: &reqwest::blocking::Client,
-    req: reqwest::blocking::Request,
-) -> Result<(reqwest::blocking::Response, TransactionLogEntry), ClientError> {
-    let method = req.method().clone();
-    let url = req.url().clone();
-    let mut req_headers = Vec::new();
-    for h in req.headers() {
-        req_headers.push(TransactionLogEntry::translate_header(h));
-    }
-
-    let rsp = client.execute(req)?;
-    let status = rsp.status();
-    let mut rsp_headers = Vec::new();
-    for h in rsp.headers() {
-        rsp_headers.push(TransactionLogEntry::translate_header(h));
-    }
-
-    let log = TransactionLogEntry {
-        method,
-        url,
-        req_headers,
-        status,
-        rsp_headers,
-    };
-
-    Ok((rsp, log))
-}
-
 const REDIRECT_MAGIC_STRING: &str = "urn:ietf:wg:oauth:2.0:oob";
 const WEBSITE: &str =
     "https://www.chiark.greenend.org.uk/~sgtatham/mastodonochrome/";
@@ -667,6 +638,36 @@ impl Client {
         req.build(&base_url, client, self.auth.user_token.as_deref())
     }
 
+    fn execute_and_log_request(
+        &self,
+        req: reqwest::blocking::Request,
+    ) -> Result<(reqwest::blocking::Response, TransactionLogEntry), ClientError>
+    {
+        let method = req.method().clone();
+        let url = req.url().clone();
+        let mut req_headers = Vec::new();
+        for h in req.headers() {
+            req_headers.push(TransactionLogEntry::translate_header(h));
+        }
+
+        let rsp = self.client.execute(req)?;
+        let status = rsp.status();
+        let mut rsp_headers = Vec::new();
+        for h in rsp.headers() {
+            rsp_headers.push(TransactionLogEntry::translate_header(h));
+        }
+
+        let log = TransactionLogEntry {
+            method,
+            url,
+            req_headers,
+            status,
+            rsp_headers,
+        };
+
+        Ok((rsp, log))
+    }
+
     /// Sends and logs, but doesn't do any checking on the response
     ///
     /// You must call `.is_success()` (or equivalent) on the response
@@ -676,7 +677,7 @@ impl Client {
         req: Req,
     ) -> Result<(String, reqwest::blocking::Response), ClientError> {
         let (urlstr, req) = self.api_request_cl(&self.client, req)?;
-        let (rsp, log) = execute_and_log_request(&self.client, req.build()?)?;
+        let (rsp, log) = self.execute_and_log_request(req.build()?)?;
         self.consume_transaction_log(log);
         Ok((urlstr, rsp))
     }
@@ -1213,7 +1214,7 @@ impl Client {
 
         let client = reqwest_client()?;
         let (url, mut req) = self.api_request_cl(&client, req)?;
-        let (rsp, log) = execute_and_log_request(&self.client, req.build()?)?;
+        let (rsp, log) = self.execute_and_log_request(req.build()?)?;
         self.consume_transaction_log(log);
         let mut rsp = rsp;
         if rsp.status().is_redirection() {
@@ -1286,7 +1287,7 @@ impl Client {
                         req = req.bearer_auth(token);
                     }
                     let (newrsp, log) =
-                        execute_and_log_request(&self.client, req.build()?)?;
+                        self.execute_and_log_request(req.build()?)?;
                     self.consume_transaction_log(log);
                     rsp = newrsp;
                 }
@@ -1700,7 +1701,7 @@ impl Client {
             }
         }?;
         let req = self.client.request(reqwest::Method::GET, url).build()?;
-        let (rsp, log) = execute_and_log_request(&self.client, req)?;
+        let (rsp, log) = self.execute_and_log_request(req)?;
         self.consume_transaction_log(log);
         let rspstatus = rsp.status();
         if rspstatus.is_redirection() || rspstatus.is_success() {