ipfs_api/
lib.rs

1// Copyright 2017 rust-ipfs-api Developers
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7//
8
9//! Rust library for connecting to the IPFS HTTP API using Hyper/Actix.
10//!
11//! ## Usage
12//!
13//! ### Using Hyper
14//!
15//! To use the Hyper backend, declare:
16//!
17//! ```toml
18//! [dependencies]
19//! ipfs-api-backend-hyper = "0.6"
20//! ```
21//!
22//! You can specify either `with-hyper-rustls` or `with-hyper-tls` (mutually exclusive) feature for TLS support.
23//!
24//! ### Using Actix
25//!
26//! To use the Actix backend, declare:
27//!
28//! ```toml
29//! [dependencies]
30//! ipfs-api-backend-actix = "0.7"
31//! ```
32//!
33//! ### Builder Pattern
34//!
35//! With either the Hyper or Actix backend, you can specify the `with-builder` feature to enable a builder pattern to use when building requests.
36//!
37//! ## Usage (DEPRECATED)
38//!
39//! ```toml
40//! [dependencies]
41//! ipfs-api = "0.17.0"
42//! ```
43//!
44//! ### Feature Flags (DEPRECATED)
45//!
46//! You can use `actix-web` as a backend instead of `hyper`.
47//!
48//! ```toml
49//! [dependencies]
50//! ipfs-api = { version = "0.17.0", features = ["with-actix"], default-features = false }
51//! ```
52//!
53//! You also have the option of using [`rustls`](https://crates.io/crates/rustls)
54//! instead of native tls:
55//!
56//! ```toml
57//! [dependencies]
58//! ipfs-api = { version = "0.17.0", features = ["with-hyper-rustls"], default-features = false }
59//! ```
60//!
61//! To enable the builder pattern (default) use the `with-builder` feature:
62//!
63//! ```toml
64//! [dependencies]
65//! ipfs-api = { version = "0.17.0", features = ["with-hyper-rustls", "with-builder"], default-features = false }
66//! ```
67//!
68//! ## Examples
69//!
70//! ### Writing a file to IPFS
71//!
72//! #### With Hyper
73//!
74//! ```no_run
75//! use ipfs_api::{IpfsApi, IpfsClient};
76//! use std::io::Cursor;
77//!
78//! #[tokio::main]
79//! async fn main() {
80//!     let client = IpfsClient::default();
81//!     let data = Cursor::new("Hello World!");
82//!
83//!     match client.add(data).await {
84//!         Ok(res) => println!("{}", res.hash),
85//!         Err(e) => eprintln!("error adding file: {}", e)
86//!     }
87//! }
88//! ```
89//!
90//! #### With Actix
91//!
92//! ```no_run
93//! use ipfs_api::{IpfsApi, IpfsClient};
94//! use std::io::Cursor;
95//!
96//! #[actix_rt::main]
97//! async fn main() {
98//!     let client = IpfsClient::default();
99//!     let data = Cursor::new("Hello World!");
100//!
101//!     match client.add(data).await {
102//!         Ok(res) => println!("{}", res.hash),
103//!         Err(e) => eprintln!("error adding file: {}", e)
104//!     }
105//! }
106//! ```
107//!
108//! ### Reading a file from IPFS
109//!
110//! #### With Hyper
111//!
112//! ```no_run
113//! use futures::TryStreamExt;
114//! use ipfs_api::{IpfsApi, IpfsClient};
115//! use std::io::{self, Write};
116//!
117//! #[tokio::main]
118//! async fn main() {
119//!     let client = IpfsClient::default();
120//!
121//!     match client
122//!         .get("/test/file.json")
123//!         .map_ok(|chunk| chunk.to_vec())
124//!         .try_concat()
125//!         .await
126//!     {
127//!         Ok(res) => {
128//!             let out = io::stdout();
129//!             let mut out = out.lock();
130//!
131//!             out.write_all(&res).unwrap();
132//!         }
133//!         Err(e) => eprintln!("error getting file: {}", e)
134//!     }
135//! }
136//! ```
137//!
138//! #### With Actix
139//!
140//! ```no_run
141//! use futures::TryStreamExt;
142//! use ipfs_api::{IpfsApi, IpfsClient};
143//! use std::io::{self, Write};
144//!
145//! #[actix_rt::main]
146//! async fn main() {
147//!     let client = IpfsClient::default();
148//!
149//!     match client
150//!         .get("/test/file.json")
151//!         .map_ok(|chunk| chunk.to_vec())
152//!         .try_concat()
153//!         .await
154//!     {
155//!         Ok(res) => {
156//!             let out = io::stdout();
157//!             let mut out = out.lock();
158//!
159//!             out.write_all(&res).unwrap();
160//!         }
161//!         Err(e) => eprintln!("error getting file: {}", e)
162//!     }
163//! }
164//! ```
165//!
166//! ### Additional Examples
167//!
168//! There are also a bunch of examples included in the project, which
169//! I used for testing
170//!
171//! For a list of examples, run:
172//!
173//! ```sh
174//! $ cargo run --example
175//! ```
176//!
177//! You can run any of the examples with cargo:
178//!
179//! ```sh
180//! $ cargo run --example add_file
181//! ```
182//!
183
184#[cfg(feature = "with-hyper")]
185pub use ipfs_api_backend_hyper::*;
186
187#[cfg(feature = "with-actix")]
188pub use ipfs_api_backend_actix::*;
189
190#[cfg(not(any(feature = "with-actix", feature = "with-hyper")))]
191compile_error!("Pick exactly one of these features: with-hyper, with-actix");