diff --git a/docs/todo.md b/docs/todo.md index 29bd8e9..28570f2 100644 --- a/docs/todo.md +++ b/docs/todo.md @@ -5,7 +5,7 @@ - [x] ~~3 Web Browser Clients~~ *Not applicable* - [ ] 4 Server Discovery - [ ] 4.1 Well-known URI - - [ ] 4.1.1 GET /.well-known/matrix/client + - [x] ~~4.1.1 GET /.well-known/matrix/client~~ - [ ] 5 Client Authentication - [x] ~~5.1 Using access tokens~~ *Only through Authorization header* - [ ] 5.2 Relationship between access tokens and devices diff --git a/src/api/client.rs b/src/api/client.rs index 5340534..eb35612 100644 --- a/src/api/client.rs +++ b/src/api/client.rs @@ -68,6 +68,23 @@ pub struct SupportedSpecs { pub unstable_features: Option>, } +#[derive(Deserialize)] +pub struct DiscoveryInformation { + #[serde(rename = "m.homeserver")] + homeserver: HomeserverInformation, + identity_server: Option, +} + +#[derive(Deserialize)] +pub struct HomeserverInformation { + base_url: String, +} + +#[derive(Deserialize)] +pub struct IdentityServerInformation { + base_url: String, +} + impl Client { pub fn new( homeserver_url: &str, @@ -100,15 +117,33 @@ impl Client { Ok(client) } - /// Implementation of [Section 2.1 **GET** /_matrix/client/versions](https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-versions). + /// Implementation of [Section 4.1.1 **GET** `/.well-known/matrix/client`](https://matrix.org/docs/spec/client_server/r0.5.0#get-well-known-matrix-client). + /// + /// This does not implement the expected behavior a client *should* perform + /// as described in [Section 4.1 Well-Known URI](https://matrix.org/docs/spec/client_server/r0.5.0#well-known-uri). + /// Higher level clients must implement this behavior. + pub fn discovery_information(&self) -> Result> { + Ok(self + .send( + MatrixHTTPMethod::Get, + "/.well-known/matrix/client", + None, + None, + None, + )? + .json()?) + } + + /// Implementation of [Section 2.1 **GET** `/_matrix/client/versions`](https://matrix.org/docs/spec/client_server/r0.5.0#get-matrix-client-versions). /// /// Returns a list of matrix specifications a server supports, as well as - /// a map of unstable features the server has advertised. + /// a map of unstable features the server has advertised. This is not rate- + /// limited nor requires authorization. pub fn supported_versions(&self) -> Result> { Ok(self .send( MatrixHTTPMethod::Get, - Some("/_matrix/client/versions"), + "/_matrix/client/versions", None, None, None, @@ -130,7 +165,7 @@ impl Client { fn send( &self, method: MatrixHTTPMethod, - path: Option<&str>, + path: &str, content: Option, query_params: Option>, headers: Option, @@ -143,7 +178,7 @@ impl Client { #[cfg(not(test))] let url = &self.homeserver_url; - let endpoint = &format!("{}{}", url, path.unwrap_or_else(|| V2_API_PATH)); + let endpoint = &format!("{}{}", url, path); let mut request = match method { MatrixHTTPMethod::Get => self.reqwest_client.get(endpoint), MatrixHTTPMethod::Put => self.reqwest_client.put(endpoint), @@ -209,7 +244,7 @@ impl Client { path: &str, query_params: HashMap, ) -> Result> { - self.send(method, Some(path), None, Some(query_params), None) + self.send(method, path, None, Some(query_params), None) } pub fn sync(