From 3cf4dfeeb95cffa53059c2cb1ec127de6b4aabdb Mon Sep 17 00:00:00 2001 From: Waffle <wafflewafflerov@gmail.com> Date: Wed, 9 Oct 2019 22:31:50 +0300 Subject: [PATCH] Update libs --- Cargo.toml | 11 +++++------ src/bot/download.rs | 4 ++-- src/network/download.rs | 31 ++++++++++++++++++++----------- src/network/request.rs | 17 +++++++++++------ src/requests/utils.rs | 4 ++-- 5 files changed, 40 insertions(+), 27 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ae9588a7..23968a33 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,14 +6,13 @@ 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" libc = "0.2.62" diff --git a/src/bot/download.rs b/src/bot/download.rs index d3c6ff69..11f77ac5 100644 --- a/src/bot/download.rs +++ b/src/bot/download.rs @@ -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 } diff --git a/src/network/download.rs b/src/network/download.rs index 82ff18ea..cd28ba4a 100644 --- a/src/network/download.rs +++ b/src/network/download.rs @@ -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, + } + })) } diff --git a/src/network/request.rs b/src/network/request.rs index 139b6f74..ac97c612 100644 --- a/src/network/request.rs +++ b/src/network/request.rs @@ -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)?, ) diff --git a/src/requests/utils.rs b/src/requests/utils.rs index 90f9c409..9add97f0 100644 --- a/src/requests/utils.rs +++ b/src/requests/utils.rs @@ -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()