From 53876ea6e8d9c57b55c39cce5913bb9f6b164fd4 Mon Sep 17 00:00:00 2001 From: Emi Simpson Date: Wed, 20 May 2020 14:24:35 -0400 Subject: [PATCH 1/5] Make Client::send a public method, add a short doccomment --- matrix_sdk/src/client.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index b3ec59ee..8afdf7cc 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -916,7 +916,17 @@ impl Client { } } - async fn send + std::fmt::Debug>( + /// Send an arbitrary request to the server, without updating client state + /// + /// **Warning:** Because this method *does not* update the client state, it is + /// important to make sure than you account for this yourself, and use wrapper methods + /// where available. This method should *only* be used if a wrapper method for the + /// endpoint you'd like to use is not available. + /// + /// # Arguments + /// + /// * `request` - A filled out and valid request for the endpoint to be hit + pub async fn send + std::fmt::Debug>( &self, request: Request, ) -> Result { From 7201749280c06092cdc176cc9c83506c854d8af1 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 28 May 2020 16:32:28 +0200 Subject: [PATCH 2/5] Add small example on how to use Client::send --- matrix_sdk/examples/get_profiles.rs | 80 +++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) create mode 100644 matrix_sdk/examples/get_profiles.rs diff --git a/matrix_sdk/examples/get_profiles.rs b/matrix_sdk/examples/get_profiles.rs new file mode 100644 index 00000000..90b84f68 --- /dev/null +++ b/matrix_sdk/examples/get_profiles.rs @@ -0,0 +1,80 @@ +use std::convert::TryFrom; +use std::{env, process::exit}; + +use url::Url; + +use matrix_sdk::{ + self, + identifiers::UserId, + api::r0::profile, + Client, ClientConfig, Result as MatrixResult, +}; + +#[derive(Debug)] +struct UserProfile { + avatar_url: Option, + displayname: Option, +} + +/// This function calls the GET profile endpoint +/// Spec: https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-profile-userid +/// Ruma: https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/r0/profile/get_profile/index.html +async fn get_profile(client: Client, mxid: UserId) -> MatrixResult { + + // First construct the request you want to make + // See https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/index.html for all available Endpoints + let request = profile::get_profile::Request { + user_id: mxid.clone() + }; + + // Start the request using matrix_sdk::Client::send + let resp = client.send(request).await?; + + // Use the response and construct a UserProfile struct. + // See https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/r0/profile/get_profile/struct.Response.html + // for details on the Response for this Request + let user_profile = UserProfile{ avatar_url: resp.avatar_url, displayname: resp.displayname }; + Ok(user_profile) +} + +async fn login( + homeserver_url: String, + username: String, + password: String, +) -> Result { + let client_config = ClientConfig::new() + .proxy("http://localhost:8080")? + .disable_ssl_verification(); + let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL"); + let client = Client::new_with_config(homeserver_url, None, client_config).unwrap(); + + client + .login(username, password, None, Some("rust-sdk".to_string())) + .await?; + + Ok(client) +} + +#[tokio::main] +async fn main() -> Result<(), matrix_sdk::Error> { + tracing_subscriber::fmt::init(); + + let (homeserver_url, username, password) = + match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) { + (Some(a), Some(b), Some(c)) => (a, b, c), + _ => { + eprintln!( + "Usage: {} ", + env::args().next().unwrap() + ); + exit(1) + } + }; + + let client = login(homeserver_url, username.clone(), password).await?; + + let user_id = UserId::try_from(username.clone()).expect("Couldn't parse the MXID"); + let profile = get_profile(client, user_id).await?; + println!("{:#?}", profile); + Ok(()) +} From 1d00f7967556a58650ceed48791e64f0ffd005db Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 28 May 2020 16:47:04 +0200 Subject: [PATCH 3/5] Run cargo fmt for the get_profiles example --- matrix_sdk/examples/get_profiles.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/matrix_sdk/examples/get_profiles.rs b/matrix_sdk/examples/get_profiles.rs index 90b84f68..5419e267 100644 --- a/matrix_sdk/examples/get_profiles.rs +++ b/matrix_sdk/examples/get_profiles.rs @@ -4,10 +4,7 @@ use std::{env, process::exit}; use url::Url; use matrix_sdk::{ - self, - identifiers::UserId, - api::r0::profile, - Client, ClientConfig, Result as MatrixResult, + self, api::r0::profile, identifiers::UserId, Client, ClientConfig, Result as MatrixResult, }; #[derive(Debug)] @@ -20,11 +17,10 @@ struct UserProfile { /// Spec: https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-profile-userid /// Ruma: https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/r0/profile/get_profile/index.html async fn get_profile(client: Client, mxid: UserId) -> MatrixResult { - // First construct the request you want to make // See https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/index.html for all available Endpoints let request = profile::get_profile::Request { - user_id: mxid.clone() + user_id: mxid.clone(), }; // Start the request using matrix_sdk::Client::send @@ -33,7 +29,10 @@ async fn get_profile(client: Client, mxid: UserId) -> MatrixResult // Use the response and construct a UserProfile struct. // See https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/r0/profile/get_profile/struct.Response.html // for details on the Response for this Request - let user_profile = UserProfile{ avatar_url: resp.avatar_url, displayname: resp.displayname }; + let user_profile = UserProfile { + avatar_url: resp.avatar_url, + displayname: resp.displayname, + }; Ok(user_profile) } From 6a323525b5b50d194f73514b2206720c0fe3aaa7 Mon Sep 17 00:00:00 2001 From: Marcel Date: Thu, 28 May 2020 16:59:30 +0200 Subject: [PATCH 4/5] Add example to the Client::send() doccomment --- matrix_sdk/src/client.rs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 8afdf7cc..e06d3eb5 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -926,6 +926,24 @@ impl Client { /// # Arguments /// /// * `request` - A filled out and valid request for the endpoint to be hit + /// + /// # Example + /// + /// ```compile_fail + /// use matrix_sdk::api::r0::profile; + /// + /// // First construct the request you want to make + /// // See https://docs.rs/ruma-client-api/latest/ruma_client_api/index.html + /// // for all available Endpoints + /// let request = profile::get_profile::Request { + /// user_id: mxid.clone(), + /// }; + /// + /// // Start the request using Client::send() + /// let resp = client.send(request).await.unwrap(); + /// + /// // Check the corresponding Response struct to find out what types are returned + /// ``` pub async fn send + std::fmt::Debug>( &self, request: Request, From 54871f2af93f730a71c8647e90fde601520df69b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 2 Jun 2020 11:15:04 +0200 Subject: [PATCH 5/5] matrix-sdk: Make the example for the send method comiple. --- matrix_sdk/src/client.rs | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index e06d3eb5..2ab728cf 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -929,20 +929,30 @@ impl Client { /// /// # Example /// - /// ```compile_fail + /// ```no_run + /// # use matrix_sdk::{Client, SyncSettings}; + /// # use futures::executor::block_on; + /// # use url::Url; + /// # use std::convert::TryFrom; + /// # block_on(async { + /// # let homeserver = Url::parse("http://localhost:8080").unwrap(); + /// # let mut client = Client::new(homeserver, None).unwrap(); /// use matrix_sdk::api::r0::profile; + /// use matrix_sdk::identifiers::UserId; /// /// // First construct the request you want to make /// // See https://docs.rs/ruma-client-api/latest/ruma_client_api/index.html /// // for all available Endpoints /// let request = profile::get_profile::Request { - /// user_id: mxid.clone(), + /// user_id: UserId::try_from("@example:localhost").unwrap(), /// }; /// /// // Start the request using Client::send() - /// let resp = client.send(request).await.unwrap(); + /// let response = client.send(request).await.unwrap(); /// - /// // Check the corresponding Response struct to find out what types are returned + /// // Check the corresponding Response struct to find out what types are + /// // returned + /// # }) /// ``` pub async fn send + std::fmt::Debug>( &self, @@ -1050,7 +1060,7 @@ impl Client { /// }); /// let txn_id = Uuid::new_v4(); /// client.room_send(&room_id, content, Some(txn_id)).await.unwrap(); - /// }) + /// # }) /// ``` pub async fn room_send( &self,