mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-22 06:45: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::{
|
pub use self::{
|
||||||
download::download_file,
|
download::download_file,
|
||||||
request::{request_json, request_multipart},
|
request::{request_json, request_multipart, request_simple},
|
||||||
telegram_response::TelegramResponse,
|
telegram_response::TelegramResponse,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
use apply::Apply;
|
|
||||||
use reqwest::{multipart::Form, Client, Response};
|
use reqwest::{multipart::Form, Client, Response};
|
||||||
use serde::{de::DeserializeOwned, Serialize};
|
use serde::{de::DeserializeOwned, Serialize};
|
||||||
|
|
||||||
|
@ -10,18 +9,33 @@ pub async fn request_multipart<T>(
|
||||||
client: &Client,
|
client: &Client,
|
||||||
token: &str,
|
token: &str,
|
||||||
method_name: &str,
|
method_name: &str,
|
||||||
params: Option<Form>,
|
params: Form,
|
||||||
) -> ResponseResult<T>
|
) -> ResponseResult<T>
|
||||||
where
|
where
|
||||||
T: DeserializeOwned,
|
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(
|
process_response(
|
||||||
client
|
client
|
||||||
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
|
.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()
|
.send()
|
||||||
.await
|
.await
|
||||||
.map_err(RequestError::NetworkError)?,
|
.map_err(RequestError::NetworkError)?,
|
||||||
|
|
|
@ -24,13 +24,7 @@ impl Request for GetMe<'_> {
|
||||||
|
|
||||||
impl GetMe<'_> {
|
impl GetMe<'_> {
|
||||||
pub async fn send(self) -> ResponseResult<User> {
|
pub async fn send(self) -> ResponseResult<User> {
|
||||||
network::request_multipart(
|
network::request_simple(self.ctx.client, self.ctx.token, "getMe").await
|
||||||
self.ctx.client,
|
|
||||||
self.ctx.token,
|
|
||||||
"getMe",
|
|
||||||
None,
|
|
||||||
)
|
|
||||||
.await
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -107,7 +107,7 @@ impl SendAudio<'_> {
|
||||||
&self.ctx.client,
|
&self.ctx.client,
|
||||||
&self.ctx.token,
|
&self.ctx.token,
|
||||||
"sendAudio",
|
"sendAudio",
|
||||||
Some(params),
|
params,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use apply::Apply;
|
|
||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
|
@ -7,7 +5,7 @@ use crate::{
|
||||||
requests::{
|
requests::{
|
||||||
form_builder::FormBuilder, Request, RequestContext, ResponseResult,
|
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.
|
/// Use this method to send a group of photos or videos as an album.
|
||||||
|
@ -33,23 +31,8 @@ impl Request for SendMediaGroup<'_> {
|
||||||
|
|
||||||
impl SendMediaGroup<'_> {
|
impl SendMediaGroup<'_> {
|
||||||
pub async fn send(self) -> ResponseResult<Vec<Message>> {
|
pub async fn send(self) -> ResponseResult<Vec<Message>> {
|
||||||
let params = FormBuilder::new()
|
let form = FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.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("media", &self.media)
|
||||||
.add_if_some(
|
.add_if_some(
|
||||||
"disable_notification",
|
"disable_notification",
|
||||||
|
@ -58,13 +41,21 @@ impl SendMediaGroup<'_> {
|
||||||
.add_if_some(
|
.add_if_some(
|
||||||
"reply_to_message_id",
|
"reply_to_message_id",
|
||||||
self.reply_to_message_id.as_ref(),
|
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(
|
request_multipart(
|
||||||
&self.ctx.client,
|
&self.ctx.client,
|
||||||
&self.ctx.token,
|
&self.ctx.token,
|
||||||
"sendMediaGroup",
|
"sendMediaGroup",
|
||||||
Some(params),
|
form.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,7 +79,7 @@ impl SendPhoto<'_> {
|
||||||
&self.ctx.client,
|
&self.ctx.client,
|
||||||
&self.ctx.token,
|
&self.ctx.token,
|
||||||
"sendPhoto",
|
"sendPhoto",
|
||||||
Some(params),
|
params,
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue