Browse Source

Add matrix_client.reqwest_ext module

environments/review-jsparber-h-cxnwl8/deployments/1
Jonas Platte 9 years ago
parent
commit
c42a168675
  1. 2
      src/app.rs
  2. 6
      src/matrix_client/mod.rs
  3. 73
      src/matrix_client/reqwest_ext.rs

2
src/app.rs

@ -78,6 +78,6 @@ impl App {
self.gtk_app.run(args_refs.len() as i32, &args_refs);
// Clean up
self.matrix_client_thread_join_handle.join();
self.matrix_client_thread_join_handle.join().unwrap();
}
}

6
src/matrix_client.rs → src/matrix_client/mod.rs

@ -1,15 +1,17 @@
use gtk;
use reqwest;
use ruma_client_api::r0 as api;
use ruma_client_api as api;
use std::sync::mpsc::Sender;
mod reqwest_ext;
pub fn run_client_main(dispatch_tx: Sender<Box<Fn(&gtk::Builder) + Send>>) {
let client = reqwest::Client::new().unwrap();
let res = client.post("https://matrix.org/_matrix/client/r0/register?kind=guest")
.body("{}")
.send()
.unwrap()
.json::<api::account::register::Response>()
.json::<api::r0::account::register::Response>()
.unwrap();
dispatch_tx.send(box move |builder| {

73
src/matrix_client/reqwest_ext.rs

@ -0,0 +1,73 @@
use reqwest;
use ruma_client_api as api;
pub trait RqwClientExt {
fn matrix_request<E>(&self,
path_params: E::PathParams,
body_params: E::BodyParams)
-> reqwest::Result<MatrixResponse<E>>
where E: api::Endpoint;
// fn matrix_request<E>(&self, body_params: E::BodyParams) -> reqwest::Result<MatrixResponse<E>>
// where E: api::Endpoint<PathParams = ()>
// {
// self.matrix_request((), body_params)
// }
}
impl RqwClientExt for reqwest::Client {
fn matrix_request<E>(&self,
path_params: E::PathParams,
body_params: E::BodyParams)
-> reqwest::Result<MatrixResponse<E>>
where E: api::Endpoint
{
self.request(to_reqwest_method(E::method()),
&E::request_path(path_params))
.json(&body_params)
.send()
.and_then(MatrixResponse::from_rqw_response)
}
}
pub struct MatrixResponse<E: api::Endpoint> {
rqw_response: reqwest::Response,
body: E::Response,
}
impl<E: api::Endpoint> MatrixResponse<E> {
fn from_rqw_response(mut rqw_response: reqwest::Response)
-> reqwest::Result<MatrixResponse<E>> {
rqw_response.json::<E::Response>().map(|body| {
MatrixResponse {
rqw_response: rqw_response,
body: body,
}
})
}
pub fn status(&self) -> &reqwest::StatusCode {
self.rqw_response.status()
}
pub fn headers(&self) -> &reqwest::header::Headers {
self.rqw_response.headers()
}
pub fn version(&self) -> &reqwest::HttpVersion {
self.rqw_response.version()
}
pub fn body(&mut self) -> &E::Response {
&self.body
}
}
fn to_reqwest_method(api_method: api::Method) -> reqwest::Method {
match api_method {
api::Method::Delete => reqwest::Method::Delete,
api::Method::Get => reqwest::Method::Get,
api::Method::Post => reqwest::Method::Post,
api::Method::Put => reqwest::Method::Put,
}
}
Loading…
Cancel
Save