summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerris Tseng <ferristseng@fastmail.fm>2021-02-22 20:41:44 -0500
committerFerris Tseng <ferristseng@fastmail.fm>2021-02-22 20:41:44 -0500
commit7dddbde3a1abcb96b75aa4636a07498193f7c551 (patch)
tree3037fa51ae3ea483629414b2e5d1c4eec19f5e62
parentc429ee51d8c29678c216f44a48ae596f99decc4b (diff)
migrate common code to ipfs-api-prelude
-rw-r--r--ipfs-api-prelude/Cargo.toml28
-rw-r--r--ipfs-api-prelude/src/api.rs886
-rw-r--r--ipfs-api-prelude/src/backend.rs220
-rw-r--r--ipfs-api-prelude/src/error.rs28
-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.rs23
-rw-r--r--ipfs-api-prelude/src/read.rs227
-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.rs133
-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, 7106 insertions, 0 deletions
diff --git a/ipfs-api-prelude/Cargo.toml b/ipfs-api-prelude/Cargo.toml
new file mode 100644
index 0000000..61590a6
--- /dev/null
+++ b/ipfs-api-prelude/Cargo.toml
@@ -0,0 +1,28 @@
+[package]
+name = "ipfs-api-prelude"
+description = "Shared code for IPFS HTTP API client"
+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"
+
+[dependencies]
+async-trait = "0.1"
+bytes = "1.0"
+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.2"
+tokio-util = { version = "0.6", features = ["codec"] }
+tracing = "0.1"
+walkdir = "2.3"
diff --git a/ipfs-api-prelude/src/api.rs b/ipfs-api-prelude/src/api.rs
new file mode 100644
index 0000000..237165e
--- /dev/null
+++ b/ipfs-api-prelude/src/api.rs
@@ -0,0 +1,886 @@
+// 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::{request, response, Backend};
+use async_trait::async_trait;
+use bytes::Bytes;
+use futures::{future, FutureExt, Stream, StreamExt, 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) => Box::new($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::IpfsClient;
+ /// use std::io::Cursor;
+ ///
+ /// let client = IpfsClient::default();
+ /// let data = Cursor::new("H