mirror of
https://github.com/teloxide/teloxide.git
synced 2025-01-09 19:49:19 +01:00
Merge pull request #123 from teloxide/update-deps
Update the dependencies
This commit is contained in:
commit
6830013922
19 changed files with 235 additions and 138 deletions
27
Cargo.toml
27
Cargo.toml
|
@ -6,21 +6,20 @@ edition = "2018"
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
reqwest = { version = "0.10.0-alpha.1", features = ["json", "unstable-stream"] }
|
serde_json = "1.0.44"
|
||||||
serde_json = "1.0.41"
|
|
||||||
serde = { version = "1.0.101", features = ["derive"] }
|
serde = { version = "1.0.101", features = ["derive"] }
|
||||||
derive_more = "0.15.0"
|
|
||||||
tokio = "0.2.0-alpha.6"
|
tokio = { version = "0.2.6", features = ["full"] }
|
||||||
bytes = "0.4.12"
|
tokio-util = { version = "0.2.0", features = ["full"] }
|
||||||
|
|
||||||
|
reqwest = { version = "0.10", features = ["json", "stream"] }
|
||||||
log = "0.4.8"
|
log = "0.4.8"
|
||||||
pin-project = "0.4.0-alpha.7"
|
bytes = "0.5.3"
|
||||||
futures-preview = "0.3.0-alpha.19"
|
|
||||||
async-trait = "0.1.13"
|
derive_more = "0.99.2"
|
||||||
thiserror = "1.0.2"
|
thiserror = "1.0.9"
|
||||||
|
async-trait = "0.1.22"
|
||||||
|
futures = "0.3.1"
|
||||||
|
pin-project = "0.4.6"
|
||||||
serde_with_macros = "1.0.1"
|
serde_with_macros = "1.0.1"
|
||||||
either = "1.5.3"
|
either = "1.5.3"
|
||||||
|
|
||||||
[features]
|
|
||||||
default = []
|
|
||||||
|
|
||||||
unstable-stream = [] # add streams to public API
|
|
||||||
|
|
|
@ -186,7 +186,8 @@ impl<'a, HandlerE, Eh> FilterDispatcher<'a, HandlerE, Eh> {
|
||||||
Eh: ErrorHandler<Either<UpdaterE, HandlerE>>,
|
Eh: ErrorHandler<Either<UpdaterE, HandlerE>>,
|
||||||
{
|
{
|
||||||
updater
|
updater
|
||||||
.for_each_concurrent(None, |res| async {
|
.for_each_concurrent(None, |res| {
|
||||||
|
async {
|
||||||
let Update { kind, id } = match res {
|
let Update { kind, id } = match res {
|
||||||
Ok(upd) => upd,
|
Ok(upd) => upd,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -261,6 +262,7 @@ impl<'a, HandlerE, Eh> FilterDispatcher<'a, HandlerE, Eh> {
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.await;
|
.await;
|
||||||
}
|
}
|
||||||
|
@ -308,13 +310,17 @@ mod tests {
|
||||||
let counter2 = &AtomicI32::new(0);
|
let counter2 = &AtomicI32::new(0);
|
||||||
|
|
||||||
let mut dp = FilterDispatcher::<Infallible, _>::new(|_| async {})
|
let mut dp = FilterDispatcher::<Infallible, _>::new(|_| async {})
|
||||||
.message_handler(true, |_mes: Message| async move {
|
.message_handler(true, |_mes: Message| {
|
||||||
|
async move {
|
||||||
counter.fetch_add(1, Ordering::SeqCst);
|
counter.fetch_add(1, Ordering::SeqCst);
|
||||||
Ok::<_, Infallible>(())
|
Ok::<_, Infallible>(())
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.message_handler(true, |_mes: Message| async move {
|
.message_handler(true, |_mes: Message| {
|
||||||
|
async move {
|
||||||
counter2.fetch_add(1, Ordering::SeqCst);
|
counter2.fetch_add(1, Ordering::SeqCst);
|
||||||
Ok::<_, Infallible>(())
|
Ok::<_, Infallible>(())
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
dp.dispatch(one_message_updater()).await;
|
dp.dispatch(one_message_updater()).await;
|
||||||
|
|
|
@ -146,7 +146,8 @@ pub fn polling(
|
||||||
|
|
||||||
stream::unfold(
|
stream::unfold(
|
||||||
(allowed_updates, bot, 0),
|
(allowed_updates, bot, 0),
|
||||||
move |(mut allowed_updates, bot, mut offset)| async move {
|
move |(mut allowed_updates, bot, mut offset)| {
|
||||||
|
async move {
|
||||||
let mut req = bot.get_updates().offset(offset);
|
let mut req = bot.get_updates().offset(offset);
|
||||||
req.timeout = timeout;
|
req.timeout = timeout;
|
||||||
req.limit = limit;
|
req.limit = limit;
|
||||||
|
@ -163,6 +164,7 @@ pub fn polling(
|
||||||
};
|
};
|
||||||
|
|
||||||
Some((stream::iter(updates), (allowed_updates, bot, offset)))
|
Some((stream::iter(updates), (allowed_updates, bot, offset)))
|
||||||
|
}
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.flatten()
|
.flatten()
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
use tokio::io::{AsyncWrite, AsyncWriteExt};
|
||||||
|
|
||||||
#[cfg(feature = "unstable-stream")]
|
use crate::errors::DownloadError;
|
||||||
use ::{bytes::Bytes, tokio::stream::Stream};
|
|
||||||
|
|
||||||
use crate::DownloadError;
|
|
||||||
|
|
||||||
use super::TELEGRAM_API_URL;
|
use super::TELEGRAM_API_URL;
|
||||||
|
|
||||||
|
@ -42,11 +39,13 @@ pub async fn download_file_stream(
|
||||||
.await?
|
.await?
|
||||||
.error_for_status()?;
|
.error_for_status()?;
|
||||||
|
|
||||||
Ok(futures::stream::unfold(res, |mut res| async {
|
Ok(futures::stream::unfold(res, |mut res| {
|
||||||
|
async {
|
||||||
match res.chunk().await {
|
match res.chunk().await {
|
||||||
Err(err) => Some((Err(err), res)),
|
Err(err) => Some((Err(err), res)),
|
||||||
Ok(Some(c)) => Some((Ok(c), res)),
|
Ok(Some(c)) => Some((Ok(c), res)),
|
||||||
Ok(None) => None,
|
Ok(None) => None,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,10 +46,15 @@ impl Request for AddStickerToSet<'_> {
|
||||||
"addStickerToSet",
|
"addStickerToSet",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("user_id", &self.user_id)
|
.add("user_id", &self.user_id)
|
||||||
|
.await
|
||||||
.add("name", &self.name)
|
.add("name", &self.name)
|
||||||
|
.await
|
||||||
.add("png_sticker", &self.png_sticker)
|
.add("png_sticker", &self.png_sticker)
|
||||||
|
.await
|
||||||
.add("emojis", &self.emojis)
|
.add("emojis", &self.emojis)
|
||||||
|
.await
|
||||||
.add("mask_position", &self.mask_position)
|
.add("mask_position", &self.mask_position)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -52,12 +52,19 @@ impl Request for CreateNewStickerSet<'_> {
|
||||||
"createNewStickerSet",
|
"createNewStickerSet",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("user_id", &self.user_id)
|
.add("user_id", &self.user_id)
|
||||||
|
.await
|
||||||
.add("name", &self.name)
|
.add("name", &self.name)
|
||||||
|
.await
|
||||||
.add("title", &self.title)
|
.add("title", &self.title)
|
||||||
|
.await
|
||||||
.add("png_sticker", &self.png_sticker)
|
.add("png_sticker", &self.png_sticker)
|
||||||
|
.await
|
||||||
.add("emojis", &self.emojis)
|
.add("emojis", &self.emojis)
|
||||||
|
.await
|
||||||
.add("contains_masks", &self.contains_masks)
|
.add("contains_masks", &self.contains_masks)
|
||||||
|
.await
|
||||||
.add("mask_position", &self.mask_position)
|
.add("mask_position", &self.mask_position)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -43,10 +43,13 @@ impl Request for EditMessageMedia<'_> {
|
||||||
} => {
|
} => {
|
||||||
params = params
|
params = params
|
||||||
.add("chat_id", chat_id)
|
.add("chat_id", chat_id)
|
||||||
.add("message_id", message_id);
|
.await
|
||||||
|
.add("message_id", message_id)
|
||||||
|
.await;
|
||||||
}
|
}
|
||||||
ChatOrInlineMessage::Inline { inline_message_id } => {
|
ChatOrInlineMessage::Inline { inline_message_id } => {
|
||||||
params = params.add("inline_message_id", inline_message_id);
|
params =
|
||||||
|
params.add("inline_message_id", inline_message_id).await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -56,7 +59,9 @@ impl Request for EditMessageMedia<'_> {
|
||||||
"editMessageMedia",
|
"editMessageMedia",
|
||||||
params
|
params
|
||||||
.add("media", &self.media)
|
.add("media", &self.media)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -72,16 +72,27 @@ impl Request for SendAnimation<'_> {
|
||||||
"sendAnimation",
|
"sendAnimation",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("animation", &self.animation)
|
.add("animation", &self.animation)
|
||||||
|
.await
|
||||||
.add("duration", &self.duration)
|
.add("duration", &self.duration)
|
||||||
|
.await
|
||||||
.add("width", &self.width)
|
.add("width", &self.width)
|
||||||
|
.await
|
||||||
.add("height", &self.height)
|
.add("height", &self.height)
|
||||||
|
.await
|
||||||
.add("thumb", &self.thumb)
|
.add("thumb", &self.thumb)
|
||||||
|
.await
|
||||||
.add("caption", &self.caption)
|
.add("caption", &self.caption)
|
||||||
|
.await
|
||||||
.add("parse_mode", &self.parse_mode)
|
.add("parse_mode", &self.parse_mode)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -68,16 +68,27 @@ impl Request for SendAudio<'_> {
|
||||||
"sendAudio",
|
"sendAudio",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("audio", &self.audio)
|
.add("audio", &self.audio)
|
||||||
|
.await
|
||||||
.add("caption", &self.caption)
|
.add("caption", &self.caption)
|
||||||
|
.await
|
||||||
.add("parse_mode", &self.parse_mode)
|
.add("parse_mode", &self.parse_mode)
|
||||||
|
.await
|
||||||
.add("duration", &self.duration)
|
.add("duration", &self.duration)
|
||||||
|
.await
|
||||||
.add("performer", &self.performer)
|
.add("performer", &self.performer)
|
||||||
|
.await
|
||||||
.add("title", &self.title)
|
.add("title", &self.title)
|
||||||
|
.await
|
||||||
.add("thumb", &self.thumb)
|
.add("thumb", &self.thumb)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -61,13 +61,21 @@ impl Request for SendDocument<'_> {
|
||||||
"sendDocument",
|
"sendDocument",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("document", &self.document)
|
.add("document", &self.document)
|
||||||
|
.await
|
||||||
.add("thumb", &self.thumb)
|
.add("thumb", &self.thumb)
|
||||||
|
.await
|
||||||
.add("caption", &self.caption)
|
.add("caption", &self.caption)
|
||||||
|
.await
|
||||||
.add("parse_mode", &self.parse_mode)
|
.add("parse_mode", &self.parse_mode)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -39,9 +39,13 @@ impl Request for SendMediaGroup<'_> {
|
||||||
"sendMediaGroup",
|
"sendMediaGroup",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("media", &self.media)
|
.add("media", &self.media)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -50,12 +50,19 @@ impl Request for SendPhoto<'_> {
|
||||||
"sendPhoto",
|
"sendPhoto",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("photo", &self.photo)
|
.add("photo", &self.photo)
|
||||||
|
.await
|
||||||
.add("caption", &self.caption)
|
.add("caption", &self.caption)
|
||||||
|
.await
|
||||||
.add("parse_mode", &self.parse_mode)
|
.add("parse_mode", &self.parse_mode)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -45,10 +45,15 @@ impl Request for SendSticker<'_> {
|
||||||
"sendSticker",
|
"sendSticker",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("sticker", &self.sticker)
|
.add("sticker", &self.sticker)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -70,17 +70,29 @@ impl Request for SendVideo<'_> {
|
||||||
"sendVideo",
|
"sendVideo",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("video", &self.video)
|
.add("video", &self.video)
|
||||||
|
.await
|
||||||
.add("duration", &self.duration)
|
.add("duration", &self.duration)
|
||||||
|
.await
|
||||||
.add("width", &self.width)
|
.add("width", &self.width)
|
||||||
|
.await
|
||||||
.add("height", &self.height)
|
.add("height", &self.height)
|
||||||
|
.await
|
||||||
.add("thumb", &self.thumb)
|
.add("thumb", &self.thumb)
|
||||||
|
.await
|
||||||
.add("caption", &self.caption)
|
.add("caption", &self.caption)
|
||||||
|
.await
|
||||||
.add("parse_mode", &self.parse_mode)
|
.add("parse_mode", &self.parse_mode)
|
||||||
|
.await
|
||||||
.add("supports_streaming", &self.supports_streaming)
|
.add("supports_streaming", &self.supports_streaming)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -59,13 +59,21 @@ impl Request for SendVideoNote<'_> {
|
||||||
"sendVideoNote",
|
"sendVideoNote",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("video_note", &self.video_note)
|
.add("video_note", &self.video_note)
|
||||||
|
.await
|
||||||
.add("duration", &self.duration)
|
.add("duration", &self.duration)
|
||||||
|
.await
|
||||||
.add("length", &self.length)
|
.add("length", &self.length)
|
||||||
|
.await
|
||||||
.add("thumb", &self.thumb)
|
.add("thumb", &self.thumb)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -56,13 +56,21 @@ impl Request for SendVoice<'_> {
|
||||||
"sendVoice",
|
"sendVoice",
|
||||||
FormBuilder::new()
|
FormBuilder::new()
|
||||||
.add("chat_id", &self.chat_id)
|
.add("chat_id", &self.chat_id)
|
||||||
|
.await
|
||||||
.add("voice", &self.voice)
|
.add("voice", &self.voice)
|
||||||
|
.await
|
||||||
.add("caption", &self.caption)
|
.add("caption", &self.caption)
|
||||||
|
.await
|
||||||
.add("parse_mode", &self.parse_mode)
|
.add("parse_mode", &self.parse_mode)
|
||||||
|
.await
|
||||||
.add("duration", &self.duration)
|
.add("duration", &self.duration)
|
||||||
|
.await
|
||||||
.add("disable_notification", &self.disable_notification)
|
.add("disable_notification", &self.disable_notification)
|
||||||
|
.await
|
||||||
.add("reply_to_message_id", &self.reply_to_message_id)
|
.add("reply_to_message_id", &self.reply_to_message_id)
|
||||||
|
.await
|
||||||
.add("reply_markup", &self.reply_markup)
|
.add("reply_markup", &self.reply_markup)
|
||||||
|
.await
|
||||||
.build(),
|
.build(),
|
||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
|
|
|
@ -22,7 +22,7 @@ impl FormBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add the supplied key-value pair to this `FormBuilder`.
|
/// Add the supplied key-value pair to this `FormBuilder`.
|
||||||
pub fn add<'a, T, N>(self, name: N, value: &T) -> Self
|
pub async fn add<'a, T, N>(self, name: N, value: &T) -> Self
|
||||||
where
|
where
|
||||||
N: Into<Cow<'a, str>>,
|
N: Into<Cow<'a, str>>,
|
||||||
T: IntoFormValue,
|
T: IntoFormValue,
|
||||||
|
@ -32,20 +32,21 @@ impl FormBuilder {
|
||||||
Some(FormValue::Str(string)) => Self {
|
Some(FormValue::Str(string)) => Self {
|
||||||
form: self.form.text(name, string),
|
form: self.form.text(name, string),
|
||||||
},
|
},
|
||||||
Some(FormValue::File(path)) => self.add_file(name, path),
|
Some(FormValue::File(path)) => self.add_file(name, path).await,
|
||||||
None => self,
|
None => self,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// used in SendMediaGroup
|
// used in SendMediaGroup
|
||||||
pub fn add_file<'a, N>(self, name: N, path_to_file: PathBuf) -> Self
|
pub async fn add_file<'a, N>(self, name: N, path_to_file: PathBuf) -> Self
|
||||||
where
|
where
|
||||||
N: Into<Cow<'a, str>>,
|
N: Into<Cow<'a, str>>,
|
||||||
{
|
{
|
||||||
Self {
|
Self {
|
||||||
form: self
|
form: self.form.part(
|
||||||
.form
|
name.into().into_owned(),
|
||||||
.part(name.into().into_owned(), file_to_part(path_to_file)),
|
file_to_part(path_to_file).await,
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,11 @@ use std::path::PathBuf;
|
||||||
|
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use reqwest::{multipart::Part, Body};
|
use reqwest::{multipart::Part, Body};
|
||||||
use tokio::{codec::FramedRead, prelude::*};
|
use tokio_util::codec::{Decoder, FramedRead};
|
||||||
|
|
||||||
struct FileDecoder;
|
struct FileDecoder;
|
||||||
|
|
||||||
impl tokio::codec::Decoder for FileDecoder {
|
impl Decoder for FileDecoder {
|
||||||
type Item = Bytes;
|
type Item = Bytes;
|
||||||
type Error = std::io::Error;
|
type Error = std::io::Error;
|
||||||
|
|
||||||
|
@ -17,24 +17,23 @@ impl tokio::codec::Decoder for FileDecoder {
|
||||||
if src.is_empty() {
|
if src.is_empty() {
|
||||||
return Ok(None);
|
return Ok(None);
|
||||||
}
|
}
|
||||||
Ok(Some(src.take().freeze()))
|
Ok(Some(src.split().freeze()))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn file_to_part(path_to_file: PathBuf) -> Part {
|
pub async fn file_to_part(path_to_file: PathBuf) -> Part {
|
||||||
let file_name = path_to_file
|
let file_name = path_to_file
|
||||||
.file_name()
|
.file_name()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.to_string_lossy()
|
.to_string_lossy()
|
||||||
.into_owned();
|
.into_owned();
|
||||||
|
|
||||||
let file = tokio::fs::File::open(path_to_file)
|
let file = FramedRead::new(
|
||||||
.map(|file| {
|
tokio::fs::File::open(path_to_file).await.unwrap(), /* TODO: this
|
||||||
FramedRead::new(
|
* can
|
||||||
file.unwrap(), /* TODO: this can cause panics */
|
* cause panics */
|
||||||
FileDecoder,
|
FileDecoder,
|
||||||
)
|
);
|
||||||
})
|
|
||||||
.flatten_stream();
|
|
||||||
Part::stream(Body::wrap_stream(file)).file_name(file_name)
|
Part::stream(Body::wrap_stream(file)).file_name(file_name)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue