Skip to content
This repository has been archived by the owner on Sep 8, 2022. It is now read-only.

Commit

Permalink
Upgrade dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
CromFr committed Feb 27, 2019
1 parent 00c4fed commit 7eaa3bf
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 48 deletions.
9 changes: 3 additions & 6 deletions glitch-in-the-matrix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,15 @@ failure = "0.1"
failure_derive = "0.1"
http = "0.1"
futures = "0.1"
hyper-openssl = "0.3"
hyper = "0.12"
hyper-openssl = "0.7"
percent-encoding = "1.0"
serde = "1.0"
serde_json = "1.0"
tokio-core = "0.1"

[dependencies.hyper]
version = ">0.11.3"
features = ["compat"]

[dependencies.uuid]
version = "0.6"
version = "0.7"
features = ["v4"]

[dependencies.gm-types]
Expand Down
6 changes: 4 additions & 2 deletions glitch-in-the-matrix/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ macro_rules! derive_from {
)*
}
}
use failure::Fail;

/// Something Matrixy that can go wrong.
#[derive(Fail, Debug)]
#[allow(missing_docs)]
Expand All @@ -19,7 +21,7 @@ pub enum MatrixError {
#[fail(display = "Serialization error: {}", _0)]
Serde(#[cause] ::serde_json::Error),
#[fail(display = "Error decoding URI: {}", _0)]
UriError(#[cause] ::hyper::error::UriError),
UriError(#[cause] ::hyper::http::uri::InvalidUri),
#[fail(display = "I/O error: {}", _0)]
Io(#[cause] ::std::io::Error),
#[fail(display = "OpenSSL error: {}", _0)]
Expand All @@ -41,7 +43,7 @@ pub enum MatrixError {
derive_from!(MatrixError,
Hyper, ::hyper::error::Error,
Serde, ::serde_json::Error,
UriError, ::hyper::error::UriError,
UriError, ::hyper::http::uri::InvalidUri,
HttpError, ::http::Error,
InvalidHeaderValue, ::http::header::InvalidHeaderValue,
Io, ::std::io::Error,
Expand Down
78 changes: 47 additions & 31 deletions glitch-in-the-matrix/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
#![warn(missing_docs)]

extern crate serde;
#[macro_use] extern crate serde_json;
extern crate serde_json;
pub extern crate http;
extern crate hyper;
extern crate hyper_openssl;
extern crate failure;
#[macro_use] extern crate failure_derive;
extern crate failure_derive;
extern crate tokio_core;
#[macro_use] extern crate futures;
extern crate futures;
extern crate percent_encoding;
extern crate uuid;
pub extern crate gm_types as types;
Expand Down Expand Up @@ -47,6 +47,7 @@ use std::borrow::Cow;
use uuid::Uuid;
use std::cell::RefCell;
use std::rc::Rc;
use serde_json::json;

#[allow(missing_docs)]
pub type MatrixHyper = Client<HttpsConnector<HttpConnector>>;
Expand All @@ -62,18 +63,29 @@ pub struct MatrixClient {
}
impl MatrixClient {
pub fn new_from_access_token(token: &str, url: &str, hdl: &Handle) -> impl Future<Item = Self, Error = MatrixError> {
let conn = match HttpsConnector::new(4, hdl) {
let conn = match HttpsConnector::new(4) {
Ok(c) => c,
Err(e) => return Either::B(futures::future::err(e.into()))
};
let client = hyper::Client::configure()
.connector(conn)
.build(hdl);
let client = Client::builder()
.build(conn);

let uri: hyper::Uri = match format!("{}/_matrix/client/r0/account/whoami?access_token={}", url, token).parse() {
Ok(u) => u,
Err(e) => return Either::B(futures::future::err(e.into()))
};
let mut req = hyper::Request::new(hyper::Method::Get, uri);

// Build the request
let req = Request::builder()
.method(Method::GET)
.uri(uri)
.body(hyper::Body::empty());
let req = match req {
Ok(r) => r,
Err(e) => return Either::B(futures::future::err(e.into()))
};

// Send request
let resp = client.request(req).map_err(|e| e.into()).and_then(ResponseWrapper::<WhoamiReply>::wrap);
let hdl = hdl.clone();
let url = url.to_string();
Expand All @@ -90,31 +102,40 @@ impl MatrixClient {
}))
}
/// Log in to a Matrix homeserver with a username and password, and return a client object.
///
///
/// ## Parameters
///
/// - `username`: the username of the account to use (NB: not a MXID)
/// - `password`: the password of the account to use
/// - `url`: the URL of the homeserver
/// - `hdl`: Tokio reactor handle
pub fn login_password(username: &str, password: &str, url: &str, hdl: &Handle) -> impl Future<Item = Self, Error = MatrixError> {
let conn = match HttpsConnector::new(4, hdl) {
let conn = match HttpsConnector::new(4) {
Ok(c) => c,
Err(e) => return Either::B(futures::future::err(e.into()))
};
let client = hyper::Client::configure()
.connector(conn)
.build(hdl);
let client = Client::builder()
.build(conn);
let uri: hyper::Uri = match format!("{}/_matrix/client/r0/login", url).parse() {
Ok(u) => u,
Err(e) => return Either::B(futures::future::err(e.into()))
};
let mut req = hyper::Request::new(hyper::Method::Post, uri);
req.set_body(json!({
"type": "m.login.password",
"user": username,
"password": password
}).to_string());

// Build the request
let req = Request::builder()
.method(Method::POST)
.uri(uri)
.body(json!({
"type": "m.login.password",
"user": username,
"password": password
}).to_string().into());
let req = match req {
Ok(r) => r,
Err(e) => return Either::B(futures::future::err(e.into()))
};

// Send request
let resp = client.request(req).map_err(|e| e.into()).and_then(ResponseWrapper::<LoginReply>::wrap);
let hdl = hdl.clone();
let url = url.to_string();
Expand Down Expand Up @@ -146,18 +167,17 @@ impl MatrixClient {
Either::A(self.typed_api_call(req, true))
}
/// (for Application Services) Make a new AS client.
///
///
/// ## Parameters
///
/// - `url`: homeserver URL
/// - `user_id`: user ID to impersonate (can be changed later, using `alter_user_id`)
/// - `as_token`: application service token
/// - `hdl`: Tokio reactor handle
pub fn as_new(url: String, user_id: String, as_token: String, hdl: &Handle) -> MatrixResult<Self> {
let conn = HttpsConnector::new(4, hdl)?;
let client = hyper::Client::configure()
.connector(conn)
.build(hdl);
let conn = HttpsConnector::new(4)?;
let client = hyper::Client::builder()
.build(conn);
let hdl = hdl.clone();
Ok(MatrixClient {
hyper: client,
Expand Down Expand Up @@ -201,14 +221,12 @@ impl MatrixRequestable for Rc<RefCell<MatrixClient>> {
self.borrow().is_as
}
fn send_request(&mut self, req: http::Request<Vec<u8>>) -> Self::SendRequestFuture {
use hyper::server::Service;

let (parts, body) = req.into_parts();
let body = hyper::Body::from(body);
let req = Request::from_parts(parts, body);

MxClientSendRequestFuture {
inner: self.borrow_mut().hyper.call(req.into())
inner: self.borrow_mut().hyper.request(req.into())
}
}
}
Expand All @@ -227,7 +245,7 @@ impl Future for MxClientResponseBodyFuture {
}
/// The `SendRequestFuture` of a `MatrixClient`.
pub struct MxClientSendRequestFuture {
inner: hyper::client::FutureResponse
inner: hyper::client::ResponseFuture
}
impl Future for MxClientSendRequestFuture {
type Item = Response<MxClientResponseBodyFuture>;
Expand Down Expand Up @@ -265,14 +283,12 @@ impl MatrixRequestable for MatrixClient {
self.is_as
}
fn send_request(&mut self, req: http::Request<Vec<u8>>) -> Self::SendRequestFuture {
use hyper::server::Service;

let (parts, body) = req.into_parts();
let body = hyper::Body::from(body);
let req = Request::from_parts(parts, body);

MxClientSendRequestFuture {
inner: self.hyper.call(req.into())
inner: self.hyper.request(req.into())
}
}
}
Expand Down
1 change: 1 addition & 0 deletions glitch-in-the-matrix/src/presence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::request::{MatrixRequest, MatrixRequestable};
use http::Method;
use crate::errors::MatrixError;
use futures::Future;
use serde_json::json;

/// Contains methods relating to `/presence/` endpoints.
pub struct PresenceManagement;
Expand Down
4 changes: 2 additions & 2 deletions glitch-in-the-matrix/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::errors::MatrixError;
pub struct Profile;

impl Profile {
/// Get the displayname of a given user ID. This API may be used to fetch the user's own displayname or
/// to query the name of other users; either locally or on remote homeservers.
/// Get the displayname of a given user ID. This API may be used to fetch the user's own displayname or
/// to query the name of other users; either locally or on remote homeservers.
pub fn get_displayname<R: MatrixRequestable>(rq: &mut R, user_id: &str) -> impl Future<Item = DisplaynameReply, Error = MatrixError> {
MatrixRequest::new_basic(Method::GET, format!("/profile/{}/displayname", user_id))
.send(rq)
Expand Down
6 changes: 3 additions & 3 deletions glitch-in-the-matrix/src/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::errors::{MatrixError, MatrixResult};
use types::replies::BadRequestReply;
use serde_json;
use percent_encoding::{utf8_percent_encode, DEFAULT_ENCODE_SET};
use futures::{self, Future, Poll, Async};
use futures::{self, Future, Poll, Async, try_ready};
use std::marker::PhantomData;

/// Describes the type of a Matrix API.
Expand Down Expand Up @@ -203,7 +203,7 @@ impl<'a, 'b, 'c> MatrixRequest<'a, HashMap<Cow<'b, str>, Cow<'c, str>>> {
/// - `meth` and `endpoint` specified
/// - `body` converted from an iterator over `(T, U)` where T & U implement `Into<Cow<str>>`
/// - `params` set to an empty hashmap
/// - `typ` set to `apis::r0::ClientApi`
/// - `typ` set to `apis::r0::ClientApi`
pub fn new_with_body<S, T, U, V>(meth: Method, endpoint: S, body: V) -> Self
where S: Into<Cow<'a, str>>,
T: Into<Cow<'b, str>>,
Expand All @@ -221,7 +221,7 @@ impl<'a, 'b, 'c> MatrixRequest<'a, HashMap<Cow<'b, str>, Cow<'c, str>>> {
}
}
impl<'a, T> MatrixRequest<'a, T> where T: Serialize {
/// Like `new_with_body`, but takes a serializable object for `body`.
/// Like `new_with_body`, but takes a serializable object for `body`.
pub fn new_with_body_ser<S>(meth: Method, endpoint: S, body: T) -> Self
where S: Into<Cow<'a, str>> {
Self {
Expand Down
7 changes: 3 additions & 4 deletions glitch-in-the-matrix/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use crate::errors::*;
use types::replies::*;
use hyper::{Body, StatusCode};
use hyper::client::Response;
use hyper::{Body, StatusCode, Response};
use serde::de::DeserializeOwned;
use futures::*;
use std::marker::PhantomData;
Expand All @@ -15,9 +14,9 @@ pub struct ResponseWrapper<T> {
_ph: PhantomData<T>,
}
impl<T: DeserializeOwned> ResponseWrapper<T> {
pub fn wrap(r: Response) -> Self {
pub fn wrap(r: Response<Body>) -> Self {
let sc = r.status();
let inner = r.body().concat2();
let inner = r.into_body().concat2();
let _ph = PhantomData;
Self { sc, inner, _ph, }
}
Expand Down

0 comments on commit 7eaa3bf

Please sign in to comment.