From c72a5defee481f1778a3ac5e2a320217b7f584c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Tue, 24 Mar 2020 16:18:56 +0100 Subject: [PATCH] rust-sdk: Changes for the new ruma-api version. --- Cargo.toml | 8 +++---- src/async_client.rs | 43 +++++++++++++++++-------------------- src/error.rs | 7 +++--- tests/async_client_tests.rs | 3 ++- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4e361bb5..ceff5919 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,13 +23,13 @@ url = "2.1.1" # Ruma dependencies js_int = "0.1.3" -ruma-api = "0.14.0" -ruma-client-api = { version = "0.6.0", git = "https://github.com/matrix-org/ruma-client-api/" } -ruma-events = "0.17.0" +ruma-api = "0.15.0-dev.1" +ruma-client-api = { version = "0.6.0", path = "/home/poljar/werk/misc/ruma-client-api" } +ruma-events = { path = "/home/poljar/werk/misc/ruma-events", version = "0.17.0" } ruma-identifiers = "0.14.1" # Dependencies for the encryption support -olm-rs = { git = "https://gitlab.gnome.org/poljar/olm-rs", branch = "idiomatic-sessions", optional = true, features = ["serde"]} +olm-rs = { path = "/home/poljar/werk/priv/olm-rs", optional = true, features = ["serde"]} serde = { version = "1.0.104", optional = true, features = ["derive"] } serde_json = { version = "1.0.48", optional = true } cjson = { version = "0.1.0", optional = true } diff --git a/src/async_client.rs b/src/async_client.rs index 3aad1bee..fbbac73b 100644 --- a/src/async_client.rs +++ b/src/async_client.rs @@ -30,6 +30,7 @@ use reqwest::header::{HeaderValue, InvalidHeaderValue}; use url::Url; use ruma_api::{Endpoint, Outgoing}; +use ruma_client_api::Error as RumaClientError; use ruma_events::collections::all::RoomEvent; use ruma_events::room::message::MessageEventContent; use ruma_events::EventResult; @@ -47,7 +48,7 @@ type RoomEventCallback = Box< dyn FnMut(Arc>, Arc>) -> BoxFuture<'static, ()> + Send, >; -const DEFAULT_SYNC_TIMEOUT: u64 = 30000; +const DEFAULT_SYNC_TIMEOUT: Duration = Duration::from_secs(30); #[derive(Clone)] /// An async/await enabled Matrix client. @@ -134,7 +135,7 @@ impl AsyncClientConfig { #[derive(Debug, Default, Clone)] /// Settings for a sync call. pub struct SyncSettings { - pub(crate) timeout: Option, + pub(crate) timeout: Option, pub(crate) token: Option, pub(crate) full_state: Option, } @@ -161,16 +162,9 @@ impl SyncSettings { /// # Arguments /// /// * `timeout` - The time the server is allowed to wait. - pub fn timeout>( - mut self, - timeout: T, - ) -> StdResult - where - js_int::TryFromIntError: - std::convert::From<>::Error>, - { - self.timeout = Some(timeout.try_into()?); - Ok(self) + pub fn timeout(mut self, timeout: Duration) -> Self { + self.timeout = Some(timeout); + self } /// Should the server return the full state from the start of the timeline. @@ -535,14 +529,11 @@ impl AsyncClient { last_sync_time = Some(now); - sync_settings = SyncSettings::new() - .timeout(DEFAULT_SYNC_TIMEOUT) - .expect("Default sync timeout doesn't contain a valid value") - .token( - self.sync_token() - .await - .expect("No sync token found after initial sync"), - ); + sync_settings = SyncSettings::new().timeout(DEFAULT_SYNC_TIMEOUT).token( + self.sync_token() + .await + .expect("No sync token found after initial sync"), + ); } } @@ -553,8 +544,13 @@ impl AsyncClient { where Request::Incoming: TryFrom>, Error = ruma_api::error::FromHttpRequestError>, - ::Incoming: - TryFrom>, Error = ruma_api::error::FromHttpResponseError>, + ::Incoming: TryFrom< + http::Response>, + Error = ruma_api::error::FromHttpResponseError< + ::ResponseError, + >, + >, + ::ResponseError: std::fmt::Debug, { let request: http::Request> = request.try_into()?; let url = request.uri(); @@ -607,7 +603,8 @@ impl AsyncClient { let body = response.bytes().await?.as_ref().to_owned(); let http_response = http_response.body(body).unwrap(); - let response = ::Incoming::try_from(http_response)?; + let response = ::Incoming::try_from(http_response) + .expect("Can't convert http response into ruma response"); Ok(response) } diff --git a/src/error.rs b/src/error.rs index 6d3de461..817baae9 100644 --- a/src/error.rs +++ b/src/error.rs @@ -18,6 +18,7 @@ use reqwest::Error as ReqwestError; use ruma_api::error::FromHttpResponseError as RumaResponseError; use ruma_api::error::IntoHttpError as RumaIntoHttpError; +use ruma_client_api::Error as RumaClientError; use thiserror::Error; use url::ParseError; @@ -41,7 +42,7 @@ pub enum Error { Uri(#[from] ParseError), /// An error converting between ruma_client_api types and Hyper types. #[error("can't parse the JSON response as a Matrix response")] - RumaResponse(RumaResponseError), + RumaResponse(RumaResponseError), /// An error converting between ruma_client_api types and Hyper types. #[error("can't convert between ruma_client_api and hyper types.")] IntoHttp(RumaIntoHttpError), @@ -51,8 +52,8 @@ pub enum Error { OlmError(#[from] OlmError), } -impl From for Error { - fn from(error: RumaResponseError) -> Self { +impl From> for Error { + fn from(error: RumaResponseError) -> Self { Self::RumaResponse(error) } } diff --git a/tests/async_client_tests.rs b/tests/async_client_tests.rs index 3c1e1251..1103310c 100644 --- a/tests/async_client_tests.rs +++ b/tests/async_client_tests.rs @@ -7,6 +7,7 @@ use url::Url; use std::convert::TryFrom; use std::str::FromStr; +use std::time::Duration; #[test] fn login() { @@ -50,7 +51,7 @@ fn sync() { let mut client = AsyncClient::new(homeserver, Some(session)).unwrap(); - let sync_settings = SyncSettings::new().timeout(3000).unwrap(); + let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); let response = rt.block_on(client.sync(sync_settings)).unwrap();