summaryrefslogtreecommitdiffstats
path: root/ipfs-api-prelude
diff options
context:
space:
mode:
authorFerris Tseng <ferristseng@fastmail.fm>2021-04-04 20:06:34 -0400
committerGitHub <noreply@github.com>2021-04-04 20:06:34 -0400
commit799f702b22a9b567c0b6b7674210583b02b10d55 (patch)
tree92d7a1c9dcac42599357097c53caccfbdcaa8105 /ipfs-api-prelude
parent4c78f5759c60499eefd4527ea34d574c2fb35c14 (diff)
parent168f3a5e68780c37f4070ea9d42ac70bd0158292 (diff)
Merge pull request #72 from ferristseng/ftseng-divide-cratesHEADmaster
Divide backends into separate crates
Diffstat (limited to 'ipfs-api-prelude')
-rw-r--r--ipfs-api-prelude/Cargo.toml39
-rw-r--r--ipfs-api-prelude/src/api.rs2098
-rw-r--r--ipfs-api-prelude/src/backend.rs232
-rw-r--r--ipfs-api-prelude/src/error.rs37
-rw-r--r--ipfs-api-prelude/src/from_uri.rs203
-rw-r--r--ipfs-api-prelude/src/header.rs12
-rw-r--r--ipfs-api-prelude/src/lib.rs27
-rw-r--r--ipfs-api-prelude/src/read.rs226
-rw-r--r--ipfs-api-prelude/src/request/add.rs61
-rw-r--r--ipfs-api-prelude/src/request/bitswap.rs55
-rw-r--r--ipfs-api-prelude/src/request/block.rs48
-rw-r--r--ipfs-api-prelude/src/request/bootstrap.rs33
-rw-r--r--ipfs-api-prelude/src/request/cat.rs20
-rw-r--r--ipfs-api-prelude/src/request/commands.rs17
-rw-r--r--ipfs-api-prelude/src/request/config.rs53
-rw-r--r--ipfs-api-prelude/src/request/dag.rs28
-rw-r--r--ipfs-api-prelude/src/request/dht.rs73
-rw-r--r--ipfs-api-prelude/src/request/diag.rs36
-rw-r--r--ipfs-api-prelude/src/request/dns.rs22
-rw-r--r--ipfs-api-prelude/src/request/file.rs20
-rw-r--r--ipfs-api-prelude/src/request/files.rs201
-rw-r--r--ipfs-api-prelude/src/request/filestore.rs38
-rw-r--r--ipfs-api-prelude/src/request/get.rs20
-rw-r--r--ipfs-api-prelude/src/request/id.rs20
-rw-r--r--ipfs-api-prelude/src/request/key.rs78
-rw-r--r--ipfs-api-prelude/src/request/log.rs85
-rw-r--r--ipfs-api-prelude/src/request/ls.rs55
-rw-r--r--ipfs-api-prelude/src/request/mod.rs130
-rw-r--r--ipfs-api-prelude/src/request/name.rs42
-rw-r--r--ipfs-api-prelude/src/request/object.rs105
-rw-r--r--ipfs-api-prelude/src/request/pin.rs48
-rw-r--r--ipfs-api-prelude/src/request/ping.rs22
-rw-r--r--ipfs-api-prelude/src/request/pubsub.rs53
-rw-r--r--ipfs-api-prelude/src/request/refs.rs17
-rw-r--r--ipfs-api-prelude/src/request/shutdown.rs17
-rw-r--r--ipfs-api-prelude/src/request/stats.rs33
-rw-r--r--ipfs-api-prelude/src/request/swarm.rs25
-rw-r--r--ipfs-api-prelude/src/request/tar.rs28
-rw-r--r--ipfs-api-prelude/src/request/version.rs17
-rw-r--r--ipfs-api-prelude/src/response/add.rs17
-rw-r--r--ipfs-api-prelude/src/response/bitswap.rs55
-rw-r--r--ipfs-api-prelude/src/response/block.rs35
-rw-r--r--ipfs-api-prelude/src/response/bootstrap.rs36
-rw-r--r--ipfs-api-prelude/src/response/commands.rs34
-rw-r--r--ipfs-api-prelude/src/response/config.rs23
-rw-r--r--ipfs-api-prelude/src/response/dag.rs46
-rw-r--r--ipfs-api-prelude/src/response/dht.rs85
-rw-r--r--ipfs-api-prelude/src/response/diag.rs13
-rw-r--r--ipfs-api-prelude/src/response/dns.rs15
-rw-r--r--ipfs-api-prelude/src/response/error.rs23
-rw-r--r--ipfs-api-prelude/src/response/file.rs40
-rw-r--r--ipfs-api-prelude/src/response/files.rs68
-rw-r--r--ipfs-api-prelude/src/response/filestore.rs33
-rw-r--r--ipfs-api-prelude/src/response/id.rs30
-rw-r--r--ipfs-api-prelude/src/response/key.rs46
-rw-r--r--ipfs-api-prelude/src/response/log.rs28
-rw-r--r--ipfs-api-prelude/src/response/ls.rs43
-rw-r--r--ipfs-api-prelude/src/response/mod.rs106
-rw-r--r--ipfs-api-prelude/src/response/mount.rs26
-rw-r--r--ipfs-api-prelude/src/response/name.rs27
-rw-r--r--ipfs-api-prelude/src/response/object.rs123
-rw-r--r--ipfs-api-prelude/src/response/pin.rs47
-rw-r--r--ipfs-api-prelude/src/response/ping.rs24
-rw-r--r--ipfs-api-prelude/src/response/pubsub.rs48
-rw-r--r--ipfs-api-prelude/src/response/refs.rs23
-rw-r--r--ipfs-api-prelude/src/response/repo.rs58
-rw-r--r--ipfs-api-prelude/src/response/resolve.rs20
-rw-r--r--ipfs-api-prelude/src/response/serde.rs171
-rw-r--r--ipfs-api-prelude/src/response/shutdown.rs9
-rw-r--r--ipfs-api-prelude/src/response/stats.rs28
-rw-r--r--ipfs-api-prelude/src/response/swarm.rs78
-rw-r--r--ipfs-api-prelude/src/response/tar.rs21
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_bitswap_stat_0.json352
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_block_stat_0.json4
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_bootstrap_list_0.json22
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_commands_0.json1465
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_dag_get_0.json19
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_file_ls_0.json50
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_file_ls_1.json4
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_files_ls_0.json3
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_files_stat_0.json7
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_id_0.json9
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_key_gen_0.json4
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_key_list_0.json16
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_key_rename_0.json6
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_key_rm_0.json8
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_log_ls_0.json71
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_ls_0.json75
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_ls_1.json45
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_mount_0.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_name_resolve_0.json3
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_object_diff_0.json14
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_object_links_0.json40
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_object_stat_0.json8
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_pin_add_0.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_pin_ls_0.json7
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_ping_0.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_ping_1.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_ping_2.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_pubsub_ls_0.json3
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_pubsub_ls_1.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_pubsub_peers_0.json1
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_pubsub_sub_0.json8
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_pubsub_sub_1.json1
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_refs_local_0.json4
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_repo_gc_0.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_repo_stat_0.json1
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_repo_verify_0.json4
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_repo_verify_1.json4
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_repo_version_0.json3
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_resolve_0.json3
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_stats_bw_0.json6
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_swarm_addrs_local_0.json8
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_swarm_peers_0.json88
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_swarm_peers_1.json18
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_swarm_peers_2.json32
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_tar_add_0.json5
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_version_0.json7
-rw-r--r--ipfs-api-prelude/src/response/tests/v0_version_1.json5
-rw-r--r--ipfs-api-prelude/src/response/version.rs29
120 files changed, 8350 insertions, 0 deletions
diff --git a/ipfs-api-prelude/Cargo.toml b/ipfs-api-prelude/Cargo.toml
new file mode 100644
index 0000000..9cedf4c
--- /dev/null
+++ b/ipfs-api-prelude/Cargo.toml
@@ -0,0 +1,39 @@
+[package]
+name = "ipfs-api-prelude"
+description = "Shared code for IPFS HTTP API clients"
+authors = ["Ferris Tseng <ferristseng@fastmail.fm>"]
+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"
+
+[features]
+with-builder = ["typed-builder"]
+
+[dependencies]
+async-trait = "0.1"
+bytes = "1.0"
+common-multipart-rfc7578 = "0.3"
+dirs = "3.0"
+futures = "0.3"
+http = "0.2"
+parity-multiaddr = "0.11"
+serde = { version = "1.0", features = ["derive"] }
+serde_json = "1.0"
+serde_urlencoded = "0.7"
+thiserror = "1.0"
+tokio = "1"
+tokio-util = { version = "0.6", features = ["codec"] }
+tracing = "0.1"
+typed-builder = { version = "0.9", optional = true }
+walkdir = "2.3"
+
+[dev-dependencies]
+ipfs-api = { package = "ipfs-api-backend-hyper", version = "0.1", path = "../ipfs-api-backend-hyper" }
+tokio = { version = "1", features = ["rt-multi-thread", "macros", "time"] }
+tokio-stream = { version = "0.1", features = ["time"] }
+tracing-subscriber = { version = "0.2", features = ["fmt"] }
diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs
new file mode 100644
index 0000000..48ed893
--- /dev/null
+++ b/ipfs-api-prelude/src/api.rs
@@ -0,0 +1,2098 @@
+// Copyright 2021 rust-ipfs-api Developers
+//
+// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
+// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
+// http://opensource.org/licenses/MIT>, at your option. This file may not be
+// copied, modified, or distributed except according to those terms.
+//
+
+use crate::{read::LineDecoder, request, response, Backend};
+use async_trait::async_trait;
+use bytes::Bytes;
+use common_multipart_rfc7578::client::multipart;
+use futures::{future, FutureExt, Stream, TryStreamExt};
+use std::{
+ fs::File,
+ io::{Cursor, Read},
+ path::{Path, PathBuf},
+};
+
+const FILE_DESCRIPTOR_LIMIT: usize = 127;
+
+// Implements a call to the IPFS that returns a streaming body response.
+// Implementing this in a macro is necessary because the Rust compiler
+// can't reason about the lifetime of the request instance properly. It
+// thinks that the request needs to live as long as the returned stream,
+// but in reality, the request instance is only used to build the Hyper
+// or Actix request.
+//
+macro_rules! impl_stream_api_response {
+ (($self:ident, $req:expr, $form:expr) => $call:ident) => {
+ impl_stream_api_response! {
+ ($self, $req, $form) |req| => { $self.$call(req) }
+ }
+ };
+ (($self:ident, $req:expr, $form:expr) |$var:ident| => $impl:block) => {
+ match $self.build_base_request(&$req, $form) {
+ Ok($var) => $impl,
+ Err(e) => Box::new(future::err(e).into_stream()),
+ }
+ };
+}
+
+#[async_trait(?Send)]
+pub trait IpfsApi: Backend {
+ /// Add file to Ipfs.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use ipfs_api::{IpfsApi, IpfsClient};
+ /// use std::io::Cursor;
+ ///
+ /// let client = IpfsClient::default();
+ /// let data = Cursor::new("Hello World!");
+ /// let res = client.add(data);
+ /// ```
+ ///
+ async fn add<R>(&self, data: R) -> Result<response::AddResponse, Self::Error>
+ 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::{IpfsApi, IpfsClient};
+ /// use std::io::Cursor;
+ ///
+ /// # fn main() {
+ /// let client = IpfsClient::default();
+ /// let data = Cursor::new("Hello World!");
+ /// #[cfg(feature = "with-builder")]
+ /// let add = ipfs_api::request::Add::builder()
+ /// .chunker("rabin-512-1024-2048")
+ /// .build();
+ /// #[cfg(not(feature = "with-builder"))]
+ /// let add = ipfs_api::request::Add {
+ /// chunker: Some("rabin-512-1024-2048"),
+ /// ..Default::default()
+ /// };
+ /// let req = client.add_with_options(data, add);
+ /// # }
+ /// ```
+ ///
+ async fn add_with_options<R>(
+ &self,
+ data: R,
+ add: request::Add<'_>,
+ ) -> Result<response::AddResponse, Self::Error>
+ where
+ R: 'static + Read + Send + Sync,
+ {
+ let mut form = multipart::Form::default();
+
+ form.add_reader("path", data);
+
+ self.request(add, Some(form)).await
+ }
+
+ /// Add a path to Ipfs. Can be a file or directory.
+ /// A hard limit of 128 open file descriptors is set such
+ /// that any small additional files are stored in-memory.
+ ///
+ /// # Examples
+ ///
+ /// ```no_run
+ /// use ipfs_api::{IpfsApi, IpfsClient};
+ ///
+ /// let client = IpfsClient::default();
+ /// let path = "./src";
+ /// let res = client.add_path(path);
+ /// ```
+ ///
+ async fn add_path<P>(&self, path: P) -> Result<Vec<response::AddResponse>, Self::Error>
+ where
+ P: AsRef<Path>,
+ {
+ let prefix = path.as_ref().parent();
+ let mut paths_to_add: Vec<(PathBuf, u64)> = vec![];
+
+ for path in walkdir::WalkDir::new(path.as_ref()) {
+ match path {
+ Ok(entry) if entry.file_type().is_file() => {