summaryrefslogtreecommitdiffstats
path: root/README.md
blob: e451f2cb2b00ba29c686752f4d2e94d9eb181dac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# ipfs-api

[![Travis](https://img.shields.io/travis/ferristseng/rust-ipfs-api.svg)](https://travis-ci.org/ferristseng/rust-ipfs-api)
[![Crates.io](https://img.shields.io/crates/v/ipfs-api.svg)](https://crates.io/crates/ipfs-api)
[![Docs.rs](https://docs.rs/ipfs-api/badge.svg)](https://docs.rs/ipfs-api/)

Rust library for connecting to the IPFS HTTP API using tokio.

### Usage

```toml
[dependencies]
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

```rust
#
use hyper::rt::Future;
use ipfs_api::IpfsClient;
use std::io::Cursor;

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

```rust
#
use futures::future::Future;
use ipfs_api::IpfsClient;
use std::io::Cursor;

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

```rust
#
use futures::{Future, Stream};
use ipfs_api::IpfsClient;
use std::io::{self, Write};

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

```rust
#
use futures::{Future, Stream};
use ipfs_api::IpfsClient;
use std::io::{self, Write};

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
```


## License

Licensed under either of

 * Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
 * MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.