From bdbc22677733491d3726943ad96865eec1a0894b Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Mon, 24 Feb 2020 20:20:15 +0300 Subject: [PATCH 1/4] Added `$TELOXIDE_PROXY` to `Bot::from_env` --- src/bot/mod.rs | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/bot/mod.rs b/src/bot/mod.rs index a35e11c8..a6177ffe 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -20,10 +20,20 @@ impl Bot { /// /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html pub fn from_env() -> Arc { - Self::new( - std::env::var("TELOXIDE_TOKEN") - .expect("Cannot get the TELOXIDE_TOKEN env variable"), - ) + match std::env::var_os("TELOXIDE_PROXY") { + Some(proxy) => Self::from_env_with_client( + Client::builder() + .proxy( + reqwest::Proxy::all( + &proxy.to_string_lossy().to_string(), + ) + .expect("creating reqwest::Proxy"), + ) + .build() + .expect("creating reqwest::Client"), + ), + None => Self::from_env_with_client(Client::new()), + } } /// Creates a new `Bot` with the `TELOXIDE_TOKEN` environmental variable (a @@ -35,8 +45,10 @@ impl Bot { /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html pub fn from_env_with_client(client: Client) -> Arc { Self::with_client( - std::env::var("TELOXIDE_TOKEN") - .expect("Cannot get the TELOXIDE_TOKEN env variable"), + &std::env::var_os("TELOXIDE_TOKEN") + .expect("Cannot get the TELOXIDE_TOKEN env variable") + .to_string_lossy() + .to_string(), client, ) } From ddefd9e8a6f0f3e8e5625e17aaeedeb4ae713816 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Mon, 24 Feb 2020 20:36:12 +0300 Subject: [PATCH 2/4] `Cow::to_string()` -> `&*Cow` --- src/bot/mod.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/bot/mod.rs b/src/bot/mod.rs index a6177ffe..4e37f88b 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -25,7 +25,7 @@ impl Bot { Client::builder() .proxy( reqwest::Proxy::all( - &proxy.to_string_lossy().to_string(), + &*proxy.to_string_lossy(), ) .expect("creating reqwest::Proxy"), ) @@ -45,10 +45,9 @@ impl Bot { /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html pub fn from_env_with_client(client: Client) -> Arc { Self::with_client( - &std::env::var_os("TELOXIDE_TOKEN") + &*std::env::var_os("TELOXIDE_TOKEN") .expect("Cannot get the TELOXIDE_TOKEN env variable") - .to_string_lossy() - .to_string(), + .to_string_lossy(), client, ) } From a326860d4b908a655d2915e3eb983ac9c47e40c2 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Mon, 24 Feb 2020 20:43:45 +0300 Subject: [PATCH 3/4] `var_os()` -> `var()` (back) --- src/bot/mod.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 4e37f88b..991e04ce 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -20,12 +20,12 @@ impl Bot { /// /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html pub fn from_env() -> Arc { - match std::env::var_os("TELOXIDE_PROXY") { + match std::env::var("TELOXIDE_PROXY").ok() { Some(proxy) => Self::from_env_with_client( Client::builder() .proxy( reqwest::Proxy::all( - &*proxy.to_string_lossy(), + &proxy, ) .expect("creating reqwest::Proxy"), ) @@ -45,9 +45,8 @@ impl Bot { /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html pub fn from_env_with_client(client: Client) -> Arc { Self::with_client( - &*std::env::var_os("TELOXIDE_TOKEN") - .expect("Cannot get the TELOXIDE_TOKEN env variable") - .to_string_lossy(), + &std::env::var("TELOXIDE_TOKEN") + .expect("Cannot get the TELOXIDE_TOKEN env variable"), client, ) } From 7aea85a187e85fe9d3fc355acd035b1b40bb86a8 Mon Sep 17 00:00:00 2001 From: Mr-Andersen Date: Mon, 24 Feb 2020 20:49:41 +0300 Subject: [PATCH 4/4] created `client_from_env()` in utils --- src/bot/mod.rs | 15 +-------------- src/utils/client_from_env.rs | 11 +++++++++++ src/utils/mod.rs | 1 + 3 files changed, 13 insertions(+), 14 deletions(-) create mode 100644 src/utils/client_from_env.rs diff --git a/src/bot/mod.rs b/src/bot/mod.rs index 991e04ce..433a6cab 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -20,20 +20,7 @@ impl Bot { /// /// [`reqwest::Client`]: https://docs.rs/reqwest/0.10.1/reqwest/struct.Client.html pub fn from_env() -> Arc { - match std::env::var("TELOXIDE_PROXY").ok() { - Some(proxy) => Self::from_env_with_client( - Client::builder() - .proxy( - reqwest::Proxy::all( - &proxy, - ) - .expect("creating reqwest::Proxy"), - ) - .build() - .expect("creating reqwest::Client"), - ), - None => Self::from_env_with_client(Client::new()), - } + Self::from_env_with_client(Client::new()) } /// Creates a new `Bot` with the `TELOXIDE_TOKEN` environmental variable (a diff --git a/src/utils/client_from_env.rs b/src/utils/client_from_env.rs new file mode 100644 index 00000000..b718e4ef --- /dev/null +++ b/src/utils/client_from_env.rs @@ -0,0 +1,11 @@ +pub fn client_from_env() -> reqwest::Client { + use reqwest::{Client, Proxy}; + + match std::env::var("TELOXIDE_PROXY").ok() { + Some(proxy) => Client::builder() + .proxy(Proxy::all(&proxy).expect("creating reqwest::Proxy")) + .build() + .expect("creating reqwest::Client"), + None => Client::new(), + } +} diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 15ff9ad4..2c4c1421 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,5 +1,6 @@ //! Some useful utilities. +pub mod client_from_env; pub mod command; pub mod html; pub mod markdown;