mirror of
https://github.com/teloxide/teloxide.git
synced 2024-12-31 16:40:37 +01:00
Split request_multipart
into self (that always accept Form
) and request_simple
, simplify SendMediaGroup::send
This commit is contained in:
parent
815c8cdb7d
commit
3999e6dbc8
6 changed files with 39 additions and 40 deletions
|
@ -3,7 +3,7 @@ pub use download::download_file_stream;
|
|||
|
||||
pub use self::{
|
||||
download::download_file,
|
||||
request::{request_json, request_multipart},
|
||||
request::{request_json, request_multipart, request_simple},
|
||||
telegram_response::TelegramResponse,
|
||||
};
|
||||
|
||||
|
|
|
@ -1,4 +1,3 @@
|
|||
use apply::Apply;
|
||||
use reqwest::{multipart::Form, Client, Response};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
|
@ -10,18 +9,33 @@ pub async fn request_multipart<T>(
|
|||
client: &Client,
|
||||
token: &str,
|
||||
method_name: &str,
|
||||
params: Option<Form>,
|
||||
params: Form,
|
||||
) -> ResponseResult<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
process_response(
|
||||
client
|
||||
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
|
||||
.multipart(params)
|
||||
.send()
|
||||
.await
|
||||
.map_err(RequestError::NetworkError)?,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
pub async fn request_simple<T>(
|
||||
client: &Client,
|
||||
token: &str,
|
||||
method_name: &str,
|
||||
) -> ResponseResult<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
process_response(
|
||||
client
|
||||
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
|
||||
.apply(|request_builder| match params {
|
||||
Some(params) => request_builder.multipart(params),
|
||||
None => request_builder,
|
||||
})
|
||||
.send()
|
||||
.await
|
||||
.map_err(RequestError::NetworkError)?,
|
||||
|
|
|
@ -24,13 +24,7 @@ impl Request for GetMe<'_> {
|
|||
|
||||
impl GetMe<'_> {
|
||||
pub async fn send(self) -> ResponseResult<User> {
|
||||
network::request_multipart(
|
||||
self.ctx.client,
|
||||
self.ctx.token,
|
||||
"getMe",
|
||||
None,
|
||||
)
|
||||
.await
|
||||
network::request_simple(self.ctx.client, self.ctx.token, "getMe").await
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -107,7 +107,7 @@ impl SendAudio<'_> {
|
|||
&self.ctx.client,
|
||||
&self.ctx.token,
|
||||
"sendAudio",
|
||||
Some(params),
|
||||
params,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
use apply::Apply;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::{
|
||||
|
@ -7,7 +5,7 @@ use crate::{
|
|||
requests::{
|
||||
form_builder::FormBuilder, Request, RequestContext, ResponseResult,
|
||||
},
|
||||
types::{ChatId, InputFile, InputMedia, Message},
|
||||
types::{ChatId, InputMedia, Message},
|
||||
};
|
||||
|
||||
/// Use this method to send a group of photos or videos as an album.
|
||||
|
@ -33,23 +31,8 @@ impl Request for SendMediaGroup<'_> {
|
|||
|
||||
impl SendMediaGroup<'_> {
|
||||
pub async fn send(self) -> ResponseResult<Vec<Message>> {
|
||||
let params = FormBuilder::new()
|
||||
let form = FormBuilder::new()
|
||||
.add("chat_id", &self.chat_id)
|
||||
.apply(|form| {
|
||||
self.media
|
||||
.iter()
|
||||
.map(|e| e.media())
|
||||
.fold(form, |acc, file| {
|
||||
if let InputFile::File(path) = file {
|
||||
acc.add_file(
|
||||
&path.file_name().unwrap().to_string_lossy(),
|
||||
path,
|
||||
)
|
||||
} else {
|
||||
acc
|
||||
}
|
||||
})
|
||||
})
|
||||
.add("media", &self.media)
|
||||
.add_if_some(
|
||||
"disable_notification",
|
||||
|
@ -58,13 +41,21 @@ impl SendMediaGroup<'_> {
|
|||
.add_if_some(
|
||||
"reply_to_message_id",
|
||||
self.reply_to_message_id.as_ref(),
|
||||
)
|
||||
.build();
|
||||
);
|
||||
|
||||
let form = self.media.iter().filter_map(|e| e.media().as_file())
|
||||
.fold(form, |acc, path|
|
||||
acc.add_file(
|
||||
&path.file_name().unwrap().to_string_lossy(),
|
||||
path,
|
||||
)
|
||||
);
|
||||
|
||||
request_multipart(
|
||||
&self.ctx.client,
|
||||
&self.ctx.token,
|
||||
"sendMediaGroup",
|
||||
Some(params),
|
||||
form.build(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
|
@ -79,7 +79,7 @@ impl SendPhoto<'_> {
|
|||
&self.ctx.client,
|
||||
&self.ctx.token,
|
||||
"sendPhoto",
|
||||
Some(params),
|
||||
params,
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue