diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2021-02-22 20:41:44 -0500 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2021-02-22 20:41:44 -0500 |
commit | 7dddbde3a1abcb96b75aa4636a07498193f7c551 (patch) | |
tree | 3037fa51ae3ea483629414b2e5d1c4eec19f5e62 /ipfs-api-prelude/src | |
parent | c429ee51d8c29678c216f44a48ae596f99decc4b (diff) |
migrate common code to ipfs-api-prelude
Diffstat (limited to 'ipfs-api-prelude/src')
119 files changed, 7078 insertions, 0 deletions
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_api; + /// # + /// use ipfs_api::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); + /// # } + |