teloxide/src/net/request.rs

78 lines
1.9 KiB
Rust
Raw Normal View History

use std::time::Duration;
2020-08-12 19:39:40 +03:00
use reqwest::{Client, Response};
use serde::{de::DeserializeOwned, Serialize};
2020-08-12 19:39:40 +03:00
use crate::{
net::{TelegramResponse, TELEGRAM_API_URL},
requests::ResponseResult,
serde_multipart::to_form,
RequestError,
};
2020-08-12 19:39:40 +03:00
const DELAY_ON_SERVER_ERROR: Duration = Duration::from_secs(10);
pub async fn request_multipart<P, R>(
2020-08-12 19:39:40 +03:00
client: &Client,
token: &str,
method_name: &str,
params: &P, // I'll regret this
) -> ResponseResult<R>
2020-08-12 19:39:40 +03:00
where
P: Serialize,
R: DeserializeOwned,
2020-08-12 19:39:40 +03:00
{
use crate::serde_multipart::Error;
let form = match to_form(params).await {
Ok(x) => x,
Err(Error::Io(ioerr)) => return Err(RequestError::Io(ioerr)),
Err(_) => unreachable!(
"we don't create requests those fail to serialize (if you see this, open an issue :|)"
),
};
2020-08-12 19:39:40 +03:00
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.multipart(form)
2020-08-12 19:39:40 +03:00
.send()
.await
.map_err(RequestError::NetworkError)?;
process_response(response).await
}
pub async fn request_json<P, R>(
2020-08-12 19:39:40 +03:00
client: &Client,
token: &str,
method_name: &str,
params: &P,
) -> ResponseResult<R>
2020-08-12 19:39:40 +03:00
where
P: Serialize,
R: DeserializeOwned,
2020-08-12 19:39:40 +03:00
{
let response = client
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
.json(params)
.send()
.await
.map_err(RequestError::NetworkError)?;
process_response(response).await
}
async fn process_response<T>(response: Response) -> ResponseResult<T>
where
T: DeserializeOwned,
{
if response.status().is_server_error() {
tokio::time::delay_for(DELAY_ON_SERVER_ERROR).await;
}
serde_json::from_str::<TelegramResponse<T>>(
&response.text().await.map_err(RequestError::NetworkError)?,
)
.map_err(RequestError::InvalidJson)?
.into()
}