From 8a2735552e3aba0dad05700be6f6cc718edbde51 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 15:40:08 +0300 Subject: [PATCH 01/11] Make custom client --- src/build/client_mod.hbs | 50 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 46 insertions(+), 4 deletions(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index 94271f3b4..a3f3589fa 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -185,20 +185,62 @@ pub mod client \{ async fn make_request(&self, req: Self::Request) -> Result>; } + pub struct ClientConfiguration { + base_url: String + } + + impl ClientConfiguration { + pub fn new() -> ClientConfiguration { + ClientConfiguration { + base_url: String::from("{base_url | unescaped}") + } + } + + fn base_url(&mut self, url: impl Into) -> &mut Self { + self.base_url = url.into(); + } + } + + impl Default for ClientConfiguration { + fn default() -> ClientConfiguration { + ClientConfiguration::new() + } + } + + pub struct Client { + client: reqwest::Client, + cfg: ClientConfiguration, + }; + + impl Client { + pub fn new() -> Client { + Client { + client: reqwest::Client::new(), + cfg: ClientConfiguration::new() + } + } + } + + impl Default for Client { + fn default() -> Client { + Client::new() + } + } + #[async_trait::async_trait] - impl ApiClient for reqwest::Client \{ + impl ApiClient for Client \{ type Request = reqwest::RequestBuilder; type Response = reqwest::Response; fn request_builder(&self, method: http::Method, rel_path: &str) -> Self::Request \{ - let mut u = String::from("{base_url | unescaped}"); + let mut u = self.cfg.base_url.clone(); u.push_str(rel_path.trim_start_matches('/')); - self.request(method, &u) + self.client.request(method, &u) } async fn make_request(&self, req: Self::Request) -> Result> \{ let req = req.build().map_err(ApiError::Reqwest)?; - let resp = self.execute(req).await.map_err(ApiError::Reqwest)?; + let resp = self.client.execute(req).await.map_err(ApiError::Reqwest)?; Ok(resp) } } From be711fc73d0c206def9be164dcb81e33ae0a0f97 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 16:11:05 +0300 Subject: [PATCH 02/11] add backslashes --- src/build/client_mod.hbs | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index a3f3589fa..4d9f8c2f7 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -185,44 +185,44 @@ pub mod client \{ async fn make_request(&self, req: Self::Request) -> Result>; } - pub struct ClientConfiguration { + pub struct ClientConfiguration \{ base_url: String } - impl ClientConfiguration { - pub fn new() -> ClientConfiguration { - ClientConfiguration { + impl ClientConfiguration \{ + pub fn new() -> ClientConfiguration \{ + ClientConfiguration \{ base_url: String::from("{base_url | unescaped}") } } - fn base_url(&mut self, url: impl Into) -> &mut Self { + fn base_url(&mut self, url: impl Into) -> &mut Self \{ self.base_url = url.into(); } } - impl Default for ClientConfiguration { - fn default() -> ClientConfiguration { + impl Default for ClientConfiguration \{ + fn default() -> ClientConfiguration \{ ClientConfiguration::new() } } - pub struct Client { + pub struct Client \{ client: reqwest::Client, cfg: ClientConfiguration, }; - impl Client { - pub fn new() -> Client { - Client { + impl Client \{ + pub fn new() -> Client \{ + Client \{ client: reqwest::Client::new(), cfg: ClientConfiguration::new() } } } - impl Default for Client { - fn default() -> Client { + impl Default for Client \{ + fn default() -> Client \{ Client::new() } } From 62b1a06132ada455f2db708ebc8d6ef834139b65 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 16:44:08 +0300 Subject: [PATCH 03/11] take cfg as param --- src/build/client_mod.hbs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index 4d9f8c2f7..9580dd2f3 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -213,17 +213,17 @@ pub mod client \{ }; impl Client \{ - pub fn new() -> Client \{ + pub fn new(cfg: ClientConfiguration) -> Client \{ Client \{ client: reqwest::Client::new(), - cfg: ClientConfiguration::new() + cfg } } } impl Default for Client \{ fn default() -> Client \{ - Client::new() + Client::new(ClientConfiguration::new()) } } From f5ed9b14d48620def7c1ee5db60d21bccb951b78 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 17:51:04 +0300 Subject: [PATCH 04/11] remove semicolon --- src/build/client_mod.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index 9580dd2f3..a3a15f8da 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -210,7 +210,7 @@ pub mod client \{ pub struct Client \{ client: reqwest::Client, cfg: ClientConfiguration, - }; + } impl Client \{ pub fn new(cfg: ClientConfiguration) -> Client \{ From 5eb550d4b5cb5cc6260ec62f8a1104704e645df8 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 19:23:00 +0300 Subject: [PATCH 05/11] fix return Self --- src/build/client_mod.hbs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index a3a15f8da..55cca3a18 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -197,7 +197,8 @@ pub mod client \{ } fn base_url(&mut self, url: impl Into) -> &mut Self \{ - self.base_url = url.into(); + self.base_url = url.into(); + self } } From caa783a61c540c6eebba0949242c211793c8944d Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 19:35:44 +0300 Subject: [PATCH 06/11] Fix visibility --- src/build/client_mod.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index 55cca3a18..fc4f04ffe 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -196,7 +196,7 @@ pub mod client \{ } } - fn base_url(&mut self, url: impl Into) -> &mut Self \{ + pub fn base_url(&mut self, url: impl Into) -> &mut Self \{ self.base_url = url.into(); self } From dd0d8e38ab0c6180c498c346aef64fbc1bab022c Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 20:11:57 +0300 Subject: [PATCH 07/11] Add api key support --- src/build/client_mod.hbs | 42 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index fc4f04ffe..2745f11ea 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -185,8 +185,19 @@ pub mod client \{ async fn make_request(&self, req: Self::Request) -> Result>; } + /// Defines api key that will be used for all requests. + #[derive(Clone)] + pub struct ApiKey \{ + /// Key will be sent in this HTTP header + pub header_name: String, + /// Key itself + pub key: String, + } + + #[derive(Clone)] pub struct ClientConfiguration \{ - base_url: String + base_url: String, + api_key: Option } impl ClientConfiguration \{ @@ -196,10 +207,20 @@ pub mod client \{ } } - pub fn base_url(&mut self, url: impl Into) -> &mut Self \{ + pub fn set_base_url(&mut self, url: impl Into) -> &mut Self \{ self.base_url = url.into(); self } + + pub fn set_api_key(&mut self, api_key: ApiKey) -> &mut Self \{ + self.api_key = Some(api_key); + self + } + + pub fn remove_api_key(&mut self) -> &mut Self \{ + self.api_key.take(); + self + } } impl Default for ClientConfiguration \{ @@ -220,6 +241,17 @@ pub mod client \{ cfg } } + + /// Returns currently used configuration + pub fn config(&self) -> &ClientConfiguration \{ + &self.cfg + } + + /// Returns mutable reference to currently used configuration. + /// If you modify it, all subsequent API requests will be affected. + pub fn config_mut(&mut self) -> &mut ClientConfiguration \{ + &mut self.cfg + } } impl Default for Client \{ @@ -236,7 +268,11 @@ pub mod client \{ fn request_builder(&self, method: http::Method, rel_path: &str) -> Self::Request \{ let mut u = self.cfg.base_url.clone(); u.push_str(rel_path.trim_start_matches('/')); - self.client.request(method, &u) + let builder = self.client.request(method, &u); + if let Some(key) = self.cfg.api_key \{ + builder.header(key.header_name, key.key); + } + builder } async fn make_request(&self, req: Self::Request) -> Result> \{ From 822391c8d8c45710c807dfb1075c37b8edf83427 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 20:34:31 +0300 Subject: [PATCH 08/11] fixup --- src/bin/main.rs | 1 - src/build/client_mod.hbs | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/bin/main.rs b/src/bin/main.rs index 2b2977735..f88f09111 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -6,7 +6,6 @@ use paperclip::v2::{ }; use paperclip::PaperClipError; use structopt::StructOpt; -use url::Url; use std::fs::{self, File}; use std::path::PathBuf; diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index 2745f11ea..c7777de23 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -197,7 +197,7 @@ pub mod client \{ #[derive(Clone)] pub struct ClientConfiguration \{ base_url: String, - api_key: Option + api_key: Option } impl ClientConfiguration \{ From 285d6287093f9b48f159ff8e58c5359170793299 Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 20:55:29 +0300 Subject: [PATCH 09/11] fixup --- src/build/client_mod.hbs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index c7777de23..16413b9ed 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -203,7 +203,8 @@ pub mod client \{ impl ClientConfiguration \{ pub fn new() -> ClientConfiguration \{ ClientConfiguration \{ - base_url: String::from("{base_url | unescaped}") + base_url: String::from("{base_url | unescaped}"), + api_key: None, } } @@ -270,7 +271,7 @@ pub mod client \{ u.push_str(rel_path.trim_start_matches('/')); let builder = self.client.request(method, &u); if let Some(key) = self.cfg.api_key \{ - builder.header(key.header_name, key.key); + builder.header(key.header_name.as_str(), key.key.as_str()); } builder } From 7a047301fb46c87172eed1944bdc898daa07403a Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 21:05:01 +0300 Subject: [PATCH 10/11] borrow --- src/build/client_mod.hbs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index 16413b9ed..aeb7a8038 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -270,7 +270,7 @@ pub mod client \{ let mut u = self.cfg.base_url.clone(); u.push_str(rel_path.trim_start_matches('/')); let builder = self.client.request(method, &u); - if let Some(key) = self.cfg.api_key \{ + if let Some(key) = &self.cfg.api_key \{ builder.header(key.header_name.as_str(), key.key.as_str()); } builder From 0d1d34877240884cf19c4da6b23c7e517af67b3f Mon Sep 17 00:00:00 2001 From: Mikail Bagishov Date: Tue, 2 Jun 2020 21:14:38 +0300 Subject: [PATCH 11/11] not consume builder --- src/build/client_mod.hbs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/build/client_mod.hbs b/src/build/client_mod.hbs index aeb7a8038..ba690f4e1 100644 --- a/src/build/client_mod.hbs +++ b/src/build/client_mod.hbs @@ -269,9 +269,9 @@ pub mod client \{ fn request_builder(&self, method: http::Method, rel_path: &str) -> Self::Request \{ let mut u = self.cfg.base_url.clone(); u.push_str(rel_path.trim_start_matches('/')); - let builder = self.client.request(method, &u); + let mut builder = self.client.request(method, &u); if let Some(key) = &self.cfg.api_key \{ - builder.header(key.header_name.as_str(), key.key.as_str()); + builder = builder.header(key.header_name.as_str(), key.key.as_str()); } builder }