diff options
author | Julius Michaelis <gitter@liftm.de> | 2020-07-12 21:24:55 +0900 |
---|---|---|
committer | Julius Michaelis <gitter@liftm.de> | 2020-07-12 21:24:55 +0900 |
commit | b84a4be646ce2c781b44954989cc02c747794916 (patch) | |
tree | e5b6845974de3defcf937e1f92ad9000455fab8b | |
parent | 71f85fc597dd5c5128c61837f50b75a25a00699f (diff) | |
parent | ec9c5269467537db4916eab6d5163f6e8b92fbaa (diff) |
Merge remote-tracking branch 'origin/master'
-rw-r--r-- | .travis.yml | 3 | ||||
-rw-r--r-- | Cargo.lock | 12 | ||||
-rw-r--r-- | ipfs-api/Cargo.toml | 2 | ||||
-rw-r--r-- | ipfs-api/src/client/internal.rs | 42 | ||||
-rw-r--r-- | ipfs-api/src/lib.rs | 6 | ||||
-rw-r--r-- | ipfs-api/src/request/add.rs | 50 |
6 files changed, 109 insertions, 6 deletions
diff --git a/.travis.yml b/.travis.yml index a166d9f..927c955 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,3 +8,6 @@ script: - cargo test --verbose - cd ipfs-api; cargo build --verbose --features actix --no-default-features - cd ipfs-api; cargo test --verbose --features actix --no-default-features + - cd ipfs-api; cargo build --verbose --features builder + - cd ipfs-api; cargo test --verbose --features builder + - cd ipfs-api; cargo test --verbose --features actix builder --no-default-features @@ -978,6 +978,7 @@ dependencies = [ "tar 0.4.26 (registry+https://github.com/rust-lang/crates.io-index)", "tokio 0.2.21 (registry+https://github.com/rust-lang/crates.io-index)", "tokio-util 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", + "typed-builder 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.3.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1813,6 +1814,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] +name = "typed-builder" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.13 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.22 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] name = "typenum" version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2148,6 +2159,7 @@ dependencies = [ "checksum trust-dns-proto 0.18.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)" = "2a7f3a2ab8a919f5eca52a468866a67ed7d3efa265d48a652a9a3452272b413f" "checksum trust-dns-resolver 0.18.0-alpha.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6f90b1502b226f8b2514c6d5b37bafa8c200d7ca4102d57dc36ee0f3b7a04a2f" "checksum try-lock 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e604eb7b43c06650e854be16a2a03155743d3752dd1c943f6829e26b7a36e382" +"checksum typed-builder 0.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "85fc4459191c621a53ef6c6ca5642e6e0e5ccc61f3e5b8ad6b6ab5317f0200fb" "checksum typenum 1.12.0 (registry+https://github.com/rust-lang/crates.io-index)" = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" "checksum unicode-bidi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "49f2bd0c6468a8230e1db229cff8029217cf623c767ea5d60bfbd42729ea54d5" "checksum unicode-normalization 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "5479532badd04e128284890390c1e876ef7a993d0570b3597ae43dfa1d59afa4" diff --git a/ipfs-api/Cargo.toml b/ipfs-api/Cargo.toml index b216fae..76deb6e 100644 --- a/ipfs-api/Cargo.toml +++ b/ipfs-api/Cargo.toml @@ -17,6 +17,7 @@ travis-ci = { repository = "ferristseng/rust-ipfs-api" } [features] default = ["hyper", "hyper-multipart-rfc7578", "hyper-tls", "failure"] actix = ["actix-http", "actix-multipart-rfc7578", "awc", "derive_more"] +builder = ["typed-builder"] [dependencies] actix-http = { version = "1.0", optional = true } @@ -38,6 +39,7 @@ tokio-util = { version = "0.2", features = ["codec"] } walkdir = "2.2" dirs = "2.0" parity-multiaddr = "0.7.3" +typed-builder = { version = "0.6", optional = true } [dev-dependencies] actix-http = "1.0" diff --git a/ipfs-api/src/client/internal.rs b/ipfs-api/src/client/internal.rs index d59288c..885f6a4 100644 --- a/ipfs-api/src/client/internal.rs +++ b/ipfs-api/src/client/internal.rs @@ -444,11 +444,49 @@ impl IpfsClient { where R: 'static + Read + Send + Sync, { + self.add_with_options(data, request::Add::default()).await + } + + /// Add a file to IPFS with options. + /// + /// # Examples + /// + /// ```no_run + /// # extern crate ipfs_api; + /// # + /// use ipfs_api::IpfsClient; + /// use std::io::Cursor; + /// + /// # fn main() { + /// let client = IpfsClient::default(); + /// let data = Cursor::new("Hello World!"); + /// #[cfg(feature = "builder")] + /// let add = ipfs_api::request::Add::builder() + /// .chunker("rabin-512-1024-2048") + /// .build(); + /// #[cfg(not(feature = "builder"))] + /// let add = ipfs_api::request::Add { + /// chunker: Some("rabin-512-1024-2048"), + /// ..Default::default() + /// }; + /// let req = client.add_with_options(data, add); + /// # } + /// ``` + /// + #[inline] + pub async fn add_with_options<R>( + &self, + data: R, + add: request::Add<'_>, + ) -> Result<response::AddResponse, Error> + where + R: 'static + Read + Send + Sync, + { let mut form = multipart::Form::default(); form.add_reader("path", data); - self.request(request::Add, Some(form)).await + self.request(add, Some(form)).await } /// Add a path to Ipfs. Can be a file or directory. @@ -515,7 +553,7 @@ impl IpfsClient { } } - let req = self.build_base_request(request::Add, Some(form))?; + let req = self.build_base_request(request::Add::default(), Some(form))?; self.request_stream_json(req).try_collect().await } diff --git a/ipfs-api/src/lib.rs b/ipfs-api/src/lib.rs index d74a913..6692649 100644 --- a/ipfs-api/src/lib.rs +++ b/ipfs-api/src/lib.rs @@ -154,13 +154,17 @@ extern crate failure; extern crate serde; +#[cfg(feature = "builder")] +#[macro_use] +extern crate typed_builder; + pub use crate::client::{IpfsClient, TryFromUri}; pub use crate::request::{KeyType, Logger, LoggingLevel, ObjectTemplate}; mod client; mod header; mod read; -mod request; +pub mod request; pub mod response; #[cfg(feature = "actix")] diff --git a/ipfs-api/src/request/add.rs b/ipfs-api/src/request/add.rs index 70eb89a..72001c5 100644 --- a/ipfs-api/src/request/add.rs +++ b/ipfs-api/src/request/add.rs @@ -7,11 +7,55 @@ // use crate::request::ApiRequest; +use serde::Serialize; -pub struct Add; +#[cfg_attr(feature = "builder", derive(TypedBuilder))] +#[derive(Serialize, Default)] +#[serde(rename_all = "kebab-case")] +pub struct Add<'a> { + /// Use trickle-dag format for dag generation. + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub trickle: Option<bool>, -impl_skip_serialize!(Add); + /// Only chunk and hash - do not write to disk. + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub only_hash: Option<bool>, -impl ApiRequest for Add { + /// Wrap files with a directory object. + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub wrap_with_directory: Option<bool>, + + /// Chunking algorithm, `size-[bytes]`, `rabin-[min]-[avg]-[max]` or `buzhash`. + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub chunker: Option<&'a str>, + + /// Pin this object when adding. Defaults to `true`. + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub pin: Option<bool>, + + /// Use raw blocks for leaf nodes. (experimental). + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub raw_leaves: Option<bool>, + + /// CID version. Defaults to 0 unless an option that depends on CIDv1 is passed. + /// (experimental). + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub cid_version: Option<u32>, + + /// Hash function to use. Implies CIDv1 if not sha2-256. (experimental). Default: + /// `sha2-256`. + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub hash: Option<&'a str>, + + /// Inline small blocks into CIDs. (experimental). + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub inline: Option<bool>, + + /// Maximum block size to inline. (experimental). Default: `32`. + #[cfg_attr(feature = "builder", builder(default, setter(strip_option)))] + pub inline_limit: Option<u32>, +} + +impl<'a> ApiRequest for Add<'a> { const PATH: &'static str = "/add"; } |