diff --git a/src/app.rs b/src/app.rs index 1626f49b..9057e6f6 100644 --- a/src/app.rs +++ b/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(); } } \ No newline at end of file diff --git a/src/matrix_client.rs b/src/matrix_client/mod.rs similarity index 89% rename from src/matrix_client.rs rename to src/matrix_client/mod.rs index f762798d..7f4185b6 100644 --- a/src/matrix_client.rs +++ b/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>) { let client = reqwest::Client::new().unwrap(); let res = client.post("https://matrix.org/_matrix/client/r0/register?kind=guest") .body("{}") .send() .unwrap() - .json::() + .json::() .unwrap(); dispatch_tx.send(box move |builder| { diff --git a/src/matrix_client/reqwest_ext.rs b/src/matrix_client/reqwest_ext.rs new file mode 100644 index 00000000..4d8572d7 --- /dev/null +++ b/src/matrix_client/reqwest_ext.rs @@ -0,0 +1,73 @@ +use reqwest; +use ruma_client_api as api; + +pub trait RqwClientExt { + fn matrix_request(&self, + path_params: E::PathParams, + body_params: E::BodyParams) + -> reqwest::Result> + where E: api::Endpoint; + + // fn matrix_request(&self, body_params: E::BodyParams) -> reqwest::Result> + // where E: api::Endpoint + // { + // self.matrix_request((), body_params) + // } +} + +impl RqwClientExt for reqwest::Client { + fn matrix_request(&self, + path_params: E::PathParams, + body_params: E::BodyParams) + -> reqwest::Result> + 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 { + rqw_response: reqwest::Response, + body: E::Response, +} + +impl MatrixResponse { + fn from_rqw_response(mut rqw_response: reqwest::Response) + -> reqwest::Result> { + rqw_response.json::().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, + } +} \ No newline at end of file