mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-09 03:43:22 +01:00
Add ApiUrl
Add `ApiUrl` and also use `Url` instead of `&str` for urls to clean code
This commit is contained in:
parent
fa0e86c547
commit
17b44db578
5 changed files with 60 additions and 28 deletions
17
src/bot/api_url.rs
Normal file
17
src/bot/api_url.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
#[derive(Debug, Clone)]
|
||||
pub(crate) enum ApiUrl {
|
||||
Default,
|
||||
#[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(),
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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,7 +29,7 @@ pub(crate) const TELOXIDE_PROXY: &str = "TELOXIDE_PROXY";
|
|||
#[derive(Debug, Clone)]
|
||||
pub struct Bot {
|
||||
token: Arc<str>,
|
||||
api_url: Option<Arc<str>>,
|
||||
api_url: ApiUrl,
|
||||
client: Client,
|
||||
parse_mode: Option<ParseMode>,
|
||||
}
|
||||
|
@ -103,7 +105,7 @@ impl Bot {
|
|||
{
|
||||
Self {
|
||||
token: Into::<Arc<str>>::into(Into::<String>::into(token)),
|
||||
api_url: None,
|
||||
api_url: ApiUrl::Default,
|
||||
client,
|
||||
parse_mode: None,
|
||||
}
|
||||
|
@ -128,9 +130,7 @@ impl Bot {
|
|||
.expect("serialization of request to be infallible");
|
||||
|
||||
// async move to capture client&token&api_url¶ms
|
||||
async move {
|
||||
net::request_json2(&client, token.as_ref(), api_url.as_deref(), P::NAME, params).await
|
||||
}
|
||||
async move { net::request_json2(&client, token.as_ref(), api_url.get(), P::NAME, params).await }
|
||||
}
|
||||
|
||||
pub(crate) fn execute_multipart<P>(
|
||||
|
@ -150,8 +150,7 @@ impl Bot {
|
|||
// async move to capture client&token&api_url¶ms
|
||||
async move {
|
||||
let params = params.await?;
|
||||
net::request_multipart2(&client, token.as_ref(), api_url.as_deref(), P::NAME, params)
|
||||
.await
|
||||
net::request_multipart2(&client, token.as_ref(), api_url.get(), P::NAME, params).await
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -289,7 +288,7 @@ impl BotBuilder {
|
|||
pub fn build(self) -> Bot {
|
||||
Bot {
|
||||
token: self.token.unwrap_or_else(|| get_env(TELOXIDE_TOKEN)).into(),
|
||||
api_url: None,
|
||||
api_url: ApiUrl::Default,
|
||||
client: self.client.unwrap_or_else(crate::client_from_env),
|
||||
parse_mode: self.parse_mode,
|
||||
}
|
||||
|
|
|
@ -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()?;
|
||||
|
|
|
@ -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"
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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,7 +80,7 @@ where
|
|||
pub async fn request_multipart2<T>(
|
||||
client: &Client,
|
||||
token: &str,
|
||||
api_url: Option<&str>,
|
||||
api_url: reqwest::Url,
|
||||
method_name: &str,
|
||||
params: reqwest::multipart::Form,
|
||||
) -> ResponseResult<T>
|
||||
|
@ -80,7 +88,7 @@ where
|
|||
T: DeserializeOwned,
|
||||
{
|
||||
let response = client
|
||||
.post(&super::method_url(api_url.unwrap_or(TELEGRAM_API_URL), token, method_name))
|
||||
.post(crate::net::method_url(api_url, token, method_name))
|
||||
.multipart(params)
|
||||
.send()
|
||||
.await
|
||||
|
@ -92,7 +100,7 @@ where
|
|||
pub async fn request_json2<T>(
|
||||
client: &Client,
|
||||
token: &str,
|
||||
api_url: Option<&str>,
|
||||
api_url: reqwest::Url,
|
||||
method_name: &str,
|
||||
params: Vec<u8>,
|
||||
) -> ResponseResult<T>
|
||||
|
@ -100,7 +108,7 @@ where
|
|||
T: DeserializeOwned,
|
||||
{
|
||||
let response = client
|
||||
.post(&super::method_url(api_url.unwrap_or(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()
|
||||
|
|
Loading…
Reference in a new issue