From 27f95acb4505b731bf3d592129137a4d4a2591b0 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Tue, 2 Jan 2024 07:01:41 +0000 Subject: [PATCH] Decentralise version component of API URLs. I thought it was /api/v1/whatever, but I've just realised that it's actually /api/v2/instance in particular. _That's_ why I keep seeing "uri" from some servers and "domain" from others: if I requested the up-to-date version of the record, it would agree on "domain". --- src/client.rs | 30 +++++++++++++++--------------- src/types.rs | 6 +----- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/client.rs b/src/client.rs index 14fbed5..66a32ef 100644 --- a/src/client.rs +++ b/src/client.rs @@ -188,7 +188,7 @@ impl Client { "Non-GET request attempted in readonly mode".to_string())); } - let urlstr = self.auth.instance_url.clone() + "/api/v1/" + + let urlstr = self.auth.instance_url.clone() + "/api/" + &req.url_suffix; let parsed = if req.parameters.is_empty() { Url::parse(&urlstr) @@ -216,7 +216,7 @@ impl Client { return Ok(inst.clone()); } - let (url, req) = self.api_request(Req::get("instance"))?; + let (url, req) = self.api_request(Req::get("v2/instance"))?; let rsp = req.send()?; let rspstatus = rsp.status(); let inst: Instance = if !rspstatus.is_success() { @@ -255,7 +255,7 @@ impl Client { } let (url, req) = self.api_request(Req::get( - &("accounts/".to_owned() + id)))?; + &("v1/accounts/".to_owned() + id)))?; let rsp = req.send()?; let rspstatus = rsp.status(); let ac: Account = if !rspstatus.is_success() { @@ -280,7 +280,7 @@ impl Client { Result { let (url, req) = self.api_request( - Req::get("accounts/relationships").param("id", id))?; + Req::get("v1/accounts/relationships").param("id", id))?; let rsp = req.send()?; let rspstatus = rsp.status(); let rels: Vec = if !rspstatus.is_success() { @@ -314,7 +314,7 @@ impl Client { } let (url, req) = self.api_request(Req::get( - &("statuses/".to_owned() + id)))?; + &("v1/statuses/".to_owned() + id)))?; let rsp = req.send()?; let rspstatus = rsp.status(); let st: Status = if !rspstatus.is_success() { @@ -359,7 +359,7 @@ impl Client { } let (url, req) = self.api_request(Req::get( - &("notifications/".to_owned() + id)))?; + &("v1/notifications/".to_owned() + id)))?; let rsp = req.send()?; let rspstatus = rsp.status(); let not: Notification = if !rspstatus.is_success() { @@ -411,26 +411,26 @@ impl Client { } let req = match id { - FeedId::Home => Req::get("timelines/home"), + FeedId::Home => Req::get("v1/timelines/home"), FeedId::Local => { - Req::get("timelines/public").param("local", true) + Req::get("v1/timelines/public").param("local", true) } FeedId::Public => { - Req::get("timelines/public").param("local", false) + Req::get("v1/timelines/public").param("local", false) } FeedId::Hashtag(ref tag) => { - Req::get(&format!("timelines/tag/{}", &tag)) + Req::get(&format!("v1/timelines/tag/{}", &tag)) } FeedId::User(id, boosts, replies) => { - Req::get(&format!("accounts/{}/statuses", id)) + Req::get(&format!("v1/accounts/{}/statuses", id)) .param("exclude_reblogs", *boosts == Boosts::Hide) .param("exclude_replies", *replies == Replies::Hide) } FeedId::Mentions => { - Req::get("notifications").param("types[]", "mention") + Req::get("v1/notifications").param("types[]", "mention") } FeedId::Ego => { - Req::get("notifications") + Req::get("v1/notifications") .param("types[]", "reblog") .param("types[]", "follow") .param("types[]", "favourite") @@ -551,7 +551,7 @@ impl Client { &self, id: &StreamId, receiver: Box) -> Result<(), ClientError> { let req = match id { - StreamId::User => Req::get("streaming/user"), + StreamId::User => Req::get("v1/streaming/user"), }; let client = reqwest::blocking::Client::new(); @@ -651,7 +651,7 @@ impl Client { Result { let (url, req) = self.api_request( - Req::get("accounts/lookup").param("acct", name))?; + Req::get("v1/accounts/lookup").param("acct", name))?; let rsp = req.send()?; let rspstatus = rsp.status(); let ac: Account = if !rspstatus.is_success() { diff --git a/src/types.rs b/src/types.rs index ef6a454..ff84ad5 100644 --- a/src/types.rs +++ b/src/types.rs @@ -245,11 +245,7 @@ pub struct InstanceConfig { #[derive(Deserialize, Debug, Clone)] pub struct Instance { - // At least one major live instance (hachyderm.io) uses the key - // "uri" for its instance domain. But the Mastodon test instance - // uses "domain". Bah. - #[serde(alias="uri")] pub domain: Option, - + pub domain: String, pub configuration: InstanceConfig, // FIXME: lots of things are missing from here! -- 2.30.2