Start integrating with ruma-client-api.
This commit is contained in:
parent
7eba14d983
commit
b1ba09a04d
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
/Cargo.lock
|
||||||
|
/target
|
19
Cargo.toml
Normal file
19
Cargo.toml
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
[package]
|
||||||
|
authors = ["Jimmy Cuadra <jimmy@jimmycuadra.com>"]
|
||||||
|
description = "A Matrix client library."
|
||||||
|
documentation = "https://docs.rs/ruma-client"
|
||||||
|
homepage = "https://github.com/ruma/ruma-client"
|
||||||
|
keywords = ["matrix", "chat", "messaging", "ruma"]
|
||||||
|
license = "MIT"
|
||||||
|
name = "ruma-client"
|
||||||
|
readme = "README.md"
|
||||||
|
repository = "https://github.com/ruma/ruma-client"
|
||||||
|
version = "0.1.0"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
hyper = "0.9.14"
|
||||||
|
ruma-client-api = { path = "../ruma-client-api" }
|
||||||
|
ruma-identifiers = "0.6.0"
|
||||||
|
serde = "0.8.21"
|
||||||
|
serde_json = "0.8.4"
|
||||||
|
url = "1.2.4"
|
33
src/error.rs
Normal file
33
src/error.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use hyper::Error as HyperError;
|
||||||
|
use serde_json::Error as SerdeJsonError;
|
||||||
|
use url::ParseError;
|
||||||
|
|
||||||
|
/// An error that occurs during client operations.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum Error {
|
||||||
|
/// An error at the HTTP layer.
|
||||||
|
Hyper(HyperError),
|
||||||
|
/// An error when parsing a string as a URL.
|
||||||
|
Url(ParseError),
|
||||||
|
/// An error when serializing or deserializing a value.
|
||||||
|
SerdeJson(SerdeJsonError)
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<HyperError> for Error {
|
||||||
|
fn from(error: HyperError) -> Error {
|
||||||
|
Error::Hyper(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<ParseError> for Error {
|
||||||
|
fn from(error: ParseError) -> Error {
|
||||||
|
Error::Url(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<SerdeJsonError> for Error {
|
||||||
|
fn from(error: SerdeJsonError) -> Error {
|
||||||
|
Error::SerdeJson(error)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
76
src/lib.rs
Normal file
76
src/lib.rs
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
//! Crate ruma_client is a [Matrix](https://matrix.org/) client library.
|
||||||
|
|
||||||
|
#![feature(try_from)]
|
||||||
|
#![deny(missing_debug_implementations)]
|
||||||
|
#![deny(missing_docs)]
|
||||||
|
|
||||||
|
extern crate hyper;
|
||||||
|
extern crate ruma_client_api;
|
||||||
|
extern crate ruma_identifiers;
|
||||||
|
extern crate serde;
|
||||||
|
extern crate serde_json;
|
||||||
|
extern crate url;
|
||||||
|
|
||||||
|
use std::convert::TryInto;
|
||||||
|
|
||||||
|
use hyper::client::{Client as Hyper, IntoUrl};
|
||||||
|
use hyper::method::Method as HyperMethod;
|
||||||
|
use ruma_client_api::{Endpoint, Method, supported_versions};
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
pub use error::Error;
|
||||||
|
pub use session::Session;
|
||||||
|
pub use response::Response;
|
||||||
|
|
||||||
|
mod error;
|
||||||
|
mod response;
|
||||||
|
mod session;
|
||||||
|
|
||||||
|
/// A client for the Matrix client-server API.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Client {
|
||||||
|
homeserver_url: Url,
|
||||||
|
hyper: Hyper,
|
||||||
|
session: Option<Session>,
|
||||||
|
}
|
||||||
|
|
||||||
|
trait IntoHyperMethod {
|
||||||
|
fn into_hyper(self) -> HyperMethod;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl IntoHyperMethod for Method {
|
||||||
|
fn into_hyper(self) -> HyperMethod {
|
||||||
|
match self {
|
||||||
|
Method::Delete => HyperMethod::Delete,
|
||||||
|
Method::Get => HyperMethod::Get,
|
||||||
|
Method::Put => HyperMethod::Put,
|
||||||
|
Method::Post => HyperMethod::Post,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Client {
|
||||||
|
/// Creates a new client for making requests to the given homeserver.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
///
|
||||||
|
/// Returns an error if the given homserver URL cannot be parsed as a URL.
|
||||||
|
pub fn new<U>(homeserver_url: U) -> Result<Self, Error> where U: IntoUrl {
|
||||||
|
Ok(Client {
|
||||||
|
homeserver_url: homeserver_url.into_url()?,
|
||||||
|
hyper: Hyper::new(),
|
||||||
|
session: None,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the versions of the Matrix client-server specification supported by the homeserver.
|
||||||
|
pub fn get_supported_versions(&self)
|
||||||
|
-> Result<Response<<supported_versions::Endpoint as Endpoint>::Response>, Error> {
|
||||||
|
let response = self.hyper.request(
|
||||||
|
supported_versions::Endpoint::method().into_hyper(),
|
||||||
|
self.homeserver_url.join(&supported_versions::Endpoint::request_path(()))?,
|
||||||
|
).send()?;
|
||||||
|
|
||||||
|
Ok(response.try_into()?)
|
||||||
|
}
|
||||||
|
}
|
41
src/response.rs
Normal file
41
src/response.rs
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
use std::convert::TryFrom;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
use hyper::client::Response as HyperResponse;
|
||||||
|
use hyper::header::Headers;
|
||||||
|
use hyper::status::StatusCode;
|
||||||
|
use hyper::version::HttpVersion;
|
||||||
|
use serde::Deserialize;
|
||||||
|
use serde_json::from_reader;
|
||||||
|
use url::Url;
|
||||||
|
|
||||||
|
use Error;
|
||||||
|
|
||||||
|
/// A response from a Matrix homeserver.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Response<T> where T: Debug + Deserialize {
|
||||||
|
/// The response from the Matrix API.
|
||||||
|
pub data: T,
|
||||||
|
/// The HTTP response code.
|
||||||
|
pub status: StatusCode,
|
||||||
|
/// The HTTP response headers.
|
||||||
|
pub headers: Headers,
|
||||||
|
/// The HTTP version.
|
||||||
|
pub http_version: HttpVersion,
|
||||||
|
/// The URL that was requested.
|
||||||
|
pub url: Url,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> TryFrom<HyperResponse> for Response<T> where T: Debug + Deserialize {
|
||||||
|
type Err = Error;
|
||||||
|
|
||||||
|
fn try_from(hyper_response: HyperResponse) -> Result<Self, Self::Err> {
|
||||||
|
Ok(Response {
|
||||||
|
status: hyper_response.status,
|
||||||
|
headers: hyper_response.headers.clone(),
|
||||||
|
http_version: hyper_response.version,
|
||||||
|
url: hyper_response.url.clone(),
|
||||||
|
data: from_reader(hyper_response)?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
10
src/session.rs
Normal file
10
src/session.rs
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
use ruma_identifiers::UserId;
|
||||||
|
use url::Host;
|
||||||
|
|
||||||
|
/// An active user session with a Matrix homeserver, allowing authenticated requests.
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Session {
|
||||||
|
access_token: String,
|
||||||
|
homeserver: Host,
|
||||||
|
user_id: UserId,
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user