summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFerris Tseng <ferris@navapbc.com>2019-12-24 11:56:01 -0500
committerFerris Tseng <ferris@navapbc.com>2019-12-24 11:56:01 -0500
commitb566becabadd28a86d4c6bc00cde252916a61f7f (patch)
tree005a868a9778568a58d6f2245cbb9497cf3306c7
parent62423a12e2402b8036e2a9e7f3781bdcb18bda50 (diff)
finish updating examples
-rw-r--r--ipfs-api/examples/dns.rs10
-rw-r--r--ipfs-api/examples/get_version.rs20
-rw-r--r--ipfs-api/examples/mfs.rs103
-rw-r--r--ipfs-api/examples/ping_peer.rs81
-rw-r--r--ipfs-api/examples/pubsub.rs77
-rw-r--r--ipfs-api/examples/replace_config.rs21
-rw-r--r--ipfs-api/examples/resolve_name.rs59
7 files changed, 187 insertions, 184 deletions
diff --git a/ipfs-api/examples/dns.rs b/ipfs-api/examples/dns.rs
index 128a35a..327913a 100644
--- a/ipfs-api/examples/dns.rs
+++ b/ipfs-api/examples/dns.rs
@@ -16,7 +16,7 @@ async fn main() {
let client = IpfsClient::default();
- let dns = match client.dns("ipfs.io", false).await {
+ let dns = match client.dns("ipfs.io", true).await {
Ok(dns) => {
eprintln!("dns resolves to ({})", &dns.path);
eprintln!();
@@ -29,13 +29,11 @@ async fn main() {
}
};
- match client.file_ls(&dns.path[..]).await {
+ match client.object_get(&dns.path[..]).await {
Ok(contents) => {
eprintln!("found contents:");
- for directory in contents.objects.values() {
- for file in directory.links.iter() {
- eprintln!("[{}] ({} bytes)", file.name, file.size);
- }
+ for link in contents.links.iter() {
+ eprintln!("[{}] ({} bytes)", link.name, link.size);
}
}
Err(e) => eprintln!("error listing path: {}", e),
diff --git a/ipfs-api/examples/get_version.rs b/ipfs-api/examples/get_version.rs
index 992f249..d38b322 100644
--- a/ipfs-api/examples/get_version.rs
+++ b/ipfs-api/examples/get_version.rs
@@ -6,24 +6,18 @@
// 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 the version of 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
- .version()
- .map(|version| println!("version: {:?}", version.version));
- let fut = req.map_err(|e| eprintln!("{}", e));
-
- Runtime::new()
- .expect("tokio runtime")
- .block_on(fut)
- .expect("successful response");
+ match client.version().await {
+ Ok(version) => eprintln!("version: {:?}", version.version),
+ Err(e) => eprintln!("error getting version: {}", e),
+ }
}
diff --git a/ipfs-api/examples/mfs.rs b/ipfs-api/examples/mfs.rs
index 30a9af4..b6e3c99 100644
--- a/ipfs-api/examples/mfs.rs
+++ b/ipfs-api/examples/mfs.rs
@@ -6,78 +6,81 @@
// copied, modified, or distributed except according to those terms.
//
-use futures::Future;
use ipfs_api::{response, IpfsClient};
use std::fs::File;
-use tokio::runtime::current_thread::Runtime;
fn print_stat(stat: response::FilesStatResponse) {
- println!(" type : {}", stat.typ);
- println!(" hash : {}", stat.hash);
- println!(" size : {}", stat.size);
- println!(" cum. size: {}", stat.cumulative_size);
- println!(" blocks : {}", stat.blocks);
- println!();
+ eprintln!(" type : {}", stat.typ);
+ eprintln!(" hash : {}", stat.hash);
+ eprintln!(" size : {}", stat.size);
+ eprintln!(" cum. size: {}", stat.cumulative_size);
+ eprintln!(" blocks : {}", stat.blocks);
+ eprintln!();
}
// Creates an Ipfs client, and makes some calls to the Mfs Api.
//
-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();
- println!("making /test...");
- println!();
+ eprintln!("making /test...");
+ eprintln!();
- let mkdir = client.files_mkdir("/test", false);
- let mkdir_recursive = client.files_mkdir("/test/does/not/exist/yet", true);
+ if let Err(e) = client.files_mkdir("/test", false).await {
+ eprintln!("error making /test: {}", e);
+ return;
+ }
- let file_stat = client.files_stat("/test/does");
+ eprintln!("making dirs /test/does/not/exist/yet...");
+ eprintln!();
- let src = File::open(file!()).expect("could not read source file");
- let file_write = client.files_write("/test/mfs.rs", true, true, src);
+ if let Err(e) = client.files_mkdir("/test/does/not/exist/yet", true).await {
+ eprintln!("error making /test/does/not/exist/yet: {}", e);
+ return;
+ }
- let file_write_stat = client.files_stat("/test/mfs.rs");
+ eprintln!("getting status of /test/does...");
+ eprintln!();
- let file_rm = client.files_rm("/test", true);
+ match client.files_stat("/test/does").await {
+ Ok(stat) => print_stat(stat),
+ Err(e) => {
+ eprintln!("error getting status of /test/does: {}", e);
+ return;
+ }
+ }
- let fut = mkdir
- .and_then(|_| {
- println!("making dirs /test/does/not/exist/yet...");
- println!();
+ eprintln!("writing source file to /test/mfs.rs");
+ eprintln!();
- mkdir_recursive
- })
- .and_then(|_| {
- println!("getting status of /test/does...");
- println!();
+ let src = File::open(file!()).expect("could not read source file");
- file_stat
- })
- .and_then(|stat| {
- print_stat(stat);
+ if let Err(e) = client.files_write("/test/mfs.rs", true, true, src).await {
+ eprintln!("error writing source file /test/mfs.rs: {}", e);
+ return;
+ }
- println!("writing source file to /test/mfs.rs");
- println!();
+ eprintln!("getting status of /test/mfs.rs...");
+ eprintln!();
- file_write
- })
- .and_then(|_| file_write_stat)
- .and_then(|stat| {
- print_stat(stat);
+ match client.files_stat("/test/mfs.rs").await {
+ Ok(stat) => print_stat(stat),
+ Err(e) => {
+ eprintln!("error getting status of /test/mfs.rs: {}", e);
+ return;
+ }
+ }
- println!("removing /test...");
- println!();
+ eprintln!("removing /test...");
+ eprintln!();
- file_rm
- })
- .map(|_| println!("done!"))
- .map_err(|e| eprintln!("{}", e));
+ if let Err(e) = client.files_rm("/test", true).await {
+ eprintln!("error removing /test: {}", e);
+ }
- Runtime::new()
- .expect("tokio runtime")
- .block_on(fut)
- .expect("successful response");
+ eprintln!("done!");
}
diff --git a/ipfs-api/examples/ping_peer.rs b/ipfs-api/examples/ping_peer.rs
index 72773ec..b432ef6 100644
--- a/ipfs-api/examples/ping_peer.rs
+++ b/ipfs-api/examples/ping_peer.rs
@@ -6,58 +6,63 @@
// copied, modified, or distributed except according to those terms.
//
-use futures::{Future, Stream};
+use futures::{future, TryStreamExt};
use ipfs_api::{response::PingResponse, IpfsClient};
-use tokio::runtime::current_thread::Runtime;
// Creates an Ipfs client, discovers a connected peer, and pings it using the
// streaming Api, and by collecting it into a collection.
//
-fn main() {
- println!("connecting to localhost:5001...");
+#[tokio::main]
+async fn main() {
+ eprintln!("connecting to localhost:5001...");
let client = IpfsClient::default();
- println!();
- println!("discovering connected peers...");
+ eprintln!();
+ eprintln!("discovering connected peers...");
- let req = client
- .swarm_peers()
- .and_then(move |connected| {
- let peer = connected
- .peers
- .iter()
- .next()
- .expect("expected at least one peer");
+ let peer = match client.swarm_peers().await {
+ Ok(connected) => connected
+ .peers
+ .into_iter()
+ .next()
+ .expect("expected at least one peer"),
+ Err(e) => {
+ eprintln!("error getting connected peers: {}", e);
+ return;
+ }
+ };
- println!();
- println!("discovered peer ({})", peer.peer);
- println!();
- println!("streaming 10 pings...");
+ eprintln!();
+ eprintln!("discovered peer ({})", peer.peer);
+ eprintln!();
+ eprintln!("streaming 10 pings...");
- let ping_stream = client.ping(&peer.peer[..], Some(10)).for_each(|ping| {
- println!("{:?}", ping);
- Ok(())
- });
+ if let Err(e) = client
+ .ping(&peer.peer[..], Some(10))
+ .try_for_each(|ping| {
+ eprintln!("{:?}", ping);
- let ping_gather = client.ping(&peer.peer[..], Some(15)).collect();
+ future::ok(())
+ })
+ .await
+ {
+ eprintln!("error streaming pings: {}", e);
+ }
- ping_stream.and_then(|_| {
- println!();
- println!("gathering 15 pings...");
+ eprintln!();
+ eprintln!("gathering 15 pings...");
- ping_gather
- })
- })
- .map(|pings: Vec<PingResponse>| {
+ match client
+ .ping(&peer.peer[..], Some(15))
+ .try_collect::<Vec<PingResponse>>()
+ .await
+ {
+ Ok(pings) => {
for ping in pings.iter() {
- println!("got response ({:?}) at ({})...", ping.text, ping.time);
+ eprintln!("got response ({:?}) at ({})...", ping.text, ping.time);
}
- })
- .map_err(|e| eprintln!("{}", e));
-
- Runtime::new()
- .expect("tokio runtime")
- .block_on(req)
- .expect("successful response");
+ }
+ Err(e) => eprintln!("error collecting pings: {}", e),
+ }
}
diff --git a/ipfs-api/examples/pubsub.rs b/ipfs-api/examples/pubsub.rs
index 924eab6..16b674e 100644
--- a/ipfs-api/examples/pubsub.rs
+++ b/ipfs-api/examples/pubsub.rs
@@ -6,16 +6,18 @@
// copied, modified, or distributed except according to those terms.
//
-use futures::{Future, Stream};
+#[macro_use]
+extern crate futures;
+
+use futures::{future, FutureExt, StreamExt, TryStreamExt};
use ipfs_api::IpfsClient;
-use std::time::{Duration, Instant};
-use tokio::runtime::current_thread::Runtime;
-use tokio_timer::Interval;
+use std::time::Duration;
+use tokio::time;
static TOPIC: &'static str = "test";
fn get_client() -> IpfsClient {
- println!("connecting to localhost:5001...");
+ eprintln!("connecting to localhost:5001...");
IpfsClient::default()
}
@@ -23,50 +25,55 @@ fn get_client() -> IpfsClient {
// Creates an Ipfs client, and simultaneously publishes and reads from a pubsub
// topic.
//
-fn main() {
- let mut rt = Runtime::new().expect("tokio runtime");
+#[tokio::main]
+async fn main() {
+ eprintln!("note: ipfs must be run with the --enable-pubsub-experiment flag");
+
+ let publish_client = get_client();
// This block will execute a repeating function that sends
// a message to the "test" topic.
//
- {
- let client = get_client();
- let publish = Interval::new(Instant::now(), Duration::from_secs(1))
- .map_err(|e| eprintln!("{}", e))
- .for_each(move |_| {
- println!();
- println!("publishing message...");
-
- client
- .pubsub_pub(TOPIC, "Hello World!")
- .map_err(|e| eprintln!("{}", e))
- });
+ let mut publish = time::interval(Duration::from_secs(1))
+ .then(|_| future::ok(())) // Coerce the stream into a TryStream
+ .try_for_each(|_| {
+ eprintln!();
+ eprintln!("publishing message...");
- println!();
- println!("starting task to publish messages to ({})...", TOPIC);
-
- rt.spawn(publish);
- }
+ publish_client.pubsub_pub(TOPIC, "Hello World!")
+ })
+ .boxed()
+ .fuse();
// This block will execute a future that suscribes to a topic,
// and reads any incoming messages.
//
- {
+ let mut subscribe = {
let client = get_client();
- let req = client.pubsub_sub(TOPIC, false);
- println!();
- println!("waiting for messages on ({})...", TOPIC);
- let fut = req
+ client
+ .pubsub_sub(TOPIC, false)
.take(5)
- .for_each(|msg| {
- println!();
- println!("received ({:?})", msg);
+ .try_for_each(|msg| {
+ eprintln!();
+ eprintln!("received ({:?})", msg);
- Ok(())
+ future::ok(())
})
- .map_err(|e| eprintln!("{}", e));
+ .fuse()
+ };
+
+ eprintln!();
+ eprintln!("publish messages to ({})...", TOPIC);
+ eprintln!("waiting for messages from ({})...", TOPIC);
- rt.block_on(fut).expect("successful response");
+ select! {
+ res = publish => if let Err(e) = res {
+ eprintln!("error publishing messages: {}", e);
+ },
+ res = subscribe => match res {
+ Ok(_) => eprintln!("done reading messages..."),
+ Err(e) => eprintln!("error reading messages: {}", e)
+ },
}
}
diff --git a/ipfs-api/examples/replace_config.rs b/ipfs-api/examples/replace_config.rs
index 65d418e..f19c113 100644
--- a/ipfs-api/examples/replace_config.rs
+++ b/ipfs-api/examples/replace_config.rs
@@ -6,26 +6,21 @@
// copied, modified, or distributed except according to those terms.
//
-use futures::Future;
use ipfs_api::IpfsClient;
use std::io::Cursor;
-use tokio::runtime::current_thread::Runtime;
// Creates an Ipfs client, and replaces the config file with the default one.
//
-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 default_config = include_str!("default_config.json");
- let req = client
- .config_replace(Cursor::new(default_config))
- .map(|_| println!("replaced file"))
- .map_err(|e| println!("{}", e));
- Runtime::new()
- .expect("tokio runtime")
- .block_on(req)
- .expect("successful response");
+ match client.config_replace(Cursor::new(default_config)).await {
+ Ok(_) => eprintln!("replaced config file"),
+ Err(e) => eprintln!("error replacing config file: {}", e),
+ }
}
diff --git a/ipfs-api/examples/resolve_name.rs b/ipfs-api/examples/resolve_name.rs
index d50011f..2b263d5 100644
--- a/ipfs-api/examples/resolve_name.rs
+++ b/ipfs-api/examples/resolve_name.rs
@@ -6,43 +6,44 @@
// copied, modified, or distributed except according to those terms.
//
-use futures::Future;
use ipfs_api::IpfsClient;
-use tokio::runtime::current_thread::Runtime;
const IPFS_IPNS: &str = "/ipns/ipfs.io";
// Creates an Ipfs client, and resolves the Ipfs domain name, and
// publishes a path to Ipns.
//
-fn main() {
- println!("connecting to localhost:5001...");
+#[tokio::main]
+async fn main() {
+ eprintln!("connecting to localhost:5001...");
let client = IpfsClient::default();
- let name_resolve = client
- .name_resolve(Some(IPFS_IPNS), true, false)
- .map(|resolved| {
- println!("{} resolves to: {}", IPFS_IPNS, &resolved.path);
- });
- let name_publish = client
- .name_publish(IPFS_IPNS, true, None, None, None)
- .and_then(move |publish| {
- println!("published {} to: /ipns/{}", IPFS_IPNS, &publish.name);
-
- client
- .name_resolve(Some(&publish.name), true, false)
- .map(move |resolved| {
- println!("/ipns/{} resolves to: {}", &publish.name, &resolved.path);
- })
- });
-
- let fut = name_resolve
- .and_then(|_| name_publish)
- .map_err(|e| eprintln!("{}", e));
-
- Runtime::new()
- .expect("tokio runtime")
- .block_on(fut)
- .expect("successful response");
+ match client.name_resolve(Some(IPFS_IPNS), true, false).await {
+ Ok(resolved) => eprintln!("{} resolves to: {}", IPFS_IPNS, &resolved.path),
+ Err(e) => {
+ eprintln!("error resolving {}: {}", IPFS_IPNS, e);
+ return;
+ }
+ }
+
+ let publish = match client.name_publish(IPFS_IPNS, true, None, None, None).await {
+ Ok(publish) => {
+ eprintln!("published {} to: /ipns/{}", IPFS_IPNS, &publish.name);
+ publish
+ }
+ Err(e) => {
+ eprintln!("error publishing name: {}", e);
+ return;
+ }
+ };
+
+ match client.name_resolve(Some(&publish.name), true, false).await {
+ Ok(resolved) => {
+ eprintln!("/ipns/{} resolves to: {}", &publish.name, &resolved.path);
+ }
+ Err(e) => {
+ eprintln!("error resolving name: {}", e);
+ }
+ }
}