chiark / gitweb /
Readonly mode for the client.
authorSimon Tatham <anakin@pobox.com>
Thu, 28 Dec 2023 18:07:48 +0000 (18:07 +0000)
committerSimon Tatham <anakin@pobox.com>
Thu, 28 Dec 2023 18:07:48 +0000 (18:07 +0000)
This was very useful when I was testing the editor last time round,
and surely it'll be just as useful this time.

src/client.rs

index ac38593416fe821667700aa6897c309d3add8179..e567edc1c79651f965e43b4fc3b723196e3dd1d2 100644 (file)
@@ -9,8 +9,10 @@ pub struct Client {
     client: reqwest::blocking::Client,
     accounts: HashMap<String, Account>,
     statuses: HashMap<String, Status>,
+    permit_write: bool,
 }
 
+#[derive(Debug)]
 pub enum ClientError {
     InternalError(String), // message
     UrlParseError(String, String), // url, message
@@ -34,12 +36,22 @@ impl Client {
             client: reqwest::blocking::Client::new(),
             accounts: HashMap::new(),
             statuses: HashMap::new(),
+            permit_write: false,
         })
     }
 
-    fn api_request(&self, method: reqwest::Method, url_suffix: &str)
-               -> Result<(String, reqwest::blocking::RequestBuilder),
-                         ClientError> {
+    pub fn set_writable(&mut self, permit: bool) {
+        self.permit_write = permit;
+    }
+
+    fn api_request(&self, method: reqwest::Method, url_suffix: &str) ->
+        Result<(String, reqwest::blocking::RequestBuilder), ClientError>
+    {
+        if method != reqwest::Method::GET && !self.permit_write {
+            return Err(ClientError::InternalError(
+                "Non-GET request attempted in readonly mode".to_string()));
+        }
+
         let urlstr = self.auth.instance_url.clone() + "/api/v1/" + url_suffix;
         let url = match Url::parse(&urlstr) {
             Ok(url) => Ok(url),