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 | |
parent | 534b95b6acba505cdd963baaefa61c49bc6e4d0b (diff) |
implementing examples
Diffstat (limited to 'ipfs-api')
-rw-r--r-- | ipfs-api/Cargo.toml | 1 | ||||
-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 | ||||
-rw-r--r-- | ipfs-api/src/client.rs | 14 |
5 files changed, 94 insertions, 87 deletions
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<response::SwarmPeersResponse> { self.request(&request::SwarmPeers, None) } + */ /// Add a tar file to Ipfs. /// @@ -2275,15 +2276,15 @@ impl IpfsClient { /// ``` /// #[inline] - pub fn tar_add<R>(&self, data: R) -> AsyncResponse<response::TarAddResponse> + pub async fn tar_add<R>(&self, data: R) -> Result<response::TarAddResponse, Error> 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<Bytes> { - 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<response::VersionResponse> { - self.request(&request::Version, None) + pub async fn version(&self) -> Result<response::VersionResponse, Error> { + self.request(request::Version, None).await } - */ } |