mirror of
https://github.com/teloxide/teloxide.git
synced 2025-03-14 11:44:04 +01:00
commit
ca6b4a4628
5 changed files with 40 additions and 27 deletions
11
Cargo.toml
11
Cargo.toml
|
@ -6,13 +6,12 @@ edition = "2018"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
reqwest = { git = "https://github.com/seanmonstar/reqwest", rev = "ba7b2a754eab0d79817ea8551d0803806ae8af7d" }
|
||||
serde_json = "1.0.39"
|
||||
serde = {version = "1.0.92", features = ["derive"] }
|
||||
lazy_static = "1.3"
|
||||
reqwest = { version = "0.10.0-alpha.1", features = ["json", "unstable-stream"] }
|
||||
serde_json = "1.0.41"
|
||||
serde = { version = "1.0.101", features = ["derive"] }
|
||||
apply = "0.2.2"
|
||||
derive_more = "0.15.0"
|
||||
tokio = "0.2.0-alpha.4"
|
||||
tokio = "0.2.0-alpha.6"
|
||||
bytes = "0.4.12"
|
||||
futures-preview = "0.3.0-alpha.18"
|
||||
futures-preview = "0.3.0-alpha.19"
|
||||
async-trait = "0.1.13"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use reqwest::r#async::Chunk;
|
||||
use tokio::{io::AsyncWrite, stream::Stream};
|
||||
use bytes::Bytes;
|
||||
|
||||
use crate::{
|
||||
bot::Bot,
|
||||
|
@ -59,7 +59,7 @@ impl Bot {
|
|||
pub async fn download_file_stream(
|
||||
&self,
|
||||
path: &str,
|
||||
) -> Result<impl Stream<Item = Result<Chunk, reqwest::Error>>, reqwest::Error>
|
||||
) -> Result<impl Stream<Item = Result<Bytes, reqwest::Error>>, reqwest::Error>
|
||||
{
|
||||
download_file_stream(&self.client, &self.token, path).await
|
||||
}
|
||||
|
|
|
@ -1,6 +1,5 @@
|
|||
use bytes::Buf;
|
||||
use futures::StreamExt;
|
||||
use reqwest::r#async::{Chunk, Client};
|
||||
use bytes::Bytes;
|
||||
use reqwest::Client;
|
||||
use tokio::{
|
||||
io::{AsyncWrite, AsyncWriteExt},
|
||||
stream::Stream,
|
||||
|
@ -19,11 +18,14 @@ pub async fn download_file<D>(
|
|||
where
|
||||
D: AsyncWrite + Unpin,
|
||||
{
|
||||
let mut stream = download_file_stream(client, token, path).await?;
|
||||
let mut res = client
|
||||
.get(&super::file_url(TELEGRAM_API_URL, token, path))
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?;
|
||||
|
||||
while let Some(chunk) = stream.next().await {
|
||||
let chunk = chunk?;
|
||||
destination.write_all(chunk.bytes()).await?;
|
||||
while let Some(chunk) = res.chunk().await? {
|
||||
destination.write_all(&chunk).await?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -33,11 +35,18 @@ pub async fn download_file_stream(
|
|||
client: &Client,
|
||||
token: &str,
|
||||
path: &str,
|
||||
) -> Result<impl Stream<Item = Result<Chunk, reqwest::Error>>, reqwest::Error> {
|
||||
Ok(client
|
||||
) -> Result<impl Stream<Item = reqwest::Result<Bytes>>, reqwest::Error> {
|
||||
let res = client
|
||||
.get(&super::file_url(TELEGRAM_API_URL, token, path))
|
||||
.send()
|
||||
.await?
|
||||
.error_for_status()?
|
||||
.into_body())
|
||||
.error_for_status()?;
|
||||
|
||||
Ok(futures::stream::unfold(res, |mut res| async {
|
||||
match res.chunk().await {
|
||||
Err(err) => Some((Err(err), res)),
|
||||
Ok(Some(c)) => Some((Ok(c), res)),
|
||||
Ok(None) => None,
|
||||
}
|
||||
}))
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use apply::Apply;
|
||||
use reqwest::r#async::{multipart::Form, Client, Response};
|
||||
use reqwest::{multipart::Form, Client, Response};
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
use crate::{requests::ResponseResult, RequestError};
|
||||
|
@ -29,12 +29,16 @@ where
|
|||
.await
|
||||
}
|
||||
|
||||
pub async fn request_json<T: DeserializeOwned, P: Serialize>(
|
||||
pub async fn request_json<T, P>(
|
||||
client: &Client,
|
||||
token: &str,
|
||||
method_name: &str,
|
||||
params: &P,
|
||||
) -> ResponseResult<T> {
|
||||
) -> ResponseResult<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
P: Serialize,
|
||||
{
|
||||
process_response(
|
||||
client
|
||||
.post(&super::method_url(TELEGRAM_API_URL, token, method_name))
|
||||
|
@ -46,9 +50,10 @@ pub async fn request_json<T: DeserializeOwned, P: Serialize>(
|
|||
.await
|
||||
}
|
||||
|
||||
async fn process_response<T: DeserializeOwned>(
|
||||
mut response: Response,
|
||||
) -> ResponseResult<T> {
|
||||
async fn process_response<T>(response: Response) -> ResponseResult<T>
|
||||
where
|
||||
T: DeserializeOwned,
|
||||
{
|
||||
serde_json::from_str::<TelegramResponse<T>>(
|
||||
&response.text().await.map_err(RequestError::NetworkError)?,
|
||||
)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use bytes::{Bytes, BytesMut};
|
||||
use reqwest::r#async::multipart::Part;
|
||||
use reqwest::{multipart::Part, Body};
|
||||
use tokio::{codec::FramedRead, prelude::*};
|
||||
|
||||
struct FileDecoder;
|
||||
|
@ -30,7 +30,7 @@ pub fn file_to_part(path_to_file: &PathBuf) -> Part {
|
|||
)
|
||||
})
|
||||
.flatten_stream();
|
||||
let part = Part::stream(file).file_name(
|
||||
let part = Part::stream(Body::wrap_stream(file)).file_name(
|
||||
path_to_file
|
||||
.file_name()
|
||||
.unwrap()
|
||||
|
|
Loading…
Add table
Reference in a new issue