From 143e072e4f6e107950ab6e7eb2de1bd57de65221 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 22 Dec 2019 22:42:30 -0500 Subject: various cleanup --- ipfs-api/src/client.rs | 113 ++++++++++++++++++++++++++----------------------- ipfs-api/src/read.rs | 3 +- 2 files changed, 62 insertions(+), 54 deletions(-) diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs index 65aba7e..0e4c0c2 100644 --- a/ipfs-api/src/client.rs +++ b/ipfs-api/src/client.rs @@ -124,15 +124,22 @@ impl IpfsClient { #[inline] pub fn new_from_uri(uri: &str) -> Result { let base_path = IpfsClient::build_base_path(uri)?; + let client = { + #[cfg(feature = "hyper")] + { + Builder::default() + .keep_alive(false) + .build(HttpsConnector::new()) + } + #[cfg(feature = "actix")] + { + Client::default() + } + }; Ok(IpfsClient { base: base_path, - #[cfg(feature = "hyper")] - client: Builder::default() - .keep_alive(false) - .build(HttpsConnector::new()), - #[cfg(feature = "actix")] - client: Client::default(), + client, }) } @@ -146,7 +153,7 @@ impl IpfsClient { /// fn build_base_request( &self, - req: &Req, + req: Req, form: Option>, ) -> Result where @@ -176,14 +183,15 @@ impl IpfsClient { } #[cfg(feature = "actix")] { - if let Some(form) = form { - Ok(self - .client + let req = if let Some(form) = form { + self.client .request(Req::METHOD.clone(), url) - .content_type(form.content_type())) + .content_type(form.content_type()) } else { - Ok(self.client.request(Req::METHOD.clone(), url)) - } + self.client.request(Req::METHOD.clone(), url) + }; + + Ok(req.timeout(std::time::Duration::from_secs(90))) } } @@ -221,8 +229,7 @@ impl IpfsClient { decoder: D, ) -> impl Stream> where - D: 'static + Decoder + Send, - Res: 'static, + D: Decoder + Send, { #[cfg(feature = "hyper")] { @@ -238,7 +245,7 @@ impl IpfsClient { /// fn request_raw( &self, - req: &Req, + req: Req, form: Option>, ) -> impl Future> where @@ -263,11 +270,7 @@ impl IpfsClient { #[cfg(feature = "actix")] { request - .and_then(|req| { - req.timeout(std::time::Duration::from_secs(90)) - .send() - .err_into() - }) + .and_then(|req| req.send().err_into()) .and_then(|mut res| { let status = res.status(); @@ -281,13 +284,12 @@ impl IpfsClient { /// fn request_stream( &self, - req: &Req, + req: Req, form: Option>, process: F, - ) -> impl Stream> + '_ + ) -> impl Stream> where - Req: 'static + ApiRequest + Serialize, - Res: 'static + Send, + Req: ApiRequest + Serialize, F: 'static + Fn(Response) -> AsyncStreamResponse, { let request = future::ready(self.build_base_request(req, form)); @@ -297,7 +299,7 @@ impl IpfsClient { let client = self.client.clone(); request - .and_then(move |req| self.client.request(req).err_into()) + .and_then(move |req| client.request(req).err_into()) .map_ok(move |res| { match res.status() { StatusCode::OK => process(res), @@ -321,11 +323,7 @@ impl IpfsClient { #[cfg(feature = "actix")] { request - .and_then(|req| { - req.timeout(std::time::Duration::from_secs(90)) - .send() - .err_into() - }) + .and_then(|req| req.send().err_into()) .map_ok(move |mut res| { match res.status() { StatusCode::OK => process(res), @@ -352,7 +350,7 @@ impl IpfsClient { /// fn request( &self, - req: &Req, + req: Req, form: Option>, ) -> impl Future> where @@ -369,7 +367,7 @@ impl IpfsClient { /// fn request_empty( &self, - req: &Req, + req: Req, form: Option>, ) -> impl Future> where @@ -388,7 +386,7 @@ impl IpfsClient { /// fn request_string( &self, - req: &Req, + req: Req, form: Option>, ) -> impl Future> where @@ -407,11 +405,11 @@ impl IpfsClient { /// fn request_stream_bytes( &self, - req: &Req, + req: Req, form: Option>, - ) -> impl Stream> + '_ + ) -> impl Stream> where - Req: 'static + ApiRequest + Serialize, + Req: ApiRequest + Serialize, { #[cfg(feature = "hyper")] { @@ -496,7 +494,7 @@ impl IpfsClient { form.add_reader("path", data); - self.request(&request::Add, Some(form)) + self.request(request::Add, Some(form)) } /* @@ -580,7 +578,6 @@ impl IpfsClient { } */ - /* /// Returns the current ledger for a peer. /// /// # Examples @@ -601,9 +598,8 @@ impl IpfsClient { &self, peer: &str, ) -> impl Future> { - self.request(&request::BitswapLedger { peer }, None) + self.request(request::BitswapLedger { peer }, None) } - */ /// Triggers a reprovide. /// @@ -624,7 +620,7 @@ impl IpfsClient { pub fn bitswap_reprovide( &self, ) -> impl Future> { - self.request_empty(&request::BitswapReprovide, None) + self.request_empty(request::BitswapReprovide, None) } /// Returns some stats about the bitswap agent. @@ -646,7 +642,7 @@ impl IpfsClient { pub fn bitswap_stat( &self, ) -> impl Future> { - self.request(&request::BitswapStat, None) + self.request(request::BitswapStat, None) } /* @@ -715,6 +711,7 @@ impl IpfsClient { pub fn block_get(&self, hash: &str) -> AsyncStreamResponse { self.request_stream_bytes(&request::BlockGet { hash }, None) } + */ /// Store input as an IPFS block. /// @@ -734,17 +731,21 @@ impl IpfsClient { /// ``` /// #[inline] - pub fn block_put(&self, data: R) -> AsyncResponse + pub fn block_put( + &self, + data: R, + ) -> impl Future> where - R: 'static + Read + Send, + R: 'static + Read + Send + Sync, { let mut form = multipart::Form::default(); form.add_reader("data", data); - self.request(&request::BlockPut, Some(form)) + self.request(request::BlockPut, Some(form)) } + /* /// Removes an IPFS block. /// /// # Examples @@ -784,6 +785,7 @@ impl IpfsClient { pub fn block_stat(&self, hash: &str) -> AsyncResponse { self.request(&request::BlockStat { hash }, None) } + */ /// Add default peers to the bootstrap list. /// @@ -801,8 +803,10 @@ impl IpfsClient { /// ``` /// #[inline] - pub fn bootstrap_add_default(&self) -> AsyncResponse { - self.request(&request::BootstrapAddDefault, None) + pub fn bootstrap_add_default( + &self, + ) -> impl Future> { + self.request(request::BootstrapAddDefault, None) } /// Lists peers in bootstrap list. @@ -821,8 +825,10 @@ impl IpfsClient { /// ``` /// #[inline] - pub fn bootstrap_list(&self) -> AsyncResponse { - self.request(&request::BootstrapList, None) + pub fn bootstrap_list( + &self, + ) -> impl Future> { + self.request(request::BootstrapList, None) } /// Removes all peers in bootstrap list. @@ -841,10 +847,13 @@ impl IpfsClient { /// ``` /// #[inline] - pub fn bootstrap_rm_all(&self) -> AsyncResponse { - self.request(&request::BootstrapRmAll, None) + pub fn bootstrap_rm_all( + &self, + ) -> impl Future> { + self.request(request::BootstrapRmAll, None) } + /* /// Returns the contents of an Ipfs object. /// /// # Examples diff --git a/ipfs-api/src/read.rs b/ipfs-api/src/read.rs index 3ae35b0..1abb5eb 100644 --- a/ipfs-api/src/read.rs +++ b/ipfs-api/src/read.rs @@ -6,8 +6,7 @@ // copied, modified, or distributed except according to those terms. // -use crate::header::X_STREAM_ERROR; -use crate::response::Error; +use crate::{header::X_STREAM_ERROR, response::Error}; use bytes::{Bytes, BytesMut}; use futures::{ task::{Context, Poll}, -- cgit v1.2.3