From d26081bfc6f5d6c0f17be2f49fcd5f555df9250d Mon Sep 17 00:00:00 2001 From: Ferris Tseng Date: Mon, 23 Dec 2019 11:54:07 -0500 Subject: update examples --- ipfs-api/examples/dns.rs | 43 +++++++++--------- ipfs-api/examples/get_commands.rs | 29 +++++------- ipfs-api/examples/get_stats.rs | 92 +++++++++++++++++++-------------------- ipfs-api/examples/get_swarm.rs | 57 +++++++++++------------- 4 files changed, 105 insertions(+), 116 deletions(-) diff --git a/ipfs-api/examples/dns.rs b/ipfs-api/examples/dns.rs index e91ccbc..128a35a 100644 --- a/ipfs-api/examples/dns.rs +++ b/ipfs-api/examples/dns.rs @@ -6,37 +6,38 @@ // copied, modified, or distributed except according to those terms. // -use futures::Future; use ipfs_api::IpfsClient; -use tokio::runtime::current_thread::Runtime; // Creates an Ipfs client, resolves ipfs.io, and lists the contents of it. // -fn main() { - println!("connecting to localhost:5001..."); +#[tokio::main] +async fn main() { + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); - let req = client - .dns("ipfs.io", false) - .and_then(move |dns| { - println!("dns resolves to ({})", &dns.path); - println!(); + let dns = match client.dns("ipfs.io", false).await { + Ok(dns) => { + eprintln!("dns resolves to ({})", &dns.path); + eprintln!(); - client.file_ls(&dns.path[..]) - }) - .map(|contents| { - println!("found contents:"); + dns + } + Err(e) => { + eprintln!("error resolving dns: {}", e); + return; + } + }; + + match client.file_ls(&dns.path[..]).await { + Ok(contents) => { + eprintln!("found contents:"); for directory in contents.objects.values() { for file in directory.links.iter() { - println!("[{}] ({} bytes)", file.name, file.size); + eprintln!("[{}] ({} bytes)", file.name, file.size); } } - }) - .map_err(|e| eprintln!("{}", e)); - - Runtime::new() - .expect("tokio runtime") - .block_on(req) - .expect("successful response"); + } + Err(e) => eprintln!("error listing path: {}", e), + } } diff --git a/ipfs-api/examples/get_commands.rs b/ipfs-api/examples/get_commands.rs index 9b5bcec..4557b70 100644 --- a/ipfs-api/examples/get_commands.rs +++ b/ipfs-api/examples/get_commands.rs @@ -6,25 +6,23 @@ // copied, modified, or distributed except according to those terms. // -use futures::Future; use ipfs_api::{response, IpfsClient}; -use tokio::runtime::current_thread::Runtime; fn print_recursive(indent: usize, cmd: &response::CommandsResponse) { let cmd_indent = " ".repeat(indent * 4); let opt_indent = " ".repeat((indent + 1) * 4); - println!("{}[{}]", cmd_indent, cmd.name); + eprintln!("{}[{}]", cmd_indent, cmd.name); if cmd.options.len() > 0 { - println!("{}* options:", cmd_indent); + eprintln!("{}* options:", cmd_indent); for options in cmd.options.iter() { - println!("{}{}", opt_indent, &options.names[..].join(", ")); + eprintln!("{}{}", opt_indent, &options.names[..].join(", ")); } } if cmd.subcommands.len() > 0 { - println!("{}- subcommands:", cmd_indent); + eprintln!("{}- subcommands:", cmd_indent); for subcommand in cmd.subcommands.iter() { print_recursive(indent + 1, subcommand); } @@ -34,17 +32,14 @@ fn print_recursive(indent: usize, cmd: &response::CommandsResponse) { // Creates an Ipfs client, and gets a list of available commands from the // Ipfs server. // -fn main() { - println!("connecting to localhost:5001..."); +#[tokio::main] +async fn main() { + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); - let req = client - .commands() - .map(|commands| print_recursive(0, &commands)) - .map_err(|e| eprintln!("{}", e)); - - Runtime::new() - .expect("tokio runtime") - .block_on(req) - .expect("successful response"); + + match client.commands().await { + Ok(commands) => print_recursive(0, &commands), + Err(e) => eprintln!("error getting commands: {}", e), + } } diff --git a/ipfs-api/examples/get_stats.rs b/ipfs-api/examples/get_stats.rs index 0395f32..36100a5 100644 --- a/ipfs-api/examples/get_stats.rs +++ b/ipfs-api/examples/get_stats.rs @@ -6,58 +6,56 @@ // copied, modified, or distributed except according to those terms. // -use futures::Future; use ipfs_api::IpfsClient; -use tokio::runtime::current_thread::Runtime; // Creates an Ipfs client, and gets some stats about the Ipfs server. // -fn main() { - println!("connecting to localhost:5001..."); +#[tokio::main] +async fn main() { + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); - let bitswap_stats = client.stats_bitswap().map(|bitswap_stats| { - println!("bitswap stats:"); - println!(" blocks recv: {}", bitswap_stats.blocks_received); - println!(" data recv: {}", bitswap_stats.data_received); - println!(" blocks sent: {}", bitswap_stats.blocks_sent); - println!(" data sent: {}", bitswap_stats.data_sent); - println!( - " peers: {}", - bitswap_stats.peers.join("\n ") - ); - println!( - " wantlist: {}", - bitswap_stats.wantlist.join("\n ") - ); - println!(); - }); - - let bw_stats = client.stats_bw().map(|bw_stats| { - println!("bandwidth stats:"); - println!(" total in: {}", bw_stats.total_in); - println!(" total out: {}", bw_stats.total_out); - println!(" rate in: {}", bw_stats.rate_in); - println!(" rate out: {}", bw_stats.rate_out); - println!(); - }); - - let repo_stats = client.stats_repo().map(|repo_stats| { - println!("repo stats:"); - println!(" num objs: {}", repo_stats.num_objects); - println!(" repo size: {}", repo_stats.repo_size); - println!(" repo path: {}", repo_stats.repo_path); - println!(" version : {}", repo_stats.version); - }); - - let fut = bitswap_stats - .and_then(|_| bw_stats) - .and_then(|_| repo_stats) - .map_err(|e| eprintln!("{}", e)); - - Runtime::new() - .expect("tokio runtime") - .block_on(fut) - .expect("successful response"); + match client.stats_bitswap().await { + Ok(bitswap_stats) => { + eprintln!("bitswap stats:"); + eprintln!(" blocks recv: {}", bitswap_stats.blocks_received); + eprintln!(" data recv: {}", bitswap_stats.data_received); + eprintln!(" blocks sent: {}", bitswap_stats.blocks_sent); + eprintln!(" data sent: {}", bitswap_stats.data_sent); + eprintln!( + " peers: {}", + bitswap_stats.peers.join("\n ") + ); + eprintln!( + " wantlist: {}", + bitswap_stats.wantlist.join("\n ") + ); + eprintln!(); + } + Err(e) => eprintln!("error getting bitswap stats: {}", e), + } + + match client.stats_bw().await { + Ok(bw_stats) => { + eprintln!("bandwidth stats:"); + eprintln!(" total in: {}", bw_stats.total_in); + eprintln!(" total out: {}", bw_stats.total_out); + eprintln!(" rate in: {}", bw_stats.rate_in); + eprintln!(" rate out: {}", bw_stats.rate_out); + eprintln!(); + } + Err(e) => eprintln!("error getting bandwidth stats: {}", e), + } + + match client.stats_repo().await { + Ok(repo_stats) => { + eprintln!("repo stats:"); + eprintln!(" num objs: {}", repo_stats.num_objects); + eprintln!(" repo size: {}", repo_stats.repo_size); + eprintln!(" repo path: {}", repo_stats.repo_path); + eprintln!(" version : {}", repo_stats.version); + } + Err(e) => eprintln!("error getting repo stats: {}", e), + } } diff --git a/ipfs-api/examples/get_swarm.rs b/ipfs-api/examples/get_swarm.rs index cb9bff4..60c8290 100644 --- a/ipfs-api/examples/get_swarm.rs +++ b/ipfs-api/examples/get_swarm.rs @@ -6,46 +6,41 @@ // copied, modified, or distributed except according to those terms. // -use futures::Future; use ipfs_api::IpfsClient; -use tokio::runtime::current_thread::Runtime; // Creates an Ipfs client, and gets information about your local address, and // connected peers. // -fn main() { - println!("connecting to localhost:5001..."); +#[tokio::main] +async fn main() { + eprintln!("connecting to localhost:5001..."); let client = IpfsClient::default(); - let local = client.swarm_addrs_local().map(|local| { - println!(); - println!("your addrs:"); - for addr in local.strings { - println!(" {}", addr); + match client.swarm_addrs_local().await { + Ok(local) => { + eprintln!("your addrs:"); + for addr in local.strings { + eprintln!(" {}", addr); + } + eprintln!(); } - }); + Err(e) => eprintln!("error getting local swarm addresses: {}", e), + } - let connected = client.swarm_peers().map(|connected| { - println!(); - println!("connected:"); - for peer in connected.peers { - let streams: Vec<&str> = peer.streams.iter().map(|s| &s.protocol[..]).collect(); - println!(" addr: {}", peer.addr); - println!(" peer: {}", peer.peer); - println!(" latency: {}", peer.latency); - println!(" muxer: {}", peer.muxer); - println!(" streams: {}", streams.join(", ")); - println!(); + match client.swarm_peers().await { + Ok(connected) => { + eprintln!("connected:"); + for peer in connected.peers { + let streams: Vec<&str> = peer.streams.iter().map(|s| &s.protocol[..]).collect(); + eprintln!(" addr: {}", peer.addr); + eprintln!(" peer: {}", peer.peer); + eprintln!(" latency: {}", peer.latency); + eprintln!(" muxer: {}", peer.muxer); + eprintln!(" streams: {}", streams.join(", ")); + eprintln!(); + } } - }); - - let fut = local - .and_then(|_| connected) - .map_err(|e| eprintln!("{}", e)); - - Runtime::new() - .expect("tokio runtime") - .block_on(fut) - .expect("successful response"); + Err(e) => eprintln!("error getting swarm peers: {}", e), + } } -- cgit v1.2.3