chiark / gitweb /
Stop centrally prepending /api to URLs.
authorSimon Tatham <anakin@pobox.com>
Wed, 17 Jan 2024 12:46:16 +0000 (12:46 +0000)
committerSimon Tatham <anakin@pobox.com>
Wed, 17 Jan 2024 12:46:16 +0000 (12:46 +0000)
If I move the login logic into client.rs, then we'll need a few URLs
that start with /oauth instead of /api.

src/client.rs

index 248a14cffdc91201e350516890e6e3e25ef7ca03..6c1fac6ef621226be20dd2b3781de52725720da6 100644 (file)
@@ -535,8 +535,12 @@ impl Client {
             .instance_url
             .as_ref()
             .expect("Should have set up an instance URL before calling")
-            .clone()
-            + "/api/";
+            .clone();
+        let base_url = if !base_url.ends_with("/") {
+            base_url + "/"
+        } else {
+            base_url
+        };
         req.build(&base_url, client, self.auth.user_token.as_deref())
     }
 
@@ -555,7 +559,7 @@ impl Client {
             return Ok(inst.clone());
         }
 
-        let (url, rsp) = self.api_request(Req::get("v2/instance"))?;
+        let (url, rsp) = self.api_request(Req::get("api/v2/instance"))?;
         let rspstatus = rsp.status();
         let inst: Instance = if !rspstatus.is_success() {
             Err(ClientError::UrlError(url, rspstatus.to_string()))
@@ -623,9 +627,9 @@ impl Client {
         }
 
         let req = if Some(id) == self.auth.account_id.as_deref() {
-            Req::get(&format!("v1/accounts/verify_credentials"))
+            Req::get(&format!("api/v1/accounts/verify_credentials"))
         } else {
-            Req::get(&format!("v1/accounts/{id}"))
+            Req::get(&format!("api/v1/accounts/{id}"))
         };
         let (url, rsp) = self.api_request(req)?;
         let rspstatus = rsp.status();
@@ -655,7 +659,7 @@ impl Client {
         }
 
         let (url, rsp) =
-            self.api_request(Req::get(&("v1/polls/".to_owned() + id)))?;
+            self.api_request(Req::get(&("api/v1/polls/".to_owned() + id)))?;
         let rspstatus = rsp.status();
         let poll: Poll = if !rspstatus.is_success() {
             Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
@@ -682,7 +686,7 @@ impl Client {
         id: &str,
     ) -> Result<Relationship, ClientError> {
         let (url, rsp) = self.api_request(
-            Req::get("v1/accounts/relationships").param("id", id),
+            Req::get("api/v1/accounts/relationships").param("id", id),
         )?;
         let rspstatus = rsp.status();
         let rels: Vec<Relationship> = if !rspstatus.is_success() {
@@ -724,7 +728,7 @@ impl Client {
         }
 
         let (url, rsp) =
-            self.api_request(Req::get(&("v1/statuses/".to_owned() + id)))?;
+            self.api_request(Req::get(&("api/v1/statuses/".to_owned() + id)))?;
         let rspstatus = rsp.status();
         let st: Status = if !rspstatus.is_success() {
             Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
@@ -766,8 +770,9 @@ impl Client {
             return Ok(not);
         }
 
-        let (url, rsp) = self
-            .api_request(Req::get(&("v1/notifications/".to_owned() + id)))?;
+        let (url, rsp) = self.api_request(Req::get(
+            &("api/v1/notifications/".to_owned() + id),
+        ))?;
         let rspstatus = rsp.status();
         let not: Notification = if !rspstatus.is_success() {
             Err(ClientError::UrlError(url.clone(), rspstatus.to_string()))
@@ -819,39 +824,39 @@ impl Client {
         }
 
         let req = match id {
-            FeedId::Home => Req::get("v1/timelines/home"),
+            FeedId::Home => Req::get("api/v1/timelines/home"),
             FeedId::Local => {
-                Req::get("v1/timelines/public").param("local", true)
+                Req::get("api/v1/timelines/public").param("local", true)
             }
             FeedId::Public => {
-                Req::get("v1/timelines/public").param("local", false)
+                Req::get("api/v1/timelines/public").param("local", false)
             }
             FeedId::Hashtag(ref tag) => {
-                Req::get(&format!("v1/timelines/tag/{}", &tag))
+                Req::get(&format!("api/v1/timelines/tag/{}", &tag))
             }
             FeedId::User(id, boosts, replies) => {
-                Req::get(&format!("v1/accounts/{}/statuses", id))
+                Req::get(&format!("api/v1/accounts/{}/statuses", id))
                     .param("exclude_reblogs", *boosts == Boosts::Hide)
                     .param("exclude_replies", *replies == Replies::Hide)
             }
             FeedId::Mentions => {
-                Req::get("v1/notifications").param("types[]", "mention")
+                Req::get("api/v1/notifications").param("types[]", "mention")
             }
-            FeedId::Ego => Req::get("v1/notifications")
+            FeedId::Ego => Req::get("api/v1/notifications")
                 .param("types[]", "reblog")
                 .param("types[]", "follow")
                 .param("types[]", "favourite"),
             FeedId::Favouriters(id) => {
-                Req::get(&format!("v1/statuses/{id}/favourited_by"))
+                Req::get(&format!("api/v1/statuses/{id}/favourited_by"))
             }
             FeedId::Boosters(id) => {
-                Req::get(&format!("v1/statuses/{id}/reblogged_by"))
+                Req::get(&format!("api/v1/statuses/{id}/reblogged_by"))
             }
             FeedId::Followers(id) => {
-                Req::get(&format!("v1/accounts/{id}/followers"))
+                Req::get(&format!("api/v1/accounts/{id}/followers"))
             }
             FeedId::Followees(id) => {
-                Req::get(&format!("v1/accounts/{id}/following"))
+                Req::get(&format!("api/v1/accounts/{id}/following"))
             }
         };
 
@@ -1066,7 +1071,7 @@ impl Client {
         receiver: Box<Recv>,
     ) -> Result<(), ClientError> {
         let req = match id {
-            StreamId::User => Req::get("v1/streaming/user"),
+            StreamId::User => Req::get("api/v1/streaming/user"),
         };
         let method = req.method.clone(); // to reuse for redirects below
 
@@ -1247,8 +1252,9 @@ impl Client {
         &mut self,
         name: &str,
     ) -> Result<Account, ClientError> {
-        let (url, rsp) = self
-            .api_request(Req::get("v1/accounts/lookup").param("acct", name))?;
+        let (url, rsp) = self.api_request(
+            Req::get("api/v1/accounts/lookup").param("acct", name),
+        )?;
         let rspstatus = rsp.status();
         let ac: Account = if !rspstatus.is_success() {
             Err(ClientError::UrlError(url, rspstatus.to_string()))
@@ -1264,7 +1270,7 @@ impl Client {
 
     pub fn post_status(&mut self, post: &Post) -> Result<(), ClientError> {
         // FIXME: separate Post from a single status, so we can post threads
-        let req = Req::post("v1/statuses")
+        let req = Req::post("api/v1/statuses")
             .param("status", post.text.trim_end_matches('\n'))
             .param("visibility", post.m.visibility)
             .param("language", &post.m.language);
@@ -1293,8 +1299,8 @@ impl Client {
         id: &str,
         verb: &str,
     ) -> Result<(), ClientError> {
-        let (url, rsp) =
-            self.api_request(Req::post(&format!("v1/statuses/{id}/{verb}")))?;
+        let (url, rsp) = self
+            .api_request(Req::post(&format!("api/v1/statuses/{id}/{verb}")))?;
         let rspstatus = rsp.status();
         // Cache the returned status so as to update its faved/boosted flags
         let st: Status = if !rspstatus.is_success() {
@@ -1331,8 +1337,8 @@ impl Client {
         &mut self,
         id: &str,
     ) -> Result<Context, ClientError> {
-        let (url, rsp) =
-            self.api_request(Req::get(&format!("v1/statuses/{id}/context")))?;
+        let (url, rsp) = self
+            .api_request(Req::get(&format!("api/v1/statuses/{id}/context")))?;
         let rspstatus = rsp.status();
         let ctx: Context = if !rspstatus.is_success() {
             Err(ClientError::UrlError(url, rspstatus.to_string()))
@@ -1357,7 +1363,7 @@ impl Client {
         choices: impl Iterator<Item = usize>,
     ) -> Result<(), ClientError> {
         let choices: Vec<_> = choices.collect();
-        let mut req = Req::post(&format!("v1/polls/{id}/votes"));
+        let mut req = Req::post(&format!("api/v1/polls/{id}/votes"));
         for choice in choices {
             req = req.param("choices[]", choice);
         }
@@ -1383,11 +1389,12 @@ impl Client {
     ) -> Result<(), ClientError> {
         let req = match follow {
             Followness::NotFollowing => {
-                Req::post(&format!("v1/accounts/{id}/unfollow"))
+                Req::post(&format!("api/v1/accounts/{id}/unfollow"))
             }
             Followness::Following { boosts, languages } => {
-                let mut req = Req::post(&format!("v1/accounts/{id}/follow"))
-                    .param("reblogs", boosts == Boosts::Show);
+                let mut req =
+                    Req::post(&format!("api/v1/accounts/{id}/follow"))
+                        .param("reblogs", boosts == Boosts::Show);
                 for language in languages {
                     req = req.param("languages[]", &language);
                 }
@@ -1411,16 +1418,16 @@ impl Client {
     ) -> Result<(), ClientError> {
         let req = match (flag, enable) {
             (AccountFlag::Block, true) => {
-                Req::post(&format!("v1/accounts/{id}/block"))
+                Req::post(&format!("api/v1/accounts/{id}/block"))
             }
             (AccountFlag::Block, false) => {
-                Req::post(&format!("v1/accounts/{id}/unblock"))
+                Req::post(&format!("api/v1/accounts/{id}/unblock"))
             }
             (AccountFlag::Mute, true) => {
-                Req::post(&format!("v1/accounts/{id}/mute"))
+                Req::post(&format!("api/v1/accounts/{id}/mute"))
             }
             (AccountFlag::Mute, false) => {
-                Req::post(&format!("v1/accounts/{id}/unmute"))
+                Req::post(&format!("api/v1/accounts/{id}/unmute"))
             }
         };
         let (url, rsp) = self.api_request(req)?;
@@ -1439,7 +1446,7 @@ impl Client {
     ) -> Result<(), ClientError> {
         // TODO: add "note" with details.bio, and "fields_attributes"
         // for the variable info fields
-        let req = Req::patch("v1/accounts/update_credentials")
+        let req = Req::patch("api/v1/accounts/update_credentials")
             .param("display_name", &details.display_name)
             .param("locked", details.locked)
             .param("bot", details.bot)