2020-08-15 23:21:55 +02:00
|
|
|
use std::time::Duration;
|
2020-08-12 18:39:40 +02:00
|
|
|
|
2020-09-19 18:00:23 +02:00
|
|
|
use reqwest::{
|
|
|
|
header::{HeaderValue, CONTENT_TYPE},
|
|
|
|
Client, Response,
|
|
|
|
};
|
2020-08-15 23:21:55 +02:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2020-08-12 18:39:40 +02:00
|
|
|
|
2020-08-15 23:21:55 +02:00
|
|
|
use crate::{
|
|
|
|
net::{TelegramResponse, TELEGRAM_API_URL},
|
|
|
|
requests::ResponseResult,
|
|
|
|
serde_multipart::to_form,
|
|
|
|
RequestError,
|
|
|
|
};
|
2020-08-12 18:39:40 +02:00
|
|
|
|
|
|
|
const DELAY_ON_SERVER_ERROR: Duration = Duration::from_secs(10);
|
|
|
|
|
2020-08-15 23:21:55 +02:00
|
|
|
pub async fn request_multipart<P, R>(
|
2020-08-12 18:39:40 +02:00
|
|
|
client: &Client,
|
|
|
|
token: &str,
|
|
|
|
method_name: &str,
|
2020-08-15 23:21:55 +02:00
|
|
|
params: &P, // I'll regret this
|
|
|
|
) -> ResponseResult<R>
|
2020-08-12 18:39:40 +02:00
|
|
|
where
|
2020-08-15 23:21:55 +02:00
|
|
|
P: Serialize,
|
|
|
|
R: DeserializeOwned,
|
2020-08-12 18:39:40 +02:00
|
|
|
{
|
2020-08-15 23:21:55 +02: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 18:39:40 +02:00
|
|
|
let response = client
|
2020-11-17 20:38:30 +01:00
|
|
|
.post(crate::net::method_url(
|
|
|
|
reqwest::Url::parse(TELEGRAM_API_URL).expect("failed to parse default url"),
|
|
|
|
token,
|
|
|
|
method_name,
|
|
|
|
))
|
2020-08-15 23:21:55 +02:00
|
|
|
.multipart(form)
|
2020-08-12 18:39:40 +02:00
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.map_err(RequestError::NetworkError)?;
|
|
|
|
|
|
|
|
process_response(response).await
|
|
|
|
}
|
|
|
|
|
2020-08-15 23:21:55 +02:00
|
|
|
pub async fn request_json<P, R>(
|
2020-08-12 18:39:40 +02:00
|
|
|
client: &Client,
|
|
|
|
token: &str,
|
|
|
|
method_name: &str,
|
|
|
|
params: &P,
|
2020-08-15 23:21:55 +02:00
|
|
|
) -> ResponseResult<R>
|
2020-08-12 18:39:40 +02:00
|
|
|
where
|
|
|
|
P: Serialize,
|
2020-08-15 23:21:55 +02:00
|
|
|
R: DeserializeOwned,
|
2020-08-12 18:39:40 +02:00
|
|
|
{
|
|
|
|
let response = client
|
2020-11-17 20:38:30 +01:00
|
|
|
.post(crate::net::method_url(
|
|
|
|
reqwest::Url::parse(TELEGRAM_API_URL).expect("failed to parse default url"),
|
|
|
|
token,
|
|
|
|
method_name,
|
|
|
|
))
|
2020-08-12 18:39:40 +02:00
|
|
|
.json(params)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.map_err(RequestError::NetworkError)?;
|
|
|
|
|
|
|
|
process_response(response).await
|
|
|
|
}
|
|
|
|
|
2020-09-19 18:00:23 +02:00
|
|
|
// FIXME(waffle):
|
|
|
|
// request_{json,mutipart} are currently used in old code, so we keep them
|
|
|
|
// for now when they will not be used anymore, we should remove them
|
|
|
|
// and rename request_{json,mutipart}2 => request_{json,mutipart}
|
|
|
|
|
|
|
|
pub async fn request_multipart2<T>(
|
|
|
|
client: &Client,
|
|
|
|
token: &str,
|
2020-11-17 20:38:30 +01:00
|
|
|
api_url: reqwest::Url,
|
2020-09-19 18:00:23 +02:00
|
|
|
method_name: &str,
|
|
|
|
params: reqwest::multipart::Form,
|
|
|
|
) -> ResponseResult<T>
|
|
|
|
where
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
|
|
|
let response = client
|
2020-11-17 20:38:30 +01:00
|
|
|
.post(crate::net::method_url(api_url, token, method_name))
|
2020-09-19 18:00:23 +02:00
|
|
|
.multipart(params)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.map_err(RequestError::NetworkError)?;
|
|
|
|
|
|
|
|
process_response(response).await
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn request_json2<T>(
|
|
|
|
client: &Client,
|
|
|
|
token: &str,
|
2020-11-17 20:38:30 +01:00
|
|
|
api_url: reqwest::Url,
|
2020-09-19 18:00:23 +02:00
|
|
|
method_name: &str,
|
|
|
|
params: Vec<u8>,
|
|
|
|
) -> ResponseResult<T>
|
|
|
|
where
|
|
|
|
T: DeserializeOwned,
|
|
|
|
{
|
|
|
|
let response = client
|
2020-11-17 20:38:30 +01:00
|
|
|
.post(crate::net::method_url(api_url, token, method_name))
|
2020-09-19 18:00:23 +02:00
|
|
|
.header(CONTENT_TYPE, HeaderValue::from_static("application/json"))
|
|
|
|
.body(params)
|
|
|
|
.send()
|
|
|
|
.await
|
|
|
|
.map_err(RequestError::NetworkError)?;
|
|
|
|
|
|
|
|
process_response(response).await
|
|
|
|
}
|
|
|
|
|
2020-08-12 18:39:40 +02:00
|
|
|
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()
|
|
|
|
}
|