From 89519b4c5ce4839a362ab9ec9c6359f3c6aa844b Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 20:42:28 -0500 Subject: add actix backend --- ipfs-api-backend-actix/Cargo.toml | 26 +++++++ ipfs-api-backend-actix/src/backend.rs | 140 ++++++++++++++++++++++++++++++++++ ipfs-api-backend-actix/src/error.rs | 46 +++++++++++ ipfs-api-backend-actix/src/lib.rs | 14 ++++ 4 files changed, 226 insertions(+) create mode 100644 ipfs-api-backend-actix/Cargo.toml create mode 100644 ipfs-api-backend-actix/src/backend.rs create mode 100644 ipfs-api-backend-actix/src/error.rs create mode 100644 ipfs-api-backend-actix/src/lib.rs (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/Cargo.toml b/ipfs-api-backend-actix/Cargo.toml new file mode 100644 index 0000000..3a616a6 --- /dev/null +++ b/ipfs-api-backend-actix/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "ipfs-api-backend-actix" +description = "Actix implementation of IPFS HTTP API" +authors = ["Ferris Tseng "] +edition = "2018" +documentation = "https://docs.rs/ipfs-api" +repository = "https://github.com/ferristseng/rust-ipfs-api" +keywords = ["ipfs"] +categories = ["filesystem", "web-programming"] +version = "0.1.0" +readme = "../README.md" +license = "MIT OR Apache-2.0" + +[dependencies] +actix-http = "2.2" +actix-multipart-rfc7578 = "0.4" +awc = "2.0" +async-trait = "0.1" +bytes = "1.0" +futures = "0.3" +http = "0.2" +ipfs-api-prelude = { version = "0.1.0", path = "../ipfs-api-prelude" } +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +serde_urlencoded = "0.7" +thiserror = "1.0" diff --git a/ipfs-api-backend-actix/src/backend.rs b/ipfs-api-backend-actix/src/backend.rs new file mode 100644 index 0000000..6aa8f50 --- /dev/null +++ b/ipfs-api-backend-actix/src/backend.rs @@ -0,0 +1,140 @@ +use crate::error::Error; +use async_trait::async_trait; +use awc::Client; +use bytes::Bytes; +use futures::{FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt}; +use http::{ + header::{HeaderName, HeaderValue}, + uri::Scheme, + StatusCode, Uri, +}; +use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri}; +use serde::Serialize; +use std::time::Duration; + +const ACTIX_REQUEST_TIMEOUT: Duration = Duration::from_secs(90); + +pub struct ActixBackend { + base: Uri, + client: Client, +} + +impl Default for ActixBackend { + fn default() -> Self { + Self::from_ipfs_config() + .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) + } +} + +impl TryFromUri for ActixBackend { + fn build_with_base_uri(base: Uri) -> Self { + let client = Client::default(); + + ActixBackend { base, client } + } +} + +#[async_trait(?Send)] +impl Backend for ActixBackend { + type HttpRequest = awc::SendClientRequest; + + type HttpResponse = awc::ClientResponse< + actix_http::encoding::Decoder>, + >; + + type MultipartForm = multipart::client::multipart::Form<'static>; + + type Error = Error; + + fn build_base_request( + &self, + req: &Req, + form: Option, + ) -> Result + where + Req: ApiRequest, + { + req.absolute_url(&self.base).and_then(|url| { + let req = if let Some(form) = form { + self.client + .post(url) + .timeout(ACTIX_REQUEST_TIMEOUT) + .content_type(form.content_type()) + .send_body(multipart::client::multipart::Body::from(form)) + } else { + self.client.post(url).timeout(ACTIX_REQUEST_TIMEOUT).send() + }; + + Ok(req) + }) + } + + fn get_header<'a>(res: &'a Self::HttpResponse, key: HeaderName) -> Option<&'a HeaderValue> { + res.headers().get(key) + } + + async fn request_raw( + &self, + req: Req, + form: Option, + ) -> Result<(StatusCode, Bytes), Self::Error> + where + Req: ApiRequest + Serialize, + { + let req = self.build_base_request(&req, form)?; + let mut res = req.await?; + let status = res.status(); + let body = res.body().await?; + + // FIXME: Actix compat with bytes 1.0 + Ok((status, Bytes::copy_from_slice(body.as_ref()))) + } + + fn response_to_byte_stream( + res: Self::HttpResponse, + ) -> Box> + Unpin> { + let stream = res + .map_ok(|bytes| Bytes::copy_from_slice(bytes.as_ref())) + .err_into(); + + Box::new(stream) + } + + fn request_stream( + &self, + req: Self::HttpRequest, + process: F, + ) -> Box> + Unpin> + where + OutStream: Stream> + Unpin, + F: 'static + Fn(Self::HttpResponse) -> OutStream, + { + let stream = req + .err_into() + .map_ok(move |mut res| { + match res.status() { + StatusCode::OK => process(res).right_stream(), + // If the server responded with an error status code, the body + // still needs to be read so an error can be built. This block will + // read the entire body stream, then immediately return an error. + // + _ => res + .body() + .map(|maybe_body| match maybe_body { + Ok(body) => { + // FIXME: Actix compat with bytes 1.0 + let body = Bytes::copy_from_slice(body.as_ref()); + + Err(Self::process_error_from_body(body)) + } + Err(e) => Err(e.into()), + }) + .into_stream() + .left_stream(), + } + }) + .try_flatten_stream(); + + Box::new(stream) + } +} diff --git a/ipfs-api-backend-actix/src/error.rs b/ipfs-api-backend-actix/src/error.rs new file mode 100644 index 0000000..4cd9a25 --- /dev/null +++ b/ipfs-api-backend-actix/src/error.rs @@ -0,0 +1,46 @@ +// Copyright 2017 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +use std::string::FromUtf8Error; +use thiserror::Error; + +#[derive(Debug, Error)] +pub enum Error { + #[error("api returned error `{0}`")] + Api(ipfs_api_prelude::ApiError), + + #[error("actix client payload error `{0}`")] + ClientPayload(#[from] awc::error::PayloadError), + + #[error("actix client send request error `{0}`")] + ClientSend(#[from] awc::error::SendRequestError), + + #[error("http error `{0}`")] + Http(#[from] http::Error), + + #[error("json parse error `{0}`")] + Parse(#[from] serde_json::Error), + + #[error("utf8 decoding error `{0}`")] + ParseUtf8(#[from] FromUtf8Error), + + #[error("uri error `{0}`")] + Url(#[from] http::uri::InvalidUri), + + #[error("url encoding error `{0}`")] + EncodeUrl(#[from] serde_urlencoded::ser::Error), + + #[error("ipfs client error `{0}`")] + IpfsClientError(#[from] ipfs_api_prelude::Error), +} + +impl From for Error { + fn from(err: ipfs_api_prelude::ApiError) -> Self { + Error::Api(err) + } +} diff --git a/ipfs-api-backend-actix/src/lib.rs b/ipfs-api-backend-actix/src/lib.rs new file mode 100644 index 0000000..414aff8 --- /dev/null +++ b/ipfs-api-backend-actix/src/lib.rs @@ -0,0 +1,14 @@ +// Copyright 2019 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + +extern crate actix_multipart_rfc7578 as multipart; + +mod backend; +mod error; + +pub use crate::{backend::ActixBackend as IpfsApi, error::Error}; -- cgit v1.2.3 From 4ede307efb87bad2627190665279123e588a3494 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 22:56:04 -0500 Subject: centralize errors in prelude as much as possible --- ipfs-api-backend-actix/Cargo.toml | 4 +--- ipfs-api-backend-actix/src/backend.rs | 23 ++++++++++------------- ipfs-api-backend-actix/src/error.rs | 13 ------------- 3 files changed, 11 insertions(+), 29 deletions(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/Cargo.toml b/ipfs-api-backend-actix/Cargo.toml index 3a616a6..22e4ba9 100644 --- a/ipfs-api-backend-actix/Cargo.toml +++ b/ipfs-api-backend-actix/Cargo.toml @@ -20,7 +20,5 @@ bytes = "1.0" futures = "0.3" http = "0.2" ipfs-api-prelude = { version = "0.1.0", path = "../ipfs-api-prelude" } -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -serde_urlencoded = "0.7" +serde = "1.0" thiserror = "1.0" diff --git a/ipfs-api-backend-actix/src/backend.rs b/ipfs-api-backend-actix/src/backend.rs index 6aa8f50..f61d70d 100644 --- a/ipfs-api-backend-actix/src/backend.rs +++ b/ipfs-api-backend-actix/src/backend.rs @@ -54,19 +54,16 @@ impl Backend for ActixBackend { where Req: ApiRequest, { - req.absolute_url(&self.base).and_then(|url| { - let req = if let Some(form) = form { - self.client - .post(url) - .timeout(ACTIX_REQUEST_TIMEOUT) - .content_type(form.content_type()) - .send_body(multipart::client::multipart::Body::from(form)) - } else { - self.client.post(url).timeout(ACTIX_REQUEST_TIMEOUT).send() - }; - - Ok(req) - }) + let url = req.absolute_url(&self.base)?; + let req = self.client.request(Req::METHOD, url); + let req = if let Some(form) = form { + req.content_type(form.content_type()) + .send_body(multipart::Body::from(form)) + } else { + req.timeout(ACTIX_REQUEST_TIMEOUT).send() + }; + + Ok(req) } fn get_header<'a>(res: &'a Self::HttpResponse, key: HeaderName) -> Option<&'a HeaderValue> { diff --git a/ipfs-api-backend-actix/src/error.rs b/ipfs-api-backend-actix/src/error.rs index 4cd9a25..aad2b05 100644 --- a/ipfs-api-backend-actix/src/error.rs +++ b/ipfs-api-backend-actix/src/error.rs @@ -6,7 +6,6 @@ // copied, modified, or distributed except according to those terms. // -use std::string::FromUtf8Error; use thiserror::Error; #[derive(Debug, Error)] @@ -23,18 +22,6 @@ pub enum Error { #[error("http error `{0}`")] Http(#[from] http::Error), - #[error("json parse error `{0}`")] - Parse(#[from] serde_json::Error), - - #[error("utf8 decoding error `{0}`")] - ParseUtf8(#[from] FromUtf8Error), - - #[error("uri error `{0}`")] - Url(#[from] http::uri::InvalidUri), - - #[error("url encoding error `{0}`")] - EncodeUrl(#[from] serde_urlencoded::ser::Error), - #[error("ipfs client error `{0}`")] IpfsClientError(#[from] ipfs_api_prelude::Error), } -- cgit v1.2.3 From 93e8c2e2ad4a3c755a707d268ede8e661c45c68d Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 22:56:45 -0500 Subject: use common form obj --- ipfs-api-backend-actix/src/backend.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/src/backend.rs b/ipfs-api-backend-actix/src/backend.rs index f61d70d..21da5c9 100644 --- a/ipfs-api-backend-actix/src/backend.rs +++ b/ipfs-api-backend-actix/src/backend.rs @@ -9,6 +9,7 @@ use http::{ StatusCode, Uri, }; use ipfs_api_prelude::{ApiRequest, Backend, TryFromUri}; +use multipart::client::multipart; use serde::Serialize; use std::time::Duration; @@ -42,14 +43,12 @@ impl Backend for ActixBackend { actix_http::encoding::Decoder>, >; - type MultipartForm = multipart::client::multipart::Form<'static>; - type Error = Error; fn build_base_request( &self, req: &Req, - form: Option, + form: Option>, ) -> Result where Req: ApiRequest, @@ -73,7 +72,7 @@ impl Backend for ActixBackend { async fn request_raw( &self, req: Req, - form: Option, + form: Option>, ) -> Result<(StatusCode, Bytes), Self::Error> where Req: ApiRequest + Serialize, -- cgit v1.2.3 From 5cb668119fdf4bc400a5b8432f2aec7547f1e717 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 22 Feb 2021 22:57:11 -0500 Subject: expose stuff in crates --- ipfs-api-backend-actix/src/lib.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/src/lib.rs b/ipfs-api-backend-actix/src/lib.rs index 414aff8..10345a6 100644 --- a/ipfs-api-backend-actix/src/lib.rs +++ b/ipfs-api-backend-actix/src/lib.rs @@ -11,4 +11,5 @@ extern crate actix_multipart_rfc7578 as multipart; mod backend; mod error; -pub use crate::{backend::ActixBackend as IpfsApi, error::Error}; +pub use crate::{backend::ActixBackend as IpfsClient, error::Error}; +pub use ipfs_api_prelude::{request, response, IpfsApi}; -- cgit v1.2.3 From 2d32fa5de144690fcd7d26064a1a4e66c5a7c0b4 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:24:18 -0500 Subject: export right stuff to make doc examples work --- ipfs-api-backend-actix/src/lib.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/src/lib.rs b/ipfs-api-backend-actix/src/lib.rs index 10345a6..d442bcf 100644 --- a/ipfs-api-backend-actix/src/lib.rs +++ b/ipfs-api-backend-actix/src/lib.rs @@ -12,4 +12,7 @@ mod backend; mod error; pub use crate::{backend::ActixBackend as IpfsClient, error::Error}; -pub use ipfs_api_prelude::{request, response, IpfsApi}; +pub use ipfs_api_prelude::{ + request::{self, KeyType, Logger, LoggingLevel, ObjectTemplate}, + response, IpfsApi, TryFromUri, +}; -- cgit v1.2.3 From 2263944af0da6b0b837285af78dafad765853853 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:25:19 -0500 Subject: add documentation --- ipfs-api-backend-actix/src/backend.rs | 3 +++ 1 file changed, 3 insertions(+) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/src/backend.rs b/ipfs-api-backend-actix/src/backend.rs index 21da5c9..73fdf0b 100644 --- a/ipfs-api-backend-actix/src/backend.rs +++ b/ipfs-api-backend-actix/src/backend.rs @@ -21,6 +21,9 @@ pub struct ActixBackend { } impl Default for ActixBackend { + /// Creates an `IpfsClient` connected to the endpoint specified in ~/.ipfs/api. + /// If not found, tries to connect to `localhost:5001`. + /// fn default() -> Self { Self::from_ipfs_config() .unwrap_or_else(|| Self::from_host_and_port(Scheme::HTTP, "localhost", 5001).unwrap()) -- cgit v1.2.3 From dd03b0edb4928894b64b85cc44a4984a721650c2 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:44:34 -0500 Subject: fixing actix examples by upgrading to new rt --- ipfs-api-backend-actix/Cargo.toml | 6 +++--- ipfs-api-backend-actix/src/backend.rs | 13 +++---------- 2 files changed, 6 insertions(+), 13 deletions(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/Cargo.toml b/ipfs-api-backend-actix/Cargo.toml index 22e4ba9..a0760d8 100644 --- a/ipfs-api-backend-actix/Cargo.toml +++ b/ipfs-api-backend-actix/Cargo.toml @@ -12,9 +12,9 @@ readme = "../README.md" license = "MIT OR Apache-2.0" [dependencies] -actix-http = "2.2" -actix-multipart-rfc7578 = "0.4" -awc = "2.0" +actix-http = "3.0.0-beta.2" +actix-multipart-rfc7578 = { version = "0.5", git = "https://github.com/ferristseng/rust-multipart-rfc7578", branch = "ftseng/upgrade-actix-rt" } +awc = "3.0.0-beta.2" async-trait = "0.1" bytes = "1.0" futures = "0.3" diff --git a/ipfs-api-backend-actix/src/backend.rs b/ipfs-api-backend-actix/src/backend.rs index 73fdf0b..fbbc6a4 100644 --- a/ipfs-api-backend-actix/src/backend.rs +++ b/ipfs-api-backend-actix/src/backend.rs @@ -86,15 +86,13 @@ impl Backend for ActixBackend { let body = res.body().await?; // FIXME: Actix compat with bytes 1.0 - Ok((status, Bytes::copy_from_slice(body.as_ref()))) + Ok((status, body)) } fn response_to_byte_stream( res: Self::HttpResponse, ) -> Box> + Unpin> { - let stream = res - .map_ok(|bytes| Bytes::copy_from_slice(bytes.as_ref())) - .err_into(); + let stream = res.err_into(); Box::new(stream) } @@ -120,12 +118,7 @@ impl Backend for ActixBackend { _ => res .body() .map(|maybe_body| match maybe_body { - Ok(body) => { - // FIXME: Actix compat with bytes 1.0 - let body = Bytes::copy_from_slice(body.as_ref()); - - Err(Self::process_error_from_body(body)) - } + Ok(body) => Err(Self::process_error_from_body(body)), Err(e) => Err(e.into()), }) .into_stream() -- cgit v1.2.3 From fc3cd21efe52b0575617bcd249b30e8846ad2278 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Tue, 23 Feb 2021 23:50:30 -0500 Subject: update copyright headers --- ipfs-api-backend-actix/src/backend.rs | 8 ++++++++ ipfs-api-backend-actix/src/error.rs | 2 +- ipfs-api-backend-actix/src/lib.rs | 2 +- 3 files changed, 10 insertions(+), 2 deletions(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/src/backend.rs b/ipfs-api-backend-actix/src/backend.rs index fbbc6a4..b4b23c7 100644 --- a/ipfs-api-backend-actix/src/backend.rs +++ b/ipfs-api-backend-actix/src/backend.rs @@ -1,3 +1,11 @@ +// Copyright 2021 rust-ipfs-api Developers +// +// Licensed under the Apache License, Version 2.0, or the MIT license , at your option. This file may not be +// copied, modified, or distributed except according to those terms. +// + use crate::error::Error; use async_trait::async_trait; use awc::Client; diff --git a/ipfs-api-backend-actix/src/error.rs b/ipfs-api-backend-actix/src/error.rs index aad2b05..99af54d 100644 --- a/ipfs-api-backend-actix/src/error.rs +++ b/ipfs-api-backend-actix/src/error.rs @@ -1,4 +1,4 @@ -// Copyright 2017 rust-ipfs-api Developers +// Copyright 2021 rust-ipfs-api Developers // // Licensed under the Apache License, Version 2.0, or the MIT license or the MIT license Date: Sun, 4 Apr 2021 18:48:28 -0400 Subject: upgrade dependency --- ipfs-api-backend-actix/Cargo.toml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/Cargo.toml b/ipfs-api-backend-actix/Cargo.toml index a0760d8..ef99591 100644 --- a/ipfs-api-backend-actix/Cargo.toml +++ b/ipfs-api-backend-actix/Cargo.toml @@ -12,9 +12,9 @@ readme = "../README.md" license = "MIT OR Apache-2.0" [dependencies] -actix-http = "3.0.0-beta.2" -actix-multipart-rfc7578 = { version = "0.5", git = "https://github.com/ferristseng/rust-multipart-rfc7578", branch = "ftseng/upgrade-actix-rt" } -awc = "3.0.0-beta.2" +actix-http = "3.0.0-beta.5" +actix-multipart-rfc7578 = "0.5" +awc = "3.0.0-beta.4" async-trait = "0.1" bytes = "1.0" futures = "0.3" -- cgit v1.2.3 From 8ae619d45b39e70d85215795b5e91e3944a30fc9 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Sun, 4 Apr 2021 19:57:45 -0400 Subject: more clippy changes --- ipfs-api-backend-actix/src/backend.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ipfs-api-backend-actix') diff --git a/ipfs-api-backend-actix/src/backend.rs b/ipfs-api-backend-actix/src/backend.rs index b4b23c7..dc55230 100644 --- a/ipfs-api-backend-actix/src/backend.rs +++ b/ipfs-api-backend-actix/src/backend.rs @@ -76,7 +76,7 @@ impl Backend for ActixBackend { Ok(req) } - fn get_header<'a>(res: &'a Self::HttpResponse, key: HeaderName) -> Option<&'a HeaderValue> { + fn get_header(res: &Self::HttpResponse, key: HeaderName) -> Option<&HeaderValue> { res.headers().get(key) } -- cgit v1.2.3