nio: Upgrade ruma-client-api.

master
Damir Jelić 2019-12-04 19:31:33 +01:00
parent 38ddad1874
commit 41a183b4c0
4 changed files with 46 additions and 18 deletions

View File

@ -15,18 +15,19 @@ js_int = "0.1.2"
futures = "0.3.1"
reqwest = "0.10.0-alpha.2"
http = "0.1.19"
ruma-api = "0.11.0"
ruma-client-api = "0.4.0"
async-std = "1.2.0"
ruma-api = "0.12.0-alpha.1"
ruma-client-api = "0.5.0-alpha.1"
ruma-events = "0.15.1"
log = "0.4.8"
ruma-identifiers = "0.14.0"
serde_json = "1.0.41"
serde_json = "1.0.42"
serde_urlencoded = "0.6.1"
url = "2.1.0"
[dependencies.serde]
version = "1.0.102"
version = "1.0.103"
features = ["derive"]
[dev-dependencies]

View File

@ -45,7 +45,7 @@ async fn login(
let homeserver_url = Url::parse(&homeserver_url)?;
let mut client = AsyncClient::new_with_config(homeserver_url, None, client_config).unwrap();
client.add_event_future(async_cb);
client.add_event_callback(async_cb);
client.login(username, password, None).await?;
let _response = client.sync(SyncSettings::new()).await?;

View File

@ -9,7 +9,7 @@ use js_int::UInt;
use reqwest::header::{HeaderValue, InvalidHeaderValue};
use url::Url;
use ruma_api::Endpoint;
use ruma_api::{Endpoint, Outgoing};
use ruma_events::collections::all::RoomEvent;
use ruma_events::room::message::MessageEventContent;
use ruma_events::EventResult;
@ -23,9 +23,12 @@ use crate::error::{Error, InnerError};
use crate::session::Session;
use crate::VERSION;
type RoomEventCallbackF =
type RoomEventCallback =
Box<dyn FnMut(Arc<RwLock<Room>>, Arc<EventResult<RoomEvent>>) -> BoxFuture<'static, ()> + Send>;
type ResponseCallback =
Box<dyn FnMut(Arc<sync_events::IncomingResponse>) -> BoxFuture<'static, ()> + Send>;
#[derive(Clone)]
pub struct AsyncClient {
/// The URL of the homeserver to connect to.
@ -36,8 +39,10 @@ pub struct AsyncClient {
base_client: Arc<RwLock<BaseClient>>,
/// The transaction id.
transaction_id: Arc<AtomicU64>,
/// Event futures
event_futures: Arc<Mutex<Vec<RoomEventCallbackF>>>,
/// Event callbacks
event_callbacks: Arc<Mutex<Vec<RoomEventCallback>>>,
// /// Response callbacks.
response_callbacks: Arc<Mutex<Vec<ResponseCallback>>>,
}
#[derive(Default, Debug)]
@ -68,7 +73,7 @@ impl AsyncClientConfig {
}
}
#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct SyncSettings {
pub(crate) timeout: Option<UInt>,
pub(crate) token: Option<String>,
@ -153,7 +158,8 @@ impl AsyncClient {
http_client,
base_client: Arc::new(RwLock::new(BaseClient::new(session))),
transaction_id: Arc::new(AtomicU64::new(0)),
event_futures: Arc::new(Mutex::new(Vec::new())),
event_callbacks: Arc::new(Mutex::new(Vec::new())),
response_callbacks: Arc::new(Mutex::new(Vec::new())),
})
}
@ -161,19 +167,32 @@ impl AsyncClient {
self.base_client.read().unwrap().logged_in()
}
pub fn add_event_future<C: 'static>(
pub fn add_event_callback<C: 'static>(
&mut self,
mut callback: impl FnMut(Arc<RwLock<Room>>, Arc<EventResult<RoomEvent>>) -> C + 'static + Send,
) where
C: Future<Output = ()> + Send,
{
let mut futures = self.event_futures.lock().unwrap();
let mut futures = self.event_callbacks.lock().unwrap();
let future = move |room, event| callback(room, event).boxed();
futures.push(Box::new(future));
}
pub fn add_response_callback<C: 'static>(
&mut self,
mut callback: impl FnMut(Arc<sync_events::IncomingResponse>) -> C + 'static + Send,
) where
C: Future<Output = ()> + Send,
{
let mut futures = self.response_callbacks.lock().unwrap();
let future = move |response| callback(response).boxed();
futures.push(Box::new(future));
}
pub async fn login<S: Into<String>>(
&mut self,
user: S,
@ -199,7 +218,7 @@ impl AsyncClient {
pub async fn sync(
&mut self,
sync_settings: SyncSettings,
) -> Result<sync_events::Response, Error> {
) -> Result<sync_events::IncomingResponse, Error> {
let request = sync_events::Request {
filter: None,
since: sync_settings.token,
@ -234,7 +253,7 @@ impl AsyncClient {
let event = Arc::new(event.clone());
let callbacks = {
let mut cb_futures = self.event_futures.lock().unwrap();
let mut cb_futures = self.event_callbacks.lock().unwrap();
let mut callbacks = Vec::new();
for cb in &mut cb_futures.iter_mut() {
@ -258,7 +277,15 @@ impl AsyncClient {
async fn sync_forever() {}
async fn send<Request: Endpoint>(&self, request: Request) -> Result<Request::Response, Error> {
async fn send<Request: Endpoint>(
&self,
request: Request,
) -> Result<<Request::Response as Outgoing>::Incoming, Error>
where
Request::Incoming: TryFrom<http::Request<Vec<u8>>, Error = ruma_api::Error>,
<Request::Response as Outgoing>::Incoming:
TryFrom<http::Response<Vec<u8>>, Error = ruma_api::Error>,
{
let request: http::Request<Vec<u8>> = request.try_into()?;
let url = request.uri();
let url = self
@ -297,7 +324,7 @@ impl AsyncClient {
let status = response.status();
let body = response.bytes().await?.as_ref().to_owned();
let response = HttpResponse::builder().status(status).body(body).unwrap();
let response = Request::Response::try_from(response)?;
let response = <Request::Response as Outgoing>::Incoming::try_from(response)?;
Ok(response)
}

View File

@ -243,7 +243,7 @@ impl Client {
room.receive_state_event(event)
}
pub fn receive_sync_response(&mut self, response: &api::sync::sync_events::Response) {
pub fn receive_sync_response(&mut self, response: &api::sync::sync_events::IncomingResponse) {
self.sync_token = Some(response.next_batch.clone());
}
}