From a422f97480a4c877b90fad63df12c130c79de624 Mon Sep 17 00:00:00 2001 From: Waffle Date: Sat, 28 Aug 2021 14:54:40 +0300 Subject: [PATCH] Do not re-parse default tba url on every request --- src/bot.rs | 42 +++++++++++++++++++++++++++++++----------- src/bot/api_url.rs | 18 ------------------ src/bot/download.rs | 10 ++++++++-- 3 files changed, 39 insertions(+), 31 deletions(-) delete mode 100644 src/bot/api_url.rs diff --git a/src/bot.rs b/src/bot.rs index e5677716..a79c75a7 100644 --- a/src/bot.rs +++ b/src/bot.rs @@ -4,14 +4,12 @@ use reqwest::Client; use serde::{de::DeserializeOwned, Serialize}; use crate::{ - bot::api_url::ApiUrl, net, requests::{MultipartPayload, Payload, ResponseResult}, serde_multipart, }; mod api; -mod api_url; mod download; const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN"; @@ -56,7 +54,7 @@ const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN"; #[derive(Debug, Clone)] pub struct Bot { token: Arc, - api_url: ApiUrl, + api_url: Arc, client: Client, } @@ -93,9 +91,15 @@ impl Bot { where S: Into, { + let token = Into::::into(token).into(); + let api_url = Arc::new( + reqwest::Url::parse(net::TELEGRAM_API_URL) + .expect("Failed to parse default Telegram bot API url"), + ); + Self { - token: Into::>::into(Into::::into(token)), - api_url: ApiUrl::Default, + token, + api_url, client, } } @@ -174,7 +178,7 @@ impl Bot { /// assert_ne!(bot2.api_url().as_str(), "https://example.com/"); /// ``` pub fn set_api_url(mut self, url: reqwest::Url) -> Self { - self.api_url = ApiUrl::Custom(Arc::new(url)); + self.api_url = Arc::new(url); self } } @@ -193,7 +197,7 @@ impl Bot { /// Returns currently used token API url. pub fn api_url(&self) -> reqwest::Url { - self.api_url.get() + reqwest::Url::clone(&*self.api_url) } } @@ -208,14 +212,23 @@ impl Bot { { let client = self.client.clone(); let token = Arc::clone(&self.token); - let api_url = self.api_url.clone(); + let api_url = Arc::clone(&self.api_url); let params = serde_json::to_vec(payload) // this `expect` should be ok since we don't write request those may trigger error here .expect("serialization of request to be infallible"); // async move to capture client&token&api_url¶ms - async move { net::request_json(&client, token.as_ref(), api_url.get(), P::NAME, params).await } + async move { + net::request_json( + &client, + token.as_ref(), + reqwest::Url::clone(&*api_url), + P::NAME, + params, + ) + .await + } } pub(crate) fn execute_multipart

( @@ -228,14 +241,21 @@ impl Bot { { let client = self.client.clone(); let token = Arc::clone(&self.token); - let api_url = self.api_url.clone(); + let api_url = Arc::clone(&self.api_url); let params = serde_multipart::to_form(payload); // async move to capture client&token&api_url¶ms async move { let params = params.await?; - net::request_multipart(&client, token.as_ref(), api_url.get(), P::NAME, params).await + net::request_multipart( + &client, + token.as_ref(), + reqwest::Url::clone(&*api_url), + P::NAME, + params, + ) + .await } } } diff --git a/src/bot/api_url.rs b/src/bot/api_url.rs deleted file mode 100644 index 9db6a826..00000000 --- a/src/bot/api_url.rs +++ /dev/null @@ -1,18 +0,0 @@ -use std::sync::Arc; - -#[derive(Debug, Clone)] -pub(crate) enum ApiUrl { - Default, - Custom(Arc), -} - -impl ApiUrl { - pub(crate) fn get(&self) -> reqwest::Url { - match self { - // FIXME(waffle): parse once - ApiUrl::Default => reqwest::Url::parse(crate::net::TELEGRAM_API_URL) - .expect("failed to parse default url"), - ApiUrl::Custom(url) => (&**url).clone(), - } - } -} diff --git a/src/bot/download.rs b/src/bot/download.rs index 32b19d6e..5a98e488 100644 --- a/src/bot/download.rs +++ b/src/bot/download.rs @@ -22,7 +22,7 @@ impl<'w> Download<'w> for Bot { ) -> Self::Fut { net::download_file( &self.client, - self.api_url.get(), + reqwest::Url::clone(&*self.api_url), &self.token, path, destination, @@ -35,6 +35,12 @@ impl<'w> Download<'w> for Bot { type Stream = BoxStream<'static, Result>; fn download_file_stream(&self, path: &str) -> Self::Stream { - net::download_file_stream(&self.client, self.api_url.get(), &self.token, path).boxed() + net::download_file_stream( + &self.client, + reqwest::Url::clone(&*self.api_url), + &self.token, + path, + ) + .boxed() } }