summaryrefslogtreecommitdiffstats
path: root/ipfs-api/src
diff options
context:
space:
mode:
Diffstat (limited to 'ipfs-api/src')
-rw-r--r--ipfs-api/src/client.rs20
-rw-r--r--ipfs-api/src/lib.rs287
2 files changed, 164 insertions, 143 deletions
diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs
index 5e1e9f5..69411ee 100644
--- a/ipfs-api/src/client.rs
+++ b/ipfs-api/src/client.rs
@@ -22,14 +22,18 @@ use http::StatusCode;
use hyper::client::{Client, HttpConnector};
#[cfg(feature = "hyper")]
use hyper_multipart::client::multipart;
+use multiaddr::{AddrComponent, ToMultiaddr};
use read::{JsonLineDecoder, LineDecoder, StreamReader};
use request::{self, ApiRequest};
use response::{self, Error};
use serde::{Deserialize, Serialize};
use serde_json;
-use std::io::Read;
-use std::net::SocketAddr;
-use std::path::{Path, PathBuf};
+use std::{
+ fs,
+ io::Read,
+ net::{IpAddr, SocketAddr},
+ path::{Path, PathBuf},
+};
use tokio_codec::{Decoder, FramedRead};
/// A response returned by the HTTP client.
@@ -70,16 +74,8 @@ impl Default for IpfsClient {
/// If not found, tries to connect to `localhost:5001`.
///
fn default() -> IpfsClient {
- use multiaddr::{AddrComponent, ToMultiaddr};
- use std::fs;
- use std::net::IpAddr;
-
dirs::home_dir()
- .map(|mut home_dir| {
- home_dir.push(".ipfs");
- home_dir.push("api");
- home_dir
- })
+ .map(|home_dir| home_dir.join(".ipfs").join("api"))
.and_then(|multiaddr_path| fs::read_to_string(&multiaddr_path).ok())
.and_then(|multiaddr_str| multiaddr_str.to_multiaddr().ok())
.and_then(|multiaddr| {
diff --git a/ipfs-api/src/lib.rs b/ipfs-api/src/lib.rs
index 64ea2f4..22568d8 100644
--- a/ipfs-api/src/lib.rs
+++ b/ipfs-api/src/lib.rs
@@ -14,145 +14,170 @@
//!
//! ```toml
//! [dependencies]
-//! ipfs-api = "0.5.0-alpha2"
+//! ipfs-api = "0.5.1"
+//! ```
+//!
+//! You can use `actix-web` as a backend instead of `hyper`.
+//!
+//! ```toml
+//! [dependencies]
+//! ipfs-api = { version = "0.5.1", features = ["actix"], default-features = false }
+//! ```
+//!
+//! ## Examples
+//!
+//! ### Writing a file to IPFS
+//!
+//! #### With Hyper
+//!
+//! ```no_run
+//! # extern crate hyper;
+//! # extern crate ipfs_api;
+//! #
+//! use hyper::rt::Future;
+//! use ipfs_api::IpfsClient;
+//! use std::io::Cursor;
+//!
+//! # fn main() {
+//! let client = IpfsClient::default();
+//! let data = Cursor::new("Hello World!");
+//!
+//! let req = client
+//! .add(data)
+//! .map(|res| {
+//! println!("{}", res.hash);
+//! })
+//! .map_err(|e| eprintln!("{}", e));
+//!
+//! hyper::rt::run(req);
+//! # }
+//! ```
+//!
+//! #### With Actix
+//!
+//! ```no_run
+//! # extern crate actix_web;
+//! # extern crate futures;
+//! # extern crate ipfs_api;
+//! #
+//! use futures::future::Future;
+//! use ipfs_api::IpfsClient;
+//! use std::io::Cursor;
+//!
+//! # fn main() {
+//! let client = IpfsClient::default();
+//! let data = Cursor::new("Hello World!");
+//!
+//! let req = client
+//! .add(data)
+//! .map(|res| {
+//! println!("{}", res.hash);
+//! })
+//! .map_err(|e| eprintln!("{}", e));
+//!
+//! actix_web::actix::run(|| {
+//! req.then(|_| {
+//! actix_web::actix::System::current().stop();
+//! Ok(())
+//! })
+//! });
+//! # }
+//! ```
+//!
+//! ### Reading a file from IPFS
+//!
+//! #### With Hyper
+//!
+//! ```no_run
+//! # extern crate futures;
+//! # extern crate hyper;
+//! # extern crate ipfs_api;
+//! #
+//! use futures::{Future, Stream};
+//! use ipfs_api::IpfsClient;
+//! use std::io::{self, Write};
+//!
+//! # fn main() {
+//! let client = IpfsClient::default();
+//!
+//! let req = client
+//! .get("/test/file.json")
+//! .concat2()
+//! .map(|res| {
+//! let out = io::stdout();
+//! let mut out = out.lock();
+//!
+//! out.write_all(&res).unwrap();
+//! })
+//! .map_err(|e| eprintln!("{}", e));
+//!
+//! hyper::rt::run(req);
+//! # }
+//! ```
+//!
+//! #### With Actix
+//!
+//! ```no_run
+//! # extern crate futures;
+//! # extern crate actix_web;
+//! # extern crate ipfs_api;
+//! #
+//! use futures::{Future, Stream};
+//! use ipfs_api::IpfsClient;
+//! use std::io::{self, Write};
+//!
+//! # fn main() {
+//! let client = IpfsClient::default();
+//!
+//! let req = client
+//! .get("/test/file.json")
+//! .concat2()
+//! .map(|res| {
+//! let out = io::stdout();
+//! let mut out = out.lock();
+//!
+//! out.write_all(&res).unwrap();
+//! })
+//! .map_err(|e| eprintln!("{}", e));
+//!
+//! actix_web::actix::run(|| {
+//! req.then(|_| {
+//! actix_web::actix::System::current().stop();
+//! Ok(())
+//! })
+//! });
+//! # }
+//! ```
+//!
+//! ### Additional Examples
+//!
+//! There are also a bunch of examples included in the project, which
+//! I used for testing
+//!
+//! For a list of examples, run:
+//!
+//! ```sh
+//! $ cargo run --example
+//! ```
+//!
+//! You can run any of the examples with cargo:
+//!
+//! ```sh
+//! $ cargo run -p ipfs-api --example add_file
+//! ```
+//!
+//! To run an example with the `actix-web` backend, use:
+//!
+//! ```sh
+//! $ cargo run -p ipfs-api --features actix --no-default-features --example add_file
//! ```
//!
#[cfg(feature = "actix")]
extern crate actix_multipart_rfc7578 as actix_multipart;
-
-/// ## Examples
-///
-/// Write a file to IPFS:
-///
-/// ```no_run
-/// # extern crate actix_web;
-/// # extern crate futures;
-/// # extern crate ipfs_api;
-/// #
-/// use futures::future::Future;
-/// use ipfs_api::IpfsClient;
-/// use std::io::Cursor;
-///
-/// # fn main() {
-/// let client = IpfsClient::default();
-/// let data = Cursor::new("Hello World!");
-///
-/// let req = client
-/// .add(data)
-/// .map(|res| {
-/// println!("{}", res.hash);
-/// })
-/// .map_err(|e| eprintln!("{}", e));
-///
-/// tokio::runtime::current_thread::run(req);
-/// # }
-/// ```
-///
-/// Read a file from IPFS:
-///
-/// ```no_run
-/// # extern crate futures;
-/// # extern crate actix_web;
-/// # extern crate ipfs_api;
-/// #
-/// use futures::{Future, Stream};
-/// use ipfs_api::IpfsClient;
-/// use std::io::{self, Write};
-///
-/// # fn main() {
-/// let client = IpfsClient::default();
-///
-/// let req = client
-/// .get("/test/file.json")
-/// .concat2()
-/// .map(|res| {
-/// let out = io::stdout();
-/// let mut out = out.lock();
-///
-/// out.write_all(&res).unwrap();
-/// })
-/// .map_err(|e| eprintln!("{}", e));
-///
-/// tokio::runtime::current_thread::run(req);
-/// # }
-/// ```
-///
-/// There are also a bunch of examples included in the project, which
-/// I used for testing
-///
-/// You can run any of the examples with cargo:
-///
-/// ```sh
-/// $ cargo run -p ipfs-api --example add_file
-/// ```
#[cfg(feature = "actix")]
extern crate actix_web;
-/// ## Examples
-///
-/// Write a file to IPFS:
-///
-/// ```no_run
-/// # extern crate hyper;
-/// # extern crate ipfs_api;
-/// #
-/// use hyper::rt::Future;
-/// use ipfs_api::IpfsClient;
-/// use std::io::Cursor;
-///
-/// # fn main() {
-/// let client = IpfsClient::default();
-/// let data = Cursor::new("Hello World!");
-///
-/// let req = client
-/// .add(data)
-/// .map(|res| {
-/// println!("{}", res.hash);
-/// })
-/// .map_err(|e| eprintln!("{}", e));
-///
-/// hyper::rt::run(req);
-/// # }
-/// ```
-///
-/// Read a file from IPFS:
-///
-/// ```no_run
-/// # extern crate futures;
-/// # extern crate hyper;
-/// # extern crate ipfs_api;
-/// #
-/// use futures::{Future, Stream};
-/// use ipfs_api::IpfsClient;
-/// use std::io::{self, Write};
-///
-/// # fn main() {
-/// let client = IpfsClient::default();
-///
-/// let req = client
-/// .get("/test/file.json")
-/// .concat2()
-/// .map(|res| {
-/// let out = io::stdout();
-/// let mut out = out.lock();
-///
-/// out.write_all(&res).unwrap();
-/// })
-/// .map_err(|e| eprintln!("{}", e));
-///
-/// hyper::rt::run(req);
-/// # }
-/// ```
-///
-/// There are also a bunch of examples included in the project, which
-/// I used for testing
-///
-/// You can run any of the examples with cargo:
-///
-/// ```sh
-/// $ cargo run -p ipfs-api --example add_file
-/// ```
#[cfg(feature = "hyper")]
extern crate hyper;
#[cfg(feature = "hyper")]