use std::{any::TypeId, time::Duration};
use reqwest::{
header::{HeaderValue, CONTENT_TYPE},
Client, Response,
};
use serde::de::DeserializeOwned;
use crate::{net::TelegramResponse, requests::ResponseResult, RequestError};
const DELAY_ON_SERVER_ERROR: Duration = Duration::from_secs(10);
pub async fn request_multipart<T>(
client: &Client,
token: &str,
api_url: reqwest::Url,
method_name: &str,
params: reqwest::multipart::Form,
timeout_hint: Option<Duration>,
) -> ResponseResult<T>
where
T: DeserializeOwned + 'static,
{
let mut request = client
.post(crate::net::method_url(api_url, token, method_name))
.multipart(params)
.build()?;
if let Some(timeout) = timeout_hint {
*request.timeout_mut() = Some(timeout.saturating_add(super::REQUEST_TIMEOUT));
}
let response = client.execute(request).await?;
process_response(response).await
}
pub async fn request_json<T>(
client: &Client,
token: &str,
api_url: reqwest::Url,
method_name: &str,
params: Vec<u8>,
timeout_hint: Option<Duration>,
) -> ResponseResult<T>
where
T: DeserializeOwned + 'static,
{
let mut request = client
.post(crate::net::method_url(api_url, token, method_name))
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
.body(params)
.build()?;
if let Some(timeout) = timeout_hint {
*request.timeout_mut() = Some(timeout.saturating_add(super::REQUEST_TIMEOUT));
}
let response = client.execute(request).await?;
process_response(response).await
}
async fn process_response<T>(response: Response) -> ResponseResult<T>
where
T: DeserializeOwned + 'static,
{
if response.status().is_server_error() {
tokio::time::sleep(DELAY_ON_SERVER_ERROR).await;
}
let text = response.text().await?;
deserialize_response(text)
}
fn deserialize_response<T>(text: String) -> Result<T, RequestError>
where
T: DeserializeOwned + 'static,
{
serde_json::from_str::<TelegramResponse<T>>(&text)
.map(|mut response| {
use crate::types::{Update, UpdateKind};
use std::{any::Any, iter::zip};
if TypeId::of::<T>() == TypeId::of::<Vec<Update>>() {
if let TelegramResponse::Ok { response, .. } = &mut response {
if let Some(updates) =
(response as &mut T as &mut dyn Any).downcast_mut::<Vec<Update>>()
{
if updates.iter().any(|u| matches!(u.kind, UpdateKind::Error(_))) {
let re_parsed = serde_json::from_str(&text);
if let Ok(TelegramResponse::Ok { response: values, .. }) = re_parsed {
for (update, value) in zip::<_, Vec<_>>(updates, values) {
if let UpdateKind::Error(dest) = &mut update.kind {
*dest = value;
}
}
}
}
}
}
}
response
})
.map_err(|source| RequestError::InvalidJson(crate::errors::InvalidJson::new(source, text)))?
.into()
}
#[cfg(test)]
mod tests {
use cool_asserts::assert_matches;
use crate::{
net::request::deserialize_response,
types::{ChatId, Seconds, True, Update, UpdateId, UpdateKind},
ApiError, RequestError,
};
#[test]
fn invalid_json_does_not_expose_response_content() {
let text = format!(r#"{{"ok":true,"result":"secret{}"}}"#, "💣".repeat(400));
let error = deserialize_response::<True>(text).unwrap_err();
assert!(!format!("{error} {error:?}").contains("secret"));
let RequestError::InvalidJson(error) = error else { panic!("wrong error") };
assert!(error.raw_preview().contains("secret"));
assert!(error.raw_preview().len() <= 1024);
assert!(std::error::Error::source(&error).is_none());
}
#[test]
fn smoke_ok() {
let json = r#"{"ok":true,"result":true}"#.to_owned();
let res = deserialize_response::<True>(json);
assert_matches!(res, Ok(True));
}
#[test]
fn smoke_err() {
let json =
r#"{"ok":false,"description":"Forbidden: bot was blocked by the user"}"#.to_owned();
let res = deserialize_response::<True>(json);
assert_matches!(res, Err(RequestError::Api(ApiError::BotBlocked)));
}
#[test]
fn migrate() {
let json = r#"{"ok":false,"description":"this string is ignored","parameters":{"migrate_to_chat_id":123456}}"#.to_owned();
let res = deserialize_response::<True>(json);
assert_matches!(res, Err(RequestError::MigrateToChatId(ChatId(123456))));
}
#[test]
fn retry_after() {
let json = r#"{"ok":false,"description":"this string is ignored","parameters":{"retry_after":123456}}"#.to_owned();
let res = deserialize_response::<True>(json);
assert_matches!(res, Err(RequestError::RetryAfter(duration)) if duration == Seconds::from_seconds(123456));
}
#[test]
fn update_ok() {
let json = r#"{
"ok":true,
"result":[
{
"update_id":0,
"poll_answer":{
"poll_id":"POLL_ID",
"user": {"id":42,"is_bot":false,"first_name":"blah"},
"option_ids": []
}
}
]
}"#
.to_owned();
let res = deserialize_response::<Vec<Update>>(json).unwrap();
assert_matches!(res, [Update { id: UpdateId(0), kind: UpdateKind::PollAnswer(_) }]);
}
#[test]
fn update_err() {
let json = r#"{
"ok":true,
"result":[
{
"update_id":0,
"poll_answer":{
"poll_id":"POLL_ID",
"user": {"id":42,"is_bot":false,"first_name":"blah"},
"option_ids": []
}
},
{
"update_id":1,
"something unknown to us":17
},
{
"update_id":2,
"poll_answer":{
"poll_id":"POLL_ID",
"user": {"id":42,"is_bot":false,"first_name":"blah"},
"option_ids": [3, 4, 8]
}
},
{
"update_id":3,
"message":{"some fields are missing":true}
}
]
}"#
.to_owned();
let res = deserialize_response::<Vec<Update>>(json).unwrap();
assert_matches!(
res,
[Update { id: UpdateId(0), kind: UpdateKind::PollAnswer(_) }, Update { id: UpdateId(1), kind: UpdateKind::Error(v) } if v.is_object(), Update { id: UpdateId(2), kind: UpdateKind::PollAnswer(_) }, Update { id: UpdateId(3), kind: UpdateKind::Error(v) } if v.is_object()]
);
}
}