diff --git a/src/bot/mod.rs b/src/bot/mod.rs index a35e11c8..433a6cab 100644 --- a/src/bot/mod.rs +++ b/src/bot/mod.rs @@ -20,10 +20,7 @@ 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"), - ) + Self::from_env_with_client(Client::new()) } /// Creates a new `Bot` with the `TELOXIDE_TOKEN` environmental variable (a @@ -35,7 +32,7 @@ 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") + &std::env::var("TELOXIDE_TOKEN") .expect("Cannot get the TELOXIDE_TOKEN env variable"), client, ) 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;