From 2e9fe3a27c4eaf1a16eb0f301a26f87b87e1d77a Mon Sep 17 00:00:00 2001 From: ncaq Date: Thu, 29 Aug 2019 20:41:03 +0900 Subject: [PATCH 1/2] added: async_send and send implement by async send closed #25 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit reqwest crush blocking IO in non-blocking IO. [attempted to run an executor while another executor is already running. · Issue #541 · seanmonstar/reqwest](https://github.com/seanmonstar/reqwest/issues/541) This pull request to use request async version. --- Cargo.toml | 3 ++- src/lib.rs | 1 + src/slack.rs | 30 ++++++++++++++++++++++++++---- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 54d448c34..2252b6866 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,13 +7,14 @@ license = "MIT/Apache-2.0" name = "slack-hook" readme = "README.md" repository = "https://github.com/frostly/rust-slack" -version = "0.8.0" +version = "0.9.0" [dependencies] chrono = "0.4" reqwest = "0.9" hex = "0.3" error-chain = "~0.11" +futures = "0.1.28" serde = "1" serde_derive = "1" serde_json = "1" diff --git a/src/lib.rs b/src/lib.rs index 52e764576..bfa257b90 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -11,6 +11,7 @@ extern crate reqwest; extern crate chrono; #[macro_use] extern crate error_chain; +extern crate futures; extern crate hex as hexx; extern crate serde; #[macro_use] diff --git a/src/slack.rs b/src/slack.rs index 05b7ac17d..79f2d0c63 100644 --- a/src/slack.rs +++ b/src/slack.rs @@ -1,6 +1,8 @@ use chrono::NaiveDateTime; use error::{Error, ErrorKind, Result}; -use reqwest::{Client, Url}; +use futures::*; +use reqwest::async::Client; +use reqwest::Url; use serde::{Serialize, Serializer}; use std::fmt; use {Payload, TryInto}; @@ -23,14 +25,34 @@ impl Slack { /// Send payload to slack service pub fn send(&self, payload: &Payload) -> Result<()> { - let response = self.client.post(self.hook.clone()).json(payload).send()?; - - if response.status().is_success(){ + let response = self + .client + .post(self.hook.clone()) + .json(payload) + .send() + .wait()?; + + if response.status().is_success() { Ok(()) } else { Err(ErrorKind::Slack(format!("HTTP error {}", response.status())).into()) } } + + /// Send payload to slack service, returning a future Response. + pub fn async_send(&self, payload: &Payload) -> impl Future> { + self.client + .post(self.hook.clone()) + .json(payload) + .send() + .map(|response| { + if response.status().is_success() { + Ok(()) + } else { + Err(ErrorKind::Slack(format!("HTTP error {}", response.status())).into()) + } + }) + } } /// Slack timestamp From 03c8234f84ea69b7e412a28546f9e979b956be8a Mon Sep 17 00:00:00 2001 From: ncaq Date: Fri, 6 Sep 2019 17:25:16 +0900 Subject: [PATCH 2/2] fixed: unneed nest Result --- src/slack.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/slack.rs b/src/slack.rs index 79f2d0c63..48b556691 100644 --- a/src/slack.rs +++ b/src/slack.rs @@ -40,12 +40,13 @@ impl Slack { } /// Send payload to slack service, returning a future Response. - pub fn async_send(&self, payload: &Payload) -> impl Future> { + pub fn async_send(&self, payload: &Payload) -> impl Future { self.client .post(self.hook.clone()) .json(payload) .send() - .map(|response| { + .map_err(|err| err.into()) + .and_then(|response| { if response.status().is_success() { Ok(()) } else {