From ef4808417a61add84782a2bdc6974d9bd0bfcb00 Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 23 Dec 2019 00:20:10 -0500 Subject: implementing examples --- Cargo.lock | 12 +++++ ipfs-api/Cargo.toml | 1 + ipfs-api/examples/add_file.rs | 21 ++++----- ipfs-api/examples/add_tar.rs | 65 ++++++++++++++++----------- ipfs-api/examples/bootstrap_default.rs | 80 +++++++++++++++++----------------- ipfs-api/src/client.rs | 14 +++--- 6 files changed, 106 insertions(+), 87 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 13e90e1..c98dc49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1728,12 +1728,23 @@ dependencies = [ "memchr 2.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "mio 0.6.21 (registry+https://github.com/rust-lang/crates.io-index)", "mio-uds 0.6.7 (registry+https://github.com/rust-lang/crates.io-index)", + "num_cpus 1.11.1 (registry+https://github.com/rust-lang/crates.io-index)", "pin-project-lite 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "signal-hook-registry 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)", "slab 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "tokio-macros 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "tokio-macros" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "tokio-tls" version = "0.3.0" @@ -2145,6 +2156,7 @@ dependencies = [ "checksum time 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "db8dcfca086c1143c9270ac42a2bbd8a7ee477b78ac8e45b19abfb0cbede4b6f" "checksum tiny-keccak 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "1d8a021c69bb74a44ccedb824a046447e2c84a01df9e5c20779750acb38e11b2" "checksum tokio 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0e1bef565a52394086ecac0a6fa3b8ace4cb3a138ee1d96bd2b93283b56824e3" +"checksum tokio-macros 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7de6c21a09bab0ce34614bb1071403ad9996db62715eb61e63be5d82f91342bc" "checksum tokio-tls 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7bde02a3a5291395f59b06ec6945a3077602fac2b07eeeaf0dee2122f3619828" "checksum tokio-util 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "571da51182ec208780505a32528fc5512a8fe1443ab960b3f2f3ef093cd16930" "checksum tower-service 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e987b6bf443f4b5b3b6f38704195592cca41c5bb7aedd3c3693c7081f8289860" diff --git a/ipfs-api/Cargo.toml b/ipfs-api/Cargo.toml index cb9ace2..f509440 100644 --- a/ipfs-api/Cargo.toml +++ b/ipfs-api/Cargo.toml @@ -47,3 +47,4 @@ awc = "1.0" hyper = "0.13" hyper-tls = "0.4" tar = "0.4" +tokio = { version = "0.2", features = ["rt-threaded", "macros"] } 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); + } + } } diff --git a/ipfs-api/src/client.rs b/ipfs-api/src/client.rs index afe39a4..f089da6 100644 --- a/ipfs-api/src/client.rs +++ b/ipfs-api/src/client.rs @@ -2255,6 +2255,7 @@ impl IpfsClient { pub fn swarm_peers(&self) -> AsyncResponse { self.request(&request::SwarmPeers, None) } + */ /// Add a tar file to Ipfs. /// @@ -2275,15 +2276,15 @@ impl IpfsClient { /// ``` /// #[inline] - pub fn tar_add(&self, data: R) -> AsyncResponse + pub async fn tar_add(&self, data: R) -> Result where - R: 'static + Read + Send, + R: 'static + Read + Send + Sync, { let mut form = multipart::Form::default(); form.add_reader("file", data); - self.request(&request::TarAdd, Some(form)) + self.request(request::TarAdd, Some(form)).await } /// Export a tar file from Ipfs. @@ -2301,7 +2302,7 @@ impl IpfsClient { /// #[inline] pub fn tar_cat(&self, path: &str) -> AsyncStreamResponse { - self.request_stream_bytes(&request::TarCat { path }, None) + self.request_stream_bytes(request::TarCat { path }, None) } /// Returns information about the Ipfs server version. @@ -2318,8 +2319,7 @@ impl IpfsClient { /// ``` /// #[inline] - pub fn version(&self) -> AsyncResponse { - self.request(&request::Version, None) + pub async fn version(&self) -> Result { + self.request(request::Version, None).await } - */ } -- cgit v1.2.3