Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Add internal ability to change API url #26

Merged
merged 3 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/bot/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ pub(crate) const TELOXIDE_PROXY: &str = "TELOXIDE_PROXY";
#[derive(Debug, Clone)]
pub struct Bot {
token: Arc<str>,
api_url: Option<Arc<str>>,
WaffleLapkin marked this conversation as resolved.
Show resolved Hide resolved
client: Client,
parse_mode: Option<ParseMode>,
}
Expand Down Expand Up @@ -102,6 +103,7 @@ impl Bot {
{
Self {
token: Into::<Arc<str>>::into(Into::<String>::into(token)),
api_url: None,
client,
parse_mode: None,
}
Expand All @@ -119,13 +121,16 @@ 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.as_deref(), P::NAME, params).await
}
}

pub(crate) fn execute_multipart<P>(
Expand All @@ -138,13 +143,15 @@ 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.as_deref(), P::NAME, params)
.await
}
}
}
Expand Down Expand Up @@ -281,8 +288,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: None,
client: self.client.unwrap_or_else(crate::client_from_env),
parse_mode: self.parse_mode,
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/net/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,15 @@ where
pub async fn request_multipart2<T>(
client: &Client,
token: &str,
api_url: Option<&str>,
method_name: &str,
params: reqwest::multipart::Form,
) -> ResponseResult<T>
where
T: DeserializeOwned,
{
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.post(&super::method_url(api_url.unwrap_or(TELEGRAM_API_URL), token, method_name))
.multipart(params)
.send()
.await
Expand All @@ -91,14 +92,15 @@ where
pub async fn request_json2<T>(
client: &Client,
token: &str,
api_url: Option<&str>,
method_name: &str,
params: Vec<u8>,
) -> ResponseResult<T>
where
T: DeserializeOwned,
{
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.post(&super::method_url(api_url.unwrap_or(TELEGRAM_API_URL), token, method_name))
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
.body(params)
.send()
Expand Down