From d0c777ff9a053782b3f94d40e4d64a2ed4f4a91b Mon Sep 17 00:00:00 2001 From: Andreas Studer Date: Sun, 19 Apr 2020 14:38:00 +0200 Subject: [PATCH] Add request_with_url_params method This method allows a client to add any URL parameters when doing an API call. This can be used by application services to add URL parameters like user_id. --- src/lib.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index ff1c39af..edeac004 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -111,6 +111,7 @@ use ruma_api::{ Endpoint, Outgoing, }; use ruma_identifiers::DeviceId; +use std::collections::BTreeMap; use url::Url; pub use ruma_client_api as api; @@ -341,6 +342,22 @@ where ) -> impl Future::Incoming, Error>> // We need to duplicate Endpoint's where clauses because the compiler is not smart enough yet. // See https://github.com/rust-lang/rust/issues/54149 + where + Request::Incoming: TryFrom>, Error = FromHttpRequestError>, + ::Incoming: + TryFrom>, Error = FromHttpResponseError>, + { + self.request_with_url_params(request, None) + } + + /// Makes a request to a Matrix API endpoint including additional URL parameters. + pub fn request_with_url_params>( + &self, + request: Request, + params: Option>, + ) -> impl Future::Incoming, Error>> + // We need to duplicate Endpoint's where clauses because the compiler is not smart enough yet. + // See https://github.com/rust-lang/rust/issues/54149 where Request::Incoming: TryFrom>, Error = FromHttpRequestError>, ::Incoming: @@ -359,6 +376,12 @@ where url.set_path(uri.path()); url.set_query(uri.query()); + if let Some(params) = params { + for (key, value) in params { + url.query_pairs_mut().append_pair(&key, &value); + } + } + if Request::METADATA.requires_authentication { if let Some(ref session) = *client.session.lock().unwrap() { url.query_pairs_mut()