diff options
Diffstat (limited to 'ipfs-api-prelude')
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("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 |