From 51d7baca06c6f708f540621870a7c35c4cda99bd Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Thu, 28 Dec 2023 18:07:48 +0000 Subject: [PATCH] Readonly mode for the client. 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 | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/src/client.rs b/src/client.rs index ac38593..e567edc 100644 --- a/src/client.rs +++ b/src/client.rs @@ -9,8 +9,10 @@ pub struct Client { client: reqwest::blocking::Client, accounts: HashMap, statuses: HashMap, + 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), -- 2.30.2