Merge pull request #26 from teloxide/intrl_api_url_chage

Add internal ability to change API url
This commit is contained in:
Temirkhan Myrzamadi 2020-11-21 04:01:28 +06:00 committed by GitHub
commit b4e0a355c3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 23 deletions

18
src/bot/api_url.rs Normal file
View file

@ -0,0 +1,18 @@
#[derive(Debug, Clone)]
pub(crate) enum ApiUrl {
Default,
// FIXME: remove #[allow] when we use this variant
#[allow(dead_code)]
Custom(reqwest::Url),
}
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(),
}
}
}

View file

@ -7,6 +7,7 @@ use reqwest::{
use serde::{de::DeserializeOwned, Serialize};
use crate::{
bot::api_url::ApiUrl,
net,
requests::{Payload, ResponseResult},
serde_multipart,
@ -14,6 +15,7 @@ use crate::{
};
mod api;
mod api_url;
mod download;
pub(crate) const TELOXIDE_TOKEN: &str = "TELOXIDE_TOKEN";
@ -27,6 +29,7 @@ pub(crate) const TELOXIDE_PROXY: &str = "TELOXIDE_PROXY";
#[derive(Debug, Clone)]
pub struct Bot {
token: Arc<str>,
api_url: ApiUrl,
client: Client,
parse_mode: Option<ParseMode>,
}
@ -102,6 +105,7 @@ impl Bot {
{
Self {
token: Into::<Arc<str>>::into(Into::<String>::into(token)),
api_url: ApiUrl::Default,
client,
parse_mode: None,
}
@ -119,13 +123,14 @@ impl Bot {
{
let client = self.client.clone();
let token = Arc::clone(&self.token);
let api_url = self.api_url.clone();
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
async move { net::request_json2(&client, token.as_ref(), P::NAME, params).await }
// async move to capture client&token&api_url&params
async move { net::request_json2(&client, token.as_ref(), api_url.get(), P::NAME, params).await }
}
pub(crate) fn execute_multipart<P>(
@ -138,13 +143,14 @@ impl Bot {
{
let client = self.client.clone();
let token = Arc::clone(&self.token);
let api_url = self.api_url.clone();
let params = serde_multipart::to_form(payload);
// async move to capture client&token&params
// async move to capture client&token&api_url&params
async move {
let params = params.await?;
net::request_multipart2(&client, token.as_ref(), P::NAME, params).await
net::request_multipart2(&client, token.as_ref(), api_url.get(), P::NAME, params).await
}
}
}
@ -281,8 +287,9 @@ impl BotBuilder {
#[must_use]
pub fn build(self) -> Bot {
Bot {
client: self.client.unwrap_or_else(crate::client_from_env),
token: self.token.unwrap_or_else(|| get_env(TELOXIDE_TOKEN)).into(),
api_url: ApiUrl::Default,
client: self.client.unwrap_or_else(crate::client_from_env),
parse_mode: self.parse_mode,
}
}

View file

@ -3,8 +3,6 @@ use tokio::io::{AsyncWrite, AsyncWriteExt};
use crate::errors::DownloadError;
use super::TELEGRAM_API_URL;
pub async fn download_file<D>(
client: &Client,
token: &str,
@ -15,7 +13,11 @@ where
D: AsyncWrite + Unpin,
{
let mut res = client
.get(&super::file_url(TELEGRAM_API_URL, token, path))
.get(crate::net::file_url(
reqwest::Url::parse(crate::net::TELEGRAM_API_URL).expect("failed to parse default url"),
token,
path,
))
.send()
.await?
.error_for_status()?;
@ -33,7 +35,11 @@ pub async fn download_file_stream(
path: &str,
) -> Result<impl futures::Stream<Item = reqwest::Result<bytes::Bytes>>, reqwest::Error> {
let res = client
.get(&super::file_url(TELEGRAM_API_URL, token, path))
.get(crate::net::file_url(
reqwest::Url::parse(crate::net::TELEGRAM_API_URL).expect("failed to parse default url"),
token,
path,
))
.send()
.await?
.error_for_status()?;

View file

@ -8,36 +8,38 @@ mod download;
mod request;
mod telegram_response;
const TELEGRAM_API_URL: &str = "https://api.telegram.org";
pub(crate) const TELEGRAM_API_URL: &str = "https://api.telegram.org";
/// Creates URL for making HTTPS requests. See the [Telegram documentation].
///
/// [Telegram documentation]: https://core.telegram.org/bots/api#making-requests
fn method_url(base: &str, token: &str, method_name: &str) -> String {
format!("{url}/bot{token}/{method}", url = base, token = token, method = method_name)
fn method_url(base: reqwest::Url, token: &str, method_name: &str) -> reqwest::Url {
base.join(&format!("/bot{token}/{method}", token = token, method = method_name))
.expect("failed to format url")
}
/// Creates URL for downloading a file. See the [Telegram documentation].
///
/// [Telegram documentation]: https://core.telegram.org/bots/api#file
fn file_url(base: &str, token: &str, file_path: &str) -> String {
format!("{url}/file/bot{token}/{file}", url = base, token = token, file = file_path,)
fn file_url(base: reqwest::Url, token: &str, file_path: &str) -> reqwest::Url {
base.join(&format!("file/bot{token}/{file}", token = token, file = file_path))
.expect("failed to format url")
}
#[cfg(test)]
mod tests {
use super::*;
use crate::net::*;
#[test]
fn method_url_test() {
let url = method_url(
TELEGRAM_API_URL,
reqwest::Url::parse(TELEGRAM_API_URL).unwrap(),
"535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao",
"methodName",
);
assert_eq!(
url,
url.as_str(),
"https://api.telegram.org/bot535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao/methodName"
);
}
@ -45,13 +47,13 @@ mod tests {
#[test]
fn file_url_test() {
let url = file_url(
TELEGRAM_API_URL,
reqwest::Url::parse(TELEGRAM_API_URL).unwrap(),
"535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao",
"AgADAgADyqoxG2g8aEsu_KjjVsGF4-zetw8ABAEAAwIAA20AA_8QAwABFgQ",
);
assert_eq!(
url,
url.as_str(),
"https://api.telegram.org/file/bot535362388:AAF7-g0gYncWnm5IyfZlpPRqRRv6kNAGlao/AgADAgADyqoxG2g8aEsu_KjjVsGF4-zetw8ABAEAAwIAA20AA_8QAwABFgQ"
);
}

View file

@ -35,7 +35,11 @@ where
};
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.post(crate::net::method_url(
reqwest::Url::parse(TELEGRAM_API_URL).expect("failed to parse default url"),
token,
method_name,
))
.multipart(form)
.send()
.await
@ -55,7 +59,11 @@ where
R: DeserializeOwned,
{
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.post(crate::net::method_url(
reqwest::Url::parse(TELEGRAM_API_URL).expect("failed to parse default url"),
token,
method_name,
))
.json(params)
.send()
.await
@ -72,6 +80,7 @@ where
pub async fn request_multipart2<T>(
client: &Client,
token: &str,
api_url: reqwest::Url,
method_name: &str,
params: reqwest::multipart::Form,
) -> ResponseResult<T>
@ -79,7 +88,7 @@ where
T: DeserializeOwned,
{
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.post(crate::net::method_url(api_url, token, method_name))
.multipart(params)
.send()
.await
@ -91,6 +100,7 @@ where
pub async fn request_json2<T>(
client: &Client,
token: &str,
api_url: reqwest::Url,
method_name: &str,
params: Vec<u8>,
) -> ResponseResult<T>
@ -98,7 +108,7 @@ where
T: DeserializeOwned,
{
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.post(crate::net::method_url(api_url, token, method_name))
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
.body(params)
.send()