Update dependencies, use published ruma crates, add new Client constructors.
This commit is contained in:
parent
b6f0d8d8b4
commit
86e837d26f
32
Cargo.toml
32
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"]
|
||||
|
14
src/error.rs
14
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<HyperError> for Error {
|
||||
@ -33,9 +33,9 @@ impl From<ParseError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RumaClientApiError> for Error {
|
||||
fn from(error: RumaClientApiError) -> Error {
|
||||
Error::RumaClientApi(error)
|
||||
impl From<RumaApiError> for Error {
|
||||
fn from(error: RumaApiError) -> Error {
|
||||
Error::RumaApi(error)
|
||||
}
|
||||
}
|
||||
|
||||
|
66
src/lib.rs
66
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<C> where C: Connect {
|
||||
homeserver_url: Url,
|
||||
hyper: HyperClient<HttpConnector>,
|
||||
hyper: Rc<HyperClient<C>>,
|
||||
session: Option<Session>,
|
||||
}
|
||||
|
||||
impl Client {
|
||||
/// Creates a new client for making requests to the given homeserver.
|
||||
impl Client<HttpConnector> {
|
||||
/// 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<HttpsConnector<HttpConnector>> {
|
||||
/// Creates a new client for making HTTPS requests to the given homeserver.
|
||||
pub fn https(handle: &Handle, homeserver_url: Url) -> Result<Self, NativeTlsError> {
|
||||
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<C> Client<C> 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<C>, 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<E>(&self, request: <E as Endpoint>::Request)
|
||||
-> impl Future<Item = E::Response, Error = Error>
|
||||
pub(crate) fn request<'a, E>(&'a self, request: <E as Endpoint>::Request)
|
||||
-> impl Future<Item = E::Response, Error = Error> + 'a
|
||||
where E: Endpoint,
|
||||
E::Response: 'static,
|
||||
Error: From<<E::Request as TryInto<HyperRequest>>::Error>,
|
||||
Error: From<<E::Response as TryFrom<HyperResponse>>::Error> {
|
||||
let cloned_hyper = self.hyper.clone();
|
||||
|
||||
<E as Endpoint>::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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user