rust-sdk: Changes for the new ruma-api version.

master
Damir Jelić 2020-03-24 16:18:56 +01:00
parent 4759dea9bd
commit c72a5defee
4 changed files with 30 additions and 31 deletions

View File

@ -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 }

View File

@ -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<SyncLock<Room>>, Arc<EventResult<RoomEvent>>) -> 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<UInt>,
pub(crate) timeout: Option<Duration>,
pub(crate) token: Option<String>,
pub(crate) full_state: Option<bool>,
}
@ -161,16 +162,9 @@ impl SyncSettings {
/// # Arguments
///
/// * `timeout` - The time the server is allowed to wait.
pub fn timeout<T: TryInto<UInt>>(
mut self,
timeout: T,
) -> StdResult<Self, js_int::TryFromIntError>
where
js_int::TryFromIntError:
std::convert::From<<T as std::convert::TryInto<js_int::UInt>>::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<http::Request<Vec<u8>>, Error = ruma_api::error::FromHttpRequestError>,
<Request::Response as Outgoing>::Incoming:
TryFrom<http::Response<Vec<u8>>, Error = ruma_api::error::FromHttpResponseError>,
<Request::Response as Outgoing>::Incoming: TryFrom<
http::Response<Vec<u8>>,
Error = ruma_api::error::FromHttpResponseError<
<Request as ruma_api::Endpoint>::ResponseError,
>,
>,
<Request as ruma_api::Endpoint>::ResponseError: std::fmt::Debug,
{
let request: http::Request<Vec<u8>> = 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 = <Request::Response as Outgoing>::Incoming::try_from(http_response)?;
let response = <Request::Response as Outgoing>::Incoming::try_from(http_response)
.expect("Can't convert http response into ruma response");
Ok(response)
}

View File

@ -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<RumaClientError>),
/// 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<RumaResponseError> for Error {
fn from(error: RumaResponseError) -> Self {
impl From<RumaResponseError<RumaClientError>> for Error {
fn from(error: RumaResponseError<RumaClientError>) -> Self {
Self::RumaResponse(error)
}
}

View File

@ -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();