summaryrefslogtreecommitdiffstats
path: root/ipfs-api
diff options
context:
space:
mode:
authorSameer Puri <sameer@users.noreply.github.com>2019-10-11 15:29:11 -0500
committerSameer Puri <sameer@users.noreply.github.com>2019-10-14 11:03:29 -0500
commit292aed360ce843fd92971db605a51ac0c02b1d0e (patch)
treed15feffae2ba5284405b6387d5e1656c54c700ae /ipfs-api
parent60d4ff7c4f4c2639b09fc8b36b87775a21ff9d76 (diff)
Upgrade actix to v1, bump ipfs-api to v0.5.2
Diffstat (limited to 'ipfs-api')
-rw-r--r--ipfs-api/Cargo.toml20
-rw-r--r--ipfs-api/src/client.rs53
-rw-r--r--ipfs-api/src/lib.rs30
-rw-r--r--ipfs-api/src/response/error.rs96
4 files changed, 117 insertions, 82 deletions
diff --git a/ipfs-api/Cargo.toml b/ipfs-api/Cargo.toml
index a1fb349..7482376 100644
--- a/ipfs-api/Cargo.toml
+++ b/ipfs-api/Cargo.toml
@@ -6,7 +6,7 @@ documentation = "https://docs.rs/ipfs-api"
repository = "https://github.com/ferristseng/rust-ipfs-api"
keywords = ["ipfs"]
categories = ["filesystem", "web-programming"]
-version = "0.5.1"
+version = "0.5.2"
readme = "../README.md"
license = "MIT OR Apache-2.0"
@@ -14,14 +14,16 @@ license = "MIT OR Apache-2.0"
travis-ci = { repository = "ferristseng/rust-ipfs-api" }
[features]
-default = ["hyper", "hyper-multipart-rfc7578", "hyper-tls"]
-actix = ["actix-web", "actix-multipart-rfc7578"]
+default = ["hyper", "hyper-multipart-rfc7578", "hyper-tls", "failure"]
+actix = ["actix-http", "actix-multipart-rfc7578", "awc", "derive_more"]
[dependencies]
-actix-multipart-rfc7578 = { version = "0.1", optional = true }
-actix-web = { version = "0.7", optional = true }
+actix-http = { version = "0.2", optional = true }
+actix-multipart-rfc7578 = { version = "0.2", optional = true }
+awc = { version = "0.2", optional = true }
bytes = "0.4"
-failure = "0.1.2"
+derive_more = { version = "0.15.0", optional = true }
+failure = { version = "0.1.2", optional = true }
futures = "0.1"
http = "0.1"
hyper = { version = "0.12", optional = true }
@@ -39,8 +41,10 @@ dirs = "1.0"
multiaddr = "0.3.1"
[dev-dependencies]
-actix-multipart-rfc7578 = "0.1"
-actix-web = "0.7"
+actix-http = "0.2"
+actix-multipart-rfc7578 = "0.2"
+actix-rt = "0.2"
+awc = "0.2"
hyper = "0.12"
hyper-tls = "0.3.2"
tokio-timer = "0.2"
diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs
index d01ece2..994ab99 100644
--- a/ipfs-api/src/client.rs
+++ b/ipfs-api/src/client.rs
@@ -12,9 +12,9 @@ use crate::read::{JsonLineDecoder, LineDecoder, StreamReader};
use crate::request::{self, ApiRequest};
use crate::response::{self, Error};
#[cfg(feature = "actix")]
-use actix_multipart::client::multipart;
+use actix_http::{encoding, Payload, PayloadStream};
#[cfg(feature = "actix")]
-use actix_web::HttpMessage;
+use actix_multipart::client::multipart;
use bytes::Bytes;
use futures::{
future,
@@ -24,7 +24,7 @@ use futures::{
use http::uri::{InvalidUri, Uri};
use http::StatusCode;
#[cfg(feature = "hyper")]
-use hyper::client::{Client, HttpConnector};
+use hyper::client::{self, Builder, HttpConnector};
#[cfg(feature = "hyper")]
use hyper_tls::HttpsConnector;
use multiaddr::{AddrComponent, ToMultiaddr};
@@ -53,22 +53,26 @@ type AsyncStreamResponse<T> = Box<Stream<Item = T, Error = Error> + 'static>;
type AsyncStreamResponse<T> = Box<dyn Stream<Item = T, Error = Error> + Send + 'static>;
#[cfg(feature = "actix")]
-type Request = actix_web::client::ClientRequest;
+type Request = awc::ClientRequest;
#[cfg(feature = "hyper")]
type Request = http::Request<hyper::Body>;
#[cfg(feature = "actix")]
-type Response = actix_web::client::ClientResponse;
+type Response = awc::ClientResponse<encoding::Decoder<Payload<PayloadStream>>>;
#[cfg(feature = "hyper")]
type Response = http::Response<hyper::Body>;
+#[cfg(feature = "actix")]
+type Client = awc::Client;
+#[cfg(feature = "hyper")]
+type Client = client::Client<HttpsConnector<HttpConnector>, hyper::Body>;
+
/// Asynchronous Ipfs client.
///
#[derive(Clone)]
pub struct IpfsClient {
base: Uri,
- #[cfg(feature = "hyper")]
- client: Client<HttpsConnector<HttpConnector>, hyper::Body>,
+ client: Client,
}
impl Default for IpfsClient {
@@ -128,8 +132,10 @@ impl IpfsClient {
#[cfg(feature = "hyper")]
client: {
let connector = HttpsConnector::new(4).unwrap();
- Client::builder().keep_alive(false).build(connector)
+ Builder::default().keep_alive(false).build(connector)
},
+ #[cfg(feature = "actix")]
+ client: Client::default(),
})
}
@@ -170,19 +176,14 @@ impl IpfsClient {
});
#[cfg(feature = "actix")]
let req = if let Some(form) = form {
- Request::build()
- .method(Req::METHOD.clone())
- .uri(url)
- .content_type(form.content_type())
- .streaming(multipart::Body::from(form))
- .map_err(From::from)
+ Ok(self
+ .client
+ .request(Req::METHOD.clone(), url)
+ .content_type(form.content_type()))
} else {
- Request::build()
- .method(Req::METHOD.clone())
- .uri(url)
- .finish()
- .map_err(From::from)
+ Ok(self.client.request(Req::METHOD.clone(), url))
};
+
req
}
@@ -227,7 +228,7 @@ impl IpfsClient {
);
#[cfg(feature = "actix")]
- let stream = FramedRead::new(StreamReader::new(res.payload().from_err()), decoder);
+ let stream = FramedRead::new(StreamReader::new(res.from_err()), decoder);
Box::new(stream)
}
@@ -258,10 +259,10 @@ impl IpfsClient {
.from_err();
#[cfg(feature = "actix")]
let res = req
- .send()
.timeout(std::time::Duration::from_secs(90))
+ .send()
.from_err()
- .and_then(|x| {
+ .and_then(|mut x| {
let status = x.status();
x.body().map(move |body| (status, body)).from_err()
});
@@ -322,8 +323,8 @@ impl IpfsClient {
match self.build_base_request(req, form) {
Ok(req) => {
let res = req
- .send()
.timeout(std::time::Duration::from_secs(90))
+ .send()
.from_err();
Box::new(res.map(process).flatten_stream())
}
@@ -408,7 +409,7 @@ impl IpfsClient {
Box::new(res.into_body().from_err().map(|c| c.into_bytes()))
});
#[cfg(feature = "actix")]
- let res = self.request_stream(req, form, |res| Box::new(res.payload().from_err()));
+ let res = self.request_stream(req, form, |res| Box::new(res.from_err()));
res
}
@@ -1631,8 +1632,8 @@ impl IpfsClient {
.build_base_request(&request::LogTail, None)
.into_future()
.and_then(|req| {
- req.send()
- .timeout(std::time::Duration::from_secs(90))
+ req.timeout(std::time::Duration::from_secs(90))
+ .send()
.from_err()
})
.map(|res| IpfsClient::process_stream_response(res, LineDecoder))
diff --git a/ipfs-api/src/lib.rs b/ipfs-api/src/lib.rs
index 09914d6..e0bf0b4 100644
--- a/ipfs-api/src/lib.rs
+++ b/ipfs-api/src/lib.rs
@@ -56,11 +56,11 @@
//! #### With Actix
//!
//! ```no_run
-//! # extern crate actix_web;
+//! # extern crate actix_rt;
//! # extern crate futures;
//! # extern crate ipfs_api;
//! #
-//! use futures::future::Future;
+//! use futures::future::{Future, lazy};
//! use ipfs_api::IpfsClient;
//! use std::io::Cursor;
//!
@@ -75,12 +75,11 @@
//! })
//! .map_err(|e| eprintln!("{}", e));
//!
-//! actix_web::actix::run(|| {
-//! req.then(|_| {
-//! actix_web::actix::System::current().stop();
+//! actix_rt::System::new("test").block_on(lazy(|| {
+//! req.and_then(|_| {
//! Ok(())
//! })
-//! });
+//! }));
//! # }
//! ```
//!
@@ -119,10 +118,10 @@
//!
//! ```no_run
//! # extern crate futures;
-//! # extern crate actix_web;
+//! # extern crate actix_rt;
//! # extern crate ipfs_api;
//! #
-//! use futures::{Future, Stream};
+//! use futures::{Future, lazy, Stream};
//! use ipfs_api::IpfsClient;
//! use std::io::{self, Write};
//!
@@ -140,12 +139,11 @@
//! })
//! .map_err(|e| eprintln!("{}", e));
//!
-//! actix_web::actix::run(|| {
-//! req.then(|_| {
-//! actix_web::actix::System::current().stop();
+//! actix_rt::System::new("test").block_on(lazy(|| {
+//! req.and_then(|_| {
//! Ok(())
//! })
-//! });
+//! }));
//! # }
//! ```
//!
@@ -174,9 +172,11 @@
//!
#[cfg(feature = "actix")]
+extern crate actix_http;
+#[cfg(feature = "actix")]
extern crate actix_multipart_rfc7578 as actix_multipart;
#[cfg(feature = "actix")]
-extern crate actix_web;
+extern crate awc;
#[cfg(feature = "hyper")]
extern crate hyper;
@@ -186,7 +186,11 @@ extern crate hyper_multipart_rfc7578 as hyper_multipart;
extern crate hyper_tls;
extern crate bytes;
+#[cfg(feature = "actix")]
#[macro_use]
+extern crate derive_more;
+#[macro_use]
+#[cfg(feature = "hyper")]
extern crate failure;
extern crate futures;
extern crate http;
diff --git a/ipfs-api/src/response/error.rs b/ipfs-api/src/response/error.rs
index ca69de6..e816673 100644
--- a/ipfs-api/src/response/error.rs
+++ b/ipfs-api/src/response/error.rs
@@ -6,7 +6,7 @@
// copied, modified, or distributed except according to those terms.
//
#[cfg(feature = "actix")]
-use actix_web;
+use awc;
use http;
#[cfg(feature = "hyper")]
use hyper;
@@ -15,64 +15,97 @@ use serde_urlencoded;
use std;
use std::string::FromUtf8Error;
-#[derive(Fail, Debug, Deserialize)]
+#[cfg_attr(feature = "actix", derive(Display), display(fmt = "{}", message))]
+#[cfg_attr(feature = "hyper", derive(Fail), fail(display = "{}", message))]
+#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
-#[fail(display = "{}", message)]
pub struct ApiError {
pub message: String,
pub code: u8,
}
-#[derive(Fail, Debug)]
+#[cfg_attr(feature = "actix", derive(Display))]
+#[cfg_attr(feature = "hyper", derive(Fail))]
+#[derive(Debug)]
pub enum Error {
/// Foreign errors.
#[cfg(feature = "hyper")]
- #[fail(display = "hyper client error '{}'", _0)]
+ #[cfg_attr(feature = "hyper", fail(display = "hyper client error '{}'", _0))]
Client(hyper::Error),
#[cfg(feature = "actix")]
- #[fail(display = "actix client error '{}'", _0)]
- Client(actix_web::error::Error),
+ #[cfg_attr(
+ feature = "actix",
+ display(fmt = "actix client payload error '{}'", _0)
+ )]
+ ClientPayload(awc::error::PayloadError),
#[cfg(feature = "actix")]
- #[fail(display = "actix client payload error '{}'", _0)]
- ClientPayload(actix_web::error::PayloadError),
-
- #[cfg(feature = "actix")]
- #[fail(display = "actix client send request error '{}'", _0)]
- ClientSend(actix_web::client::SendRequestError),
-
- #[fail(display = "http error '{}'", _0)]
+ #[cfg_attr(
+ feature = "actix",
+ display(fmt = "actix client send request error '{}'", _0)
+ )]
+ ClientSend(awc::error::SendRequestError),
+
+ #[cfg_attr(feature = "actix", display(fmt = "http error '{}'", _0))]
+ #[cfg_attr(feature = "hyper", fail(display = "http error '{}'", _0))]
Http(http::Error),
- #[fail(display = "json parse error '{}'", _0)]
+ #[cfg_attr(feature = "actix", display(fmt = "json parse error '{}'", _0))]
+ #[cfg_attr(feature = "hyper", fail(display = "json parse error '{}'", _0))]
Parse(serde_json::Error),
- #[fail(display = "utf8 decoding error '{}'", _0)]
+ #[cfg_attr(feature = "actix", display(fmt = "utf8 decoding error '{}'", _0))]
+ #[cfg_attr(feature = "hyper", fail(display = "utf8 decoding error '{}'", _0))]
ParseUtf8(FromUtf8Error),
- #[fail(display = "uri error '{}'", _0)]
+ #[cfg_attr(feature = "actix", display(fmt = "uri error '{}'", _0))]
+ #[cfg_attr(feature = "hyper", fail(display = "uri error '{}'", _0))]
Url(http::uri::InvalidUri),
- #[fail(display = "io error '{}'", _0)]
+ #[cfg_attr(feature = "actix", display(fmt = "io error '{}'", _0))]
+ #[cfg_attr(feature = "hyper", fail(display = "io error '{}'", _0))]
Io(std::io::Error),
- #[fail(display = "url encoding error '{}'", _0)]
+ #[cfg_attr(feature = "actix", display(fmt = "url encoding error '{}'", _0))]
+ #[cfg_attr(feature = "hyper", fail(display = "url encoding error '{}'", _0))]
EncodeUrl(serde_urlencoded::ser::Error),
/// An error returned by the Ipfs api.
- #[fail(display = "api returned error '{}'", _0)]
+ #[cfg_attr(feature = "actix", display(fmt = "api returned error '{}'", _0))]
+ #[cfg_attr(feature = "hyper", fail(display = "api returned error '{}'", _0))]
Api(ApiError),
/// A stream error indicated in the Trailer header.
- #[fail(display = "api returned an error while streaming: '{}'", _0)]
+ #[cfg_attr(
+ feature = "actix",
+ display(fmt = "api returned an error while streaming: '{}'", _0)
+ )]
+ #[cfg_attr(
+ feature = "hyper",
+ fail(display = "api returned an error while streaming: '{}'", _0)
+ )]
StreamError(String),
/// API returned a trailer header with unrecognized value.
- #[fail(display = "api returned a trailer header with unknown value: '{}'", _0)]
+ #[cfg_attr(
+ feature = "actix",
+ display(fmt = "api returned a trailer header with unknown value: '{}'", _0)
+ )]
+ #[cfg_attr(
+ feature = "hyper",
+ fail(display = "api returned a trailer header with unknown value: '{}'", _0)
+ )]
UnrecognizedTrailerHeader(String),
- #[fail(display = "api returned unknwon error '{}'", _0)]
+ #[cfg_attr(
+ feature = "actix",
+ display(fmt = "api returned unknwon error '{}'", _0)
+ )]
+ #[cfg_attr(
+ feature = "hyper",
+ fail(display = "api returned unknwon error '{}'", _0)
+ )]
Uncategorized(String),
}
@@ -84,22 +117,15 @@ impl From<hyper::Error> for Error {
}
#[cfg(feature = "actix")]
-impl From<actix_web::error::Error> for Error {
- fn from(err: actix_web::error::Error) -> Error {
- Error::Client(err)
- }
-}
-
-#[cfg(feature = "actix")]
-impl From<actix_web::client::SendRequestError> for Error {
- fn from(err: actix_web::client::SendRequestError) -> Error {
+impl From<awc::error::SendRequestError> for Error {
+ fn from(err: awc::error::SendRequestError) -> Error {
Error::ClientSend(err)
}
}
#[cfg(feature = "actix")]
-impl From<actix_web::error::PayloadError> for Error {
- fn from(err: actix_web::error::PayloadError) -> Error {
+impl From<awc::error::PayloadError> for Error {
+ fn from(err: awc::error::PayloadError) -> Error {
Error::ClientPayload(err)
}
}