diff options
author | Ferris Tseng <ferristseng@fastmail.fm> | 2019-12-23 00:20:10 -0500 |
---|---|---|
committer | Ferris Tseng <ferristseng@fastmail.fm> | 2019-12-23 00:20:10 -0500 |
commit | ef4808417a61add84782a2bdc6974d9bd0bfcb00 (patch) | |
tree | b383076781f78dc656882177a107b7bd998ab0f5 /ipfs-api/examples | |
parent | 534b95b6acba505cdd963baaefa61c49bc6e4d0b (diff) |
implementing examples
Diffstat (limited to 'ipfs-api/examples')
-rw-r--r-- | ipfs-api/examples/add_file.rs | 21 | ||||
-rw-r--r-- | ipfs-api/examples/add_tar.rs | 65 | ||||
-rw-r--r-- | ipfs-api/examples/bootstrap_default.rs | 80 |
3 files changed, 86 insertions, 80 deletions
diff --git a/ipfs-api/examples/add_file.rs b/ipfs-api/examples/add_file.rs index 2361986..798a5f2 100644 --- a/ipfs-api/examples/add_file.rs +++ b/ipfs-api/examples/add_file.rs @@ -6,26 +6,21 @@ // copied, modified, or distributed except according to those terms. // -use futures::Future; use ipfs_api::IpfsClient; use std::fs::File; -use tokio::runtime::current_thread::Runtime; // Creates an Ipfs client, and adds this source file to Ipfs. // -fn main() { - println!("note: this must be run in the root of the project repository"); - println!("connecting to localhost:5001..."); +#[tokio::main] +async fn main() { + eprintln!("note: this must be run in the root of the project repository"); + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); let file = File::open(file!()).expect("could not read source file"); - let req = client - .add(file) - .map(|add| println!("added file: {:?}", add)) - .map_err(|e| eprintln!("{}", e)); - Runtime::new() - .expect("tokio runtime") - .block_on(req) - .expect("successful response"); + match client.add(file).await { + Ok(file) => eprintln!("added file: {:?}", file), + Err(e) => eprintln!("error adding file: {}", e), + } } diff --git a/ipfs-api/examples/add_tar.rs b/ipfs-api/examples/add_tar.rs index a5da698..c309456 100644 --- a/ipfs-api/examples/add_tar.rs +++ b/ipfs-api/examples/add_tar.rs @@ -6,24 +6,24 @@ // copied, modified, or distributed except according to those terms. // -use futures::{Future, Stream}; +use bytes::BytesMut; +use futures::TryStreamExt; use ipfs_api::IpfsClient; use std::io::Cursor; use tar::Builder; -use tokio::runtime::current_thread::Runtime; // Creates an Ipfs client, and adds this source file to Ipfs. // -fn main() { - println!("note: this must be run in the root of the project repository"); - println!("connecting to localhost:5001..."); +#[tokio::main] +async fn main() { + eprintln!("note: this must be run in the root of the project repository"); + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); - let mut buf = Vec::new(); - // Create a in-memory tar file with this source file as its contents. // + let mut buf = Vec::new(); { let mut builder = Builder::new(&mut buf); @@ -32,24 +32,37 @@ fn main() { .expect("failed to create tar file"); builder.finish().expect("failed to create tar file"); } - let cursor = Cursor::new(buf); - let req = client - .tar_add(cursor) - .and_then(move |add| { - println!("added tar file: {:?}", add); - println!(); - - client.tar_cat(&add.hash[..]).concat2() - }) - .map(|cat| { - println!("{}", String::from_utf8_lossy(&cat[..])); - println!(); - }) - .map_err(|e| eprintln!("{}", e)); - - Runtime::new() - .expect("tokio runtime") - .block_on(req) - .expect("successful response"); + + // Write in-memory tar file to IPFS. + // + let file = match client.tar_add(cursor).await { + Ok(file) => { + eprintln!("added tar file: {:?}", file); + eprintln!(); + + file + } + Err(e) => { + eprintln!("error writing tar file: {}", e); + + return; + } + }; + + // Read tar file from IPFS. + // + match client + .tar_cat(&file.hash[..]) + .map_ok(|chunk| chunk.to_vec()) + .try_concat() + .await + { + Ok(tar) => { + println!("{}", String::from_utf8_lossy(&tar[..])); + } + Err(e) => { + eprintln!("error reading tar file: {}", e); + } + } } diff --git a/ipfs-api/examples/bootstrap_default.rs b/ipfs-api/examples/bootstrap_default.rs index c2e6871..5a60496 100644 --- a/ipfs-api/examples/bootstrap_default.rs +++ b/ipfs-api/examples/bootstrap_default.rs @@ -6,56 +6,54 @@ // copied, modified, or distributed except according to those terms. // -use futures::Future; use ipfs_api::IpfsClient; -use tokio::runtime::current_thread::Runtime; // Lists clients in bootstrap list, then adds the default list, then removes // them, and readds them. // -fn main() { - println!("connecting to localhost:5001..."); +#[tokio::main] +async fn main() { + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); - let bootstrap = client.bootstrap_list().map(|bootstrap| { - println!("current bootstrap peers:"); - for peer in bootstrap.peers { - println!(" {}", peer); + match client.bootstrap_list().await { + Ok(bootstrap) => { + eprintln!("current bootstrap peers:"); + for peer in bootstrap.peers { + eprintln!(" {}", peer); + } + eprintln!(); } - }); - - let drop = client.bootstrap_rm_all().map(|drop| { - println!("dropped:"); - for peer in drop.peers { - println!(" {}", peer); + Err(e) => { + eprintln!("error getting list of bootstrap peers: {}", e); + return; } - }); - - let add = client.bootstrap_add_default().map(|add| { - println!("added:"); - for peer in add.peers { - println!(" {}", peer); + } + + match client.bootstrap_rm_all().await { + Ok(drop) => { + eprintln!("dropped:"); + for peer in drop.peers { + eprintln!(" {}", peer); + } + eprintln!(); } - }); - - let fut = bootstrap - .and_then(|_| { - println!(); - println!("dropping all bootstrap peers..."); - - drop - }) - .and_then(|_| { - println!(); - println!("adding default peers..."); - - add - }) - .map_err(|e| eprintln!("{}", e)); - - Runtime::new() - .expect("tokio runtime") - .block_on(fut) - .expect("successful response"); + Err(e) => { + eprintln!("error dropping bootstrap peers: {}", e); + } + } + + match client.bootstrap_add_default().await { + Ok(add) => { + eprintln!("added:"); + for peer in add.peers { + eprintln!(" {}", peer); + } + eprintln!(); + } + Err(e) => { + eprintln!("error adding default peers: {}", e); + } + } } |