From 86e837d26fb10fbe76976994935e38b46453bc4d Mon Sep 17 00:00:00 2001 From: Jimmy Cuadra Date: Fri, 7 Jul 2017 23:19:12 -0700 Subject: [PATCH] Update dependencies, use published ruma crates, add new Client constructors. --- Cargo.toml | 32 ++++++++++++++----------- src/error.rs | 14 +++++------ src/lib.rs | 66 +++++++++++++++++++++++++++++++++++++--------------- 3 files changed, 72 insertions(+), 40 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 486c7300..9f1a1619 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,21 +11,25 @@ repository = "https://github.com/ruma/ruma-client" version = "0.1.0" [dependencies] -futures = "0.1.13" +futures = "0.1.14" +hyper = "0.11.1" +ruma-api = "0.4.0" +ruma-client-api = "0.1.0" ruma-identifiers = "0.11.0" -serde = "1.0.2" -serde_json = "1.0.1" -serde_urlencoded = "0.4.3" -tokio-core = "0.1.6" -url = "1.4.0" +serde = "1.0.9" +serde_json = "1.0.2" +serde_urlencoded = "0.5.1" +tokio-core = "0.1.8" +url = "1.5.1" -[dependencies.hyper] -git = "https://github.com/hyperium/hyper" -rev = "fed04dfb58e19b408322d4e5ca7474871e848a35" +[dependencies.hyper-tls] +optional = true +version = "0.1.2" -[dependencies.ruma-api] -git = "https://github.com/ruma/ruma-api" +[dependencies.native-tls] +optional = true +version = "0.1.4" -[dependencies.ruma-client-api] -git = "https://github.com/ruma/ruma-client-api" -branch = "manual" +[features] +default = ["tls"] +tls = ["hyper-tls", "native-tls"] diff --git a/src/error.rs b/src/error.rs index b2fecb50..1cc80fb7 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ use hyper::Error as HyperError; -use ruma_client_api::Error as RumaClientApiError; +use ruma_api::Error as RumaApiError; use serde_json::Error as SerdeJsonError; use serde_urlencoded::ser::Error as SerdeUrlEncodedSerializeError; use url::ParseError; @@ -7,18 +7,18 @@ use url::ParseError; /// An error that occurs during client operations. #[derive(Debug)] pub enum Error { + /// Queried endpoint requires authentication but was called on an anonymous client + AuthenticationRequired, /// An error at the HTTP layer. Hyper(HyperError), /// An error when parsing a string as a URL. Url(ParseError), /// An error converting between ruma_client_api types and Hyper types. - RumaClientApi(RumaClientApiError), + RumaApi(RumaApiError), /// An error when serializing or deserializing a JSON value. SerdeJson(SerdeJsonError), /// An error when serializing a query string value. SerdeUrlEncodedSerialize(SerdeUrlEncodedSerializeError), - /// Queried endpoint requires authentication but was called on an anonymous client - AuthenticationRequired, } impl From for Error { @@ -33,9 +33,9 @@ impl From for Error { } } -impl From for Error { - fn from(error: RumaClientApiError) -> Error { - Error::RumaClientApi(error) +impl From for Error { + fn from(error: RumaApiError) -> Error { + Error::RumaApi(error) } } diff --git a/src/lib.rs b/src/lib.rs index 44fbb3b8..d8450765 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,8 +6,10 @@ extern crate futures; extern crate hyper; +#[cfg(feature = "tls")] extern crate hyper_tls; +#[cfg(feature = "tls")] extern crate native_tls; extern crate ruma_api; -pub extern crate ruma_client_api; +extern crate ruma_client_api; extern crate ruma_identifiers; extern crate serde; extern crate serde_json; @@ -15,11 +17,14 @@ extern crate serde_urlencoded; extern crate tokio_core; extern crate url; -use std::convert::{TryFrom, TryInto}; +use std::convert::TryInto; +use std::rc::Rc; -use futures::{Future, IntoFuture}; -use hyper::{Client as HyperClient, Request as HyperRequest, Response as HyperResponse}; -use hyper::client::HttpConnector; +use futures::future::{Future, FutureFrom, IntoFuture}; +use hyper::{Client as HyperClient}; +use hyper::client::{Connect, HttpConnector}; +#[cfg(feature = "hyper-tls")] use hyper_tls::HttpsConnector; +#[cfg(feature = "hyper-tls")] use native_tls::Error as NativeTlsError; use ruma_api::Endpoint; use tokio_core::reactor::Handle; use url::Url; @@ -32,40 +37,63 @@ mod session; /// A client for the Matrix client-server API. #[derive(Debug)] -pub struct Client { +pub struct Client where C: Connect { homeserver_url: Url, - hyper: HyperClient, + hyper: Rc>, session: Option, } -impl Client { - /// Creates a new client for making requests to the given homeserver. +impl Client { + /// Creates a new client for making HTTP requests to the given homeserver. pub fn new(handle: &Handle, homeserver_url: Url) -> Self { Client { homeserver_url, - hyper: HyperClient::configure().keep_alive(true).build(handle), + hyper: Rc::new(HyperClient::configure().keep_alive(true).build(handle)), + session: None, + } + } +} + +#[cfg(feature = "tls")] +impl Client> { + /// Creates a new client for making HTTPS requests to the given homeserver. + pub fn https(handle: &Handle, homeserver_url: Url) -> Result { + let connector = HttpsConnector::new(4, handle)?; + + Ok(Client { + homeserver_url, + hyper: Rc::new(HyperClient::configure().connector(connector).keep_alive(true).build(handle)), + session: None, + }) + } +} + +impl Client where C: Connect { + /// Creates a new client using the given `hyper::Client`. + /// + /// This allows the user to configure the details of HTTP as desired. + pub fn custom(hyper_client: HyperClient, homeserver_url: Url) -> Self { + Client { + homeserver_url, + hyper: Rc::new(hyper_client), session: None, } } /// Makes a request to a Matrix API endpoint. - pub fn request(&self, request: ::Request) - -> impl Future + pub(crate) fn request<'a, E>(&'a self, request: ::Request) + -> impl Future + 'a where E: Endpoint, - E::Response: 'static, - Error: From<>::Error>, - Error: From<>::Error> { - let cloned_hyper = self.hyper.clone(); - + ::Response: 'a { request .try_into() .map_err(Error::from) .into_future() .and_then(move |hyper_request| { - cloned_hyper.request(hyper_request).map_err(Error::from) + self.hyper.clone().request(hyper_request).map_err(Error::from) }) .and_then(|hyper_response| { - hyper_response.try_into().map_err(Error::from) + E::Response::future_from(hyper_response).map_err(Error::from) }) } }