summaryrefslogtreecommitdiffstats
path: root/ipfs-api
diff options
context:
space:
mode:
authorJulius Michaelis <gitter@liftm.de>2020-07-12 21:24:55 +0900
committerJulius Michaelis <gitter@liftm.de>2020-07-12 21:24:55 +0900
commitb84a4be646ce2c781b44954989cc02c747794916 (patch)
treee5b6845974de3defcf937e1f92ad9000455fab8b /ipfs-api
parent71f85fc597dd5c5128c61837f50b75a25a00699f (diff)
parentec9c5269467537db4916eab6d5163f6e8b92fbaa (diff)
Merge remote-tracking branch 'origin/master'
Diffstat (limited to 'ipfs-api')
-rw-r--r--ipfs-api/Cargo.toml2
-rw-r--r--ipfs-api/src/client/internal.rs42
-rw-r--r--ipfs-api/src/lib.rs6
-rw-r--r--ipfs-api/src/request/add.rs50
4 files changed, 94 insertions, 6 deletions
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";
}