From b82d94f7cde2a99bc6538fb5af08da08472b6640 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 24 Sep 2018 17:38:37 +0200 Subject: Add new "master" example, to contain all examples as subcommands. Only implements fetching the channel-stream for now. --- examples/channel_stream.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++ examples/zot.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 106 insertions(+) create mode 100644 examples/channel_stream.rs create mode 100644 examples/zot.rs (limited to 'examples') diff --git a/examples/channel_stream.rs b/examples/channel_stream.rs new file mode 100644 index 0000000..4e77d1c --- /dev/null +++ b/examples/channel_stream.rs @@ -0,0 +1,52 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +use serde_json; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match client.channel_stream() { + Ok(payload) => { + if raw { + println!("{}", payload); + } + else { + print(&payload); + } + }, + Err(e) => { + println!("Error getting channel stream: {:?}", e); + } + } +} + +fn print(payload: &str) { + match serde_json::from_str(&payload) { + Ok(serde_json::Value::Array(v)) => { + for item in v.into_iter() { + println!("{} {} {}", item["title"], item["type"], item["author"]["name"]); + } + }, + Ok(_) => { + println!("Wrong type returned, expected an array.") + }, + Err(e) => { + println!("Error: {}", e); + } + } +} diff --git a/examples/zot.rs b/examples/zot.rs new file mode 100644 index 0000000..a0d930e --- /dev/null +++ b/examples/zot.rs @@ -0,0 +1,54 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +extern crate zotapi; +extern crate dotenv; +#[macro_use] +extern crate clap; +extern crate serde_json; + +use clap::{Arg, SubCommand}; +use dotenv::dotenv; +use std::env; + +mod channel_stream; + +fn main() { + dotenv().ok(); + let site = env::var("HZ_SITE").expect("SITE variable expected"); + let user = env::var("HZ_USER").expect("USER variable expected"); + let password = env::var("HZ_PASSWORD").expect("PASSWORD variable expected"); + + let matches = app_from_crate!() + .subcommand( + SubCommand::with_name("channel-stream") + .about("Fetch the channel stream") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload"))) + .get_matches(); + + match matches.subcommand() { + ("channel-stream", Some(m)) => { + let raw = m.is_present("raw"); + channel_stream::fetch(&zotapi::client(&site, &user, &password), raw); + }, + _ => { + println!("{}", matches.usage()); + }, + } +} -- cgit v1.2.3 From f9f58140c86857da04ee1147d0b4b9c3c2734f23 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 7 Oct 2018 23:32:09 +0200 Subject: Expand channel_stream to output items as on the web. --- examples/channel_stream.rs | 55 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 52 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/channel_stream.rs b/examples/channel_stream.rs index 4e77d1c..38b0ee4 100644 --- a/examples/channel_stream.rs +++ b/examples/channel_stream.rs @@ -18,6 +18,7 @@ use zotapi; use serde_json; +use std::iter::Iterator; pub fn fetch(client: &zotapi::Client, raw: bool) { match client.channel_stream() { @@ -26,7 +27,7 @@ pub fn fetch(client: &zotapi::Client, raw: bool) { println!("{}", payload); } else { - print(&payload); + list(&payload); } }, Err(e) => { @@ -35,11 +36,12 @@ pub fn fetch(client: &zotapi::Client, raw: bool) { } } -fn print(payload: &str) { +fn list(payload: &str) { match serde_json::from_str(&payload) { Ok(serde_json::Value::Array(v)) => { for item in v.into_iter() { - println!("{} {} {}", item["title"], item["type"], item["author"]["name"]); + print_item(&item); + println!("-----"); } }, Ok(_) => { @@ -50,3 +52,50 @@ fn print(payload: &str) { } } } + +fn get_str(v: &serde_json::Value) -> &str { + v.as_str().unwrap_or("") +} + +fn get_verb(item: &serde_json::Value) -> &str { + match get_str(&item["verb"]).split("/").last() { + Some("post") => "posted", + Some("like") => "liked", + Some("share") => "shared", + Some("dislike") => "disliked", + _ => "unknown verb", + } +} + +fn get_object_type(item: &serde_json::Value) -> &str { + get_str(&item["object_type"]) + .split("/") + .last() + .unwrap_or("unknown object") +} + +fn get_hashtags(item: &serde_json::Value) -> String { + match item["tags"] { + serde_json::Value::Array(ref v) => { + v.iter().map(|t| get_str(&t["tag"])).collect::>().join(", ") + }, + serde_json::Value::Null => { + String::new() + }, + _ => { + String::from("invalid tags, expected array...") + } + } +} + +fn print_item(item: &serde_json::Value) { + let author = &item["author"]; + print!("{} ", get_str(&item["created"])); + print!("{} ", get_str(&author["name"])); + println!("{} a {}", get_verb(&item), get_object_type(&item)); + println!("URL: {}", get_str(&author["url"])); + println!("Proto: {}", get_str(&author["network"])); + println!("Title: {}", get_str(&item["title"])); + println!("Tags : {}", get_hashtags(&item)); + println!("Message:\n{}", get_str(&item["body"])); +} -- cgit v1.2.3 From 8d20318f22fa0344fa8804e15263b7736c6eff15 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 8 Oct 2018 10:14:24 +0200 Subject: Differ between different type of tags in channel_stream example. --- examples/channel_stream.rs | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'examples') diff --git a/examples/channel_stream.rs b/examples/channel_stream.rs index 38b0ee4..a278244 100644 --- a/examples/channel_stream.rs +++ b/examples/channel_stream.rs @@ -74,10 +74,18 @@ fn get_object_type(item: &serde_json::Value) -> &str { .unwrap_or("unknown object") } -fn get_hashtags(item: &serde_json::Value) -> String { +fn get_tags(item: &serde_json::Value) -> String { match item["tags"] { serde_json::Value::Array(ref v) => { - v.iter().map(|t| get_str(&t["tag"])).collect::>().join(", ") + v.iter().map(|t| { + let prefix = match get_str(&t["type"]) { + "hashtag" => "#", + "forum" => "!", + "mention" => "@", + _ => "", + }; + format!("{}{}", prefix, get_str(&t["tag"])) + }).collect::>().join(", ") }, serde_json::Value::Null => { String::new() @@ -96,6 +104,6 @@ fn print_item(item: &serde_json::Value) { println!("URL: {}", get_str(&author["url"])); println!("Proto: {}", get_str(&author["network"])); println!("Title: {}", get_str(&item["title"])); - println!("Tags : {}", get_hashtags(&item)); + println!("Tags : {}", get_tags(&item)); println!("Message:\n{}", get_str(&item["body"])); } -- cgit v1.2.3 From fc7735d33a562f71729e96514b722db7d6f55f05 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 8 Oct 2018 13:48:11 +0200 Subject: Add fetching network_stream to main example. This duplicates a lot of the code from the channel_stream example, will try to refactor later. --- examples/network_stream.rs | 109 +++++++++++++++++++++++++++++++++++++++++++++ examples/zot.rs | 15 ++++++- 2 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 examples/network_stream.rs (limited to 'examples') diff --git a/examples/network_stream.rs b/examples/network_stream.rs new file mode 100644 index 0000000..de3b12f --- /dev/null +++ b/examples/network_stream.rs @@ -0,0 +1,109 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +use serde_json; +use std::iter::Iterator; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match client.network_stream() { + Ok(payload) => { + if raw { + println!("{}", payload); + } + else { + list(&payload); + } + }, + Err(e) => { + println!("Error getting channel stream: {:?}", e); + } + } +} + +fn list(payload: &str) { + match serde_json::from_str(&payload) { + Ok(serde_json::Value::Array(v)) => { + for item in v.into_iter() { + print_item(&item); + println!("-----"); + } + }, + Ok(_) => { + println!("Wrong type returned, expected an array.") + }, + Err(e) => { + println!("Error: {}", e); + } + } +} + +fn get_str(v: &serde_json::Value) -> &str { + v.as_str().unwrap_or("") +} + +fn get_verb(item: &serde_json::Value) -> &str { + match get_str(&item["verb"]).split("/").last() { + Some("post") => "posted", + Some("like") => "liked", + Some("share") => "shared", + Some("dislike") => "disliked", + _ => "unknown verb", + } +} + +fn get_object_type(item: &serde_json::Value) -> &str { + get_str(&item["object_type"]) + .split("/") + .last() + .unwrap_or("unknown object") +} + +fn get_tags(item: &serde_json::Value) -> String { + match item["tags"] { + serde_json::Value::Array(ref v) => { + v.iter().map(|t| { + let prefix = match get_str(&t["type"]) { + "hashtag" => "#", + "forum" => "!", + "mention" => "@", + _ => "", + }; + format!("{}{}", prefix, get_str(&t["tag"])) + }).collect::>().join(", ") + }, + serde_json::Value::Null => { + String::new() + }, + _ => { + String::from("invalid tags, expected array...") + } + } +} + +fn print_item(item: &serde_json::Value) { + let author = &item["author"]; + print!("{} ", get_str(&item["created"])); + print!("{} ", get_str(&author["name"])); + println!("{} a {}", get_verb(&item), get_object_type(&item)); + println!("URL: {}", get_str(&author["url"])); + println!("Proto: {}", get_str(&author["network"])); + println!("Title: {}", get_str(&item["title"])); + println!("Tags : {}", get_tags(&item)); + println!("Message:\n{}", get_str(&item["body"])); +} diff --git a/examples/zot.rs b/examples/zot.rs index a0d930e..2c49fe3 100644 --- a/examples/zot.rs +++ b/examples/zot.rs @@ -26,6 +26,7 @@ use dotenv::dotenv; use std::env; mod channel_stream; +mod network_stream; fn main() { dotenv().ok(); @@ -40,12 +41,24 @@ fn main() { .arg(Arg::with_name("raw") .long("raw") .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("network-stream") + .about("Fetch the network stream") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload"))) .get_matches(); + let client = zotapi::client(&site, &user, &password); + match matches.subcommand() { ("channel-stream", Some(m)) => { let raw = m.is_present("raw"); - channel_stream::fetch(&zotapi::client(&site, &user, &password), raw); + channel_stream::fetch(&client, raw); + }, + ("network-stream", Some(m)) => { + let raw = m.is_present("raw"); + network_stream::fetch(&client, raw); }, _ => { println!("{}", matches.usage()); -- cgit v1.2.3 From 2d8e3c5cd1cc3fdcec838710af377bfa32718fbd Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 8 Oct 2018 19:57:23 +0200 Subject: Add abook api to main example. --- examples/abook.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ examples/zot.rs | 11 +++++++++++ 2 files changed, 63 insertions(+) create mode 100644 examples/abook.rs (limited to 'examples') diff --git a/examples/abook.rs b/examples/abook.rs new file mode 100644 index 0000000..bceaacc --- /dev/null +++ b/examples/abook.rs @@ -0,0 +1,52 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +extern crate zotapi; +extern crate serde_json; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match client.abook().fetch() { + Ok(payload) => { + if raw { + println!("{}", &payload); + } + else { + process(&payload); + } + }, + Err(e) => { + println!("{:?}", e); + } + } +} + +fn process(payload: &str) { + let data: serde_json::Value = serde_json::from_str(&payload).unwrap(); + match data { + serde_json::Value::Array(v) => { + for contact in v { + println!("{} ({}, {})", + contact["xchan_name"], + contact["xchan_addr"], + contact["xchan_network"]); + } + }, + _ => { + println!("Unexpected data:\n{}", payload); + } + }; +} diff --git a/examples/zot.rs b/examples/zot.rs index 2c49fe3..5fa4f09 100644 --- a/examples/zot.rs +++ b/examples/zot.rs @@ -25,6 +25,7 @@ use clap::{Arg, SubCommand}; use dotenv::dotenv; use std::env; +mod abook; mod channel_stream; mod network_stream; @@ -47,6 +48,12 @@ fn main() { .arg(Arg::with_name("raw") .long("raw") .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("abook") + .about("Fetch address book/contact info") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload"))) .get_matches(); let client = zotapi::client(&site, &user, &password); @@ -60,6 +67,10 @@ fn main() { let raw = m.is_present("raw"); network_stream::fetch(&client, raw); }, + ("abook", Some(m)) => { + let raw = m.is_present("raw"); + abook::fetch(&client, raw); + }, _ => { println!("{}", matches.usage()); }, -- cgit v1.2.3 From 8760299b1c5ca352db2146c5ed63a3ed8bd2c599 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 8 Oct 2018 21:09:20 +0200 Subject: Add fetching xchans to main sample. --- examples/xchan.rs | 41 +++++++++++++++++++++++++++++++++++++++++ examples/zot.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 examples/xchan.rs (limited to 'examples') diff --git a/examples/xchan.rs b/examples/xchan.rs new file mode 100644 index 0000000..d37328d --- /dev/null +++ b/examples/xchan.rs @@ -0,0 +1,41 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +pub enum Type { + Addr, + Hash, + GUID, +} + +pub fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) { + let res = match t { + Type::Addr => client.xchan().by_address(&id).fetch(), + Type::Hash => client.xchan().by_hash(&id).fetch(), + Type::GUID => client.xchan().by_guid(&id).fetch(), + }; + + match res { + Ok(payload) => { + println!("{}", payload); + }, + Err(e) => { + println!("{:?}", e); + } + } +} diff --git a/examples/zot.rs b/examples/zot.rs index 5fa4f09..3ad9bf0 100644 --- a/examples/zot.rs +++ b/examples/zot.rs @@ -28,6 +28,7 @@ use std::env; mod abook; mod channel_stream; mod network_stream; +mod xchan; fn main() { dotenv().ok(); @@ -54,6 +55,24 @@ fn main() { .arg(Arg::with_name("raw") .long("raw") .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("xchan") + .about("Fetch xchan info") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload")) + .arg(Arg::with_name("addr") + .long("addr") + .help("ID is given as a webbie address (default)")) + .arg(Arg::with_name("hash") + .long("hash") + .help("ID is given as a xchan hash")) + .arg(Arg::with_name("guid") + .long("guid") + .help("ID is given as a GUID")) + .arg(Arg::with_name("ID") + .help("id (email, hash or GUID) of xchan to fetch") + .required(true))) .get_matches(); let client = zotapi::client(&site, &user, &password); @@ -71,6 +90,20 @@ fn main() { let raw = m.is_present("raw"); abook::fetch(&client, raw); }, + ("xchan", Some(m)) => { + let raw = m.is_present("raw"); + let t = if m.is_present("guid") { + xchan::Type::GUID + } + else if m.is_present("hash") { + xchan::Type::Hash + } + else { + xchan::Type::Addr + }; + + xchan::fetch(&client, raw, t, m.value_of("ID").unwrap()); + }, _ => { println!("{}", matches.usage()); }, -- cgit v1.2.3 From 75dcb1e9dc2c554e7193daeb1a84157fbcd8603d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 28 Oct 2018 22:12:17 +0100 Subject: Move zot cli example modules into submodule. Also rename example from zot to zotcli to avoid name clash. --- examples/abook.rs | 52 ------------------- examples/channel_stream.rs | 109 ---------------------------------------- examples/network_stream.rs | 109 ---------------------------------------- examples/xchan.rs | 41 --------------- examples/zot.rs | 111 ----------------------------------------- examples/zot/abook.rs | 52 +++++++++++++++++++ examples/zot/channel_stream.rs | 109 ++++++++++++++++++++++++++++++++++++++++ examples/zot/mod.rs | 4 ++ examples/zot/network_stream.rs | 109 ++++++++++++++++++++++++++++++++++++++++ examples/zot/xchan.rs | 41 +++++++++++++++ examples/zotcli.rs | 108 +++++++++++++++++++++++++++++++++++++++ 11 files changed, 423 insertions(+), 422 deletions(-) delete mode 100644 examples/abook.rs delete mode 100644 examples/channel_stream.rs delete mode 100644 examples/network_stream.rs delete mode 100644 examples/xchan.rs delete mode 100644 examples/zot.rs create mode 100644 examples/zot/abook.rs create mode 100644 examples/zot/channel_stream.rs create mode 100644 examples/zot/mod.rs create mode 100644 examples/zot/network_stream.rs create mode 100644 examples/zot/xchan.rs create mode 100644 examples/zotcli.rs (limited to 'examples') diff --git a/examples/abook.rs b/examples/abook.rs deleted file mode 100644 index bceaacc..0000000 --- a/examples/abook.rs +++ /dev/null @@ -1,52 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -extern crate zotapi; -extern crate serde_json; - -pub fn fetch(client: &zotapi::Client, raw: bool) { - match client.abook().fetch() { - Ok(payload) => { - if raw { - println!("{}", &payload); - } - else { - process(&payload); - } - }, - Err(e) => { - println!("{:?}", e); - } - } -} - -fn process(payload: &str) { - let data: serde_json::Value = serde_json::from_str(&payload).unwrap(); - match data { - serde_json::Value::Array(v) => { - for contact in v { - println!("{} ({}, {})", - contact["xchan_name"], - contact["xchan_addr"], - contact["xchan_network"]); - } - }, - _ => { - println!("Unexpected data:\n{}", payload); - } - }; -} diff --git a/examples/channel_stream.rs b/examples/channel_stream.rs deleted file mode 100644 index a278244..0000000 --- a/examples/channel_stream.rs +++ /dev/null @@ -1,109 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use zotapi; - -use serde_json; -use std::iter::Iterator; - -pub fn fetch(client: &zotapi::Client, raw: bool) { - match client.channel_stream() { - Ok(payload) => { - if raw { - println!("{}", payload); - } - else { - list(&payload); - } - }, - Err(e) => { - println!("Error getting channel stream: {:?}", e); - } - } -} - -fn list(payload: &str) { - match serde_json::from_str(&payload) { - Ok(serde_json::Value::Array(v)) => { - for item in v.into_iter() { - print_item(&item); - println!("-----"); - } - }, - Ok(_) => { - println!("Wrong type returned, expected an array.") - }, - Err(e) => { - println!("Error: {}", e); - } - } -} - -fn get_str(v: &serde_json::Value) -> &str { - v.as_str().unwrap_or("") -} - -fn get_verb(item: &serde_json::Value) -> &str { - match get_str(&item["verb"]).split("/").last() { - Some("post") => "posted", - Some("like") => "liked", - Some("share") => "shared", - Some("dislike") => "disliked", - _ => "unknown verb", - } -} - -fn get_object_type(item: &serde_json::Value) -> &str { - get_str(&item["object_type"]) - .split("/") - .last() - .unwrap_or("unknown object") -} - -fn get_tags(item: &serde_json::Value) -> String { - match item["tags"] { - serde_json::Value::Array(ref v) => { - v.iter().map(|t| { - let prefix = match get_str(&t["type"]) { - "hashtag" => "#", - "forum" => "!", - "mention" => "@", - _ => "", - }; - format!("{}{}", prefix, get_str(&t["tag"])) - }).collect::>().join(", ") - }, - serde_json::Value::Null => { - String::new() - }, - _ => { - String::from("invalid tags, expected array...") - } - } -} - -fn print_item(item: &serde_json::Value) { - let author = &item["author"]; - print!("{} ", get_str(&item["created"])); - print!("{} ", get_str(&author["name"])); - println!("{} a {}", get_verb(&item), get_object_type(&item)); - println!("URL: {}", get_str(&author["url"])); - println!("Proto: {}", get_str(&author["network"])); - println!("Title: {}", get_str(&item["title"])); - println!("Tags : {}", get_tags(&item)); - println!("Message:\n{}", get_str(&item["body"])); -} diff --git a/examples/network_stream.rs b/examples/network_stream.rs deleted file mode 100644 index de3b12f..0000000 --- a/examples/network_stream.rs +++ /dev/null @@ -1,109 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use zotapi; - -use serde_json; -use std::iter::Iterator; - -pub fn fetch(client: &zotapi::Client, raw: bool) { - match client.network_stream() { - Ok(payload) => { - if raw { - println!("{}", payload); - } - else { - list(&payload); - } - }, - Err(e) => { - println!("Error getting channel stream: {:?}", e); - } - } -} - -fn list(payload: &str) { - match serde_json::from_str(&payload) { - Ok(serde_json::Value::Array(v)) => { - for item in v.into_iter() { - print_item(&item); - println!("-----"); - } - }, - Ok(_) => { - println!("Wrong type returned, expected an array.") - }, - Err(e) => { - println!("Error: {}", e); - } - } -} - -fn get_str(v: &serde_json::Value) -> &str { - v.as_str().unwrap_or("") -} - -fn get_verb(item: &serde_json::Value) -> &str { - match get_str(&item["verb"]).split("/").last() { - Some("post") => "posted", - Some("like") => "liked", - Some("share") => "shared", - Some("dislike") => "disliked", - _ => "unknown verb", - } -} - -fn get_object_type(item: &serde_json::Value) -> &str { - get_str(&item["object_type"]) - .split("/") - .last() - .unwrap_or("unknown object") -} - -fn get_tags(item: &serde_json::Value) -> String { - match item["tags"] { - serde_json::Value::Array(ref v) => { - v.iter().map(|t| { - let prefix = match get_str(&t["type"]) { - "hashtag" => "#", - "forum" => "!", - "mention" => "@", - _ => "", - }; - format!("{}{}", prefix, get_str(&t["tag"])) - }).collect::>().join(", ") - }, - serde_json::Value::Null => { - String::new() - }, - _ => { - String::from("invalid tags, expected array...") - } - } -} - -fn print_item(item: &serde_json::Value) { - let author = &item["author"]; - print!("{} ", get_str(&item["created"])); - print!("{} ", get_str(&author["name"])); - println!("{} a {}", get_verb(&item), get_object_type(&item)); - println!("URL: {}", get_str(&author["url"])); - println!("Proto: {}", get_str(&author["network"])); - println!("Title: {}", get_str(&item["title"])); - println!("Tags : {}", get_tags(&item)); - println!("Message:\n{}", get_str(&item["body"])); -} diff --git a/examples/xchan.rs b/examples/xchan.rs deleted file mode 100644 index d37328d..0000000 --- a/examples/xchan.rs +++ /dev/null @@ -1,41 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -use zotapi; - -pub enum Type { - Addr, - Hash, - GUID, -} - -pub fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) { - let res = match t { - Type::Addr => client.xchan().by_address(&id).fetch(), - Type::Hash => client.xchan().by_hash(&id).fetch(), - Type::GUID => client.xchan().by_guid(&id).fetch(), - }; - - match res { - Ok(payload) => { - println!("{}", payload); - }, - Err(e) => { - println!("{:?}", e); - } - } -} diff --git a/examples/zot.rs b/examples/zot.rs deleted file mode 100644 index 3ad9bf0..0000000 --- a/examples/zot.rs +++ /dev/null @@ -1,111 +0,0 @@ -/* Example Zot API command line utility, part of zotapi. - * Copyright (C) 2018 Harald Eilertsen - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -extern crate zotapi; -extern crate dotenv; -#[macro_use] -extern crate clap; -extern crate serde_json; - -use clap::{Arg, SubCommand}; -use dotenv::dotenv; -use std::env; - -mod abook; -mod channel_stream; -mod network_stream; -mod xchan; - -fn main() { - dotenv().ok(); - let site = env::var("HZ_SITE").expect("SITE variable expected"); - let user = env::var("HZ_USER").expect("USER variable expected"); - let password = env::var("HZ_PASSWORD").expect("PASSWORD variable expected"); - - let matches = app_from_crate!() - .subcommand( - SubCommand::with_name("channel-stream") - .about("Fetch the channel stream") - .arg(Arg::with_name("raw") - .long("raw") - .help("Display raw json payload"))) - .subcommand( - SubCommand::with_name("network-stream") - .about("Fetch the network stream") - .arg(Arg::with_name("raw") - .long("raw") - .help("Display raw json payload"))) - .subcommand( - SubCommand::with_name("abook") - .about("Fetch address book/contact info") - .arg(Arg::with_name("raw") - .long("raw") - .help("Display raw json payload"))) - .subcommand( - SubCommand::with_name("xchan") - .about("Fetch xchan info") - .arg(Arg::with_name("raw") - .long("raw") - .help("Display raw json payload")) - .arg(Arg::with_name("addr") - .long("addr") - .help("ID is given as a webbie address (default)")) - .arg(Arg::with_name("hash") - .long("hash") - .help("ID is given as a xchan hash")) - .arg(Arg::with_name("guid") - .long("guid") - .help("ID is given as a GUID")) - .arg(Arg::with_name("ID") - .help("id (email, hash or GUID) of xchan to fetch") - .required(true))) - .get_matches(); - - let client = zotapi::client(&site, &user, &password); - - match matches.subcommand() { - ("channel-stream", Some(m)) => { - let raw = m.is_present("raw"); - channel_stream::fetch(&client, raw); - }, - ("network-stream", Some(m)) => { - let raw = m.is_present("raw"); - network_stream::fetch(&client, raw); - }, - ("abook", Some(m)) => { - let raw = m.is_present("raw"); - abook::fetch(&client, raw); - }, - ("xchan", Some(m)) => { - let raw = m.is_present("raw"); - let t = if m.is_present("guid") { - xchan::Type::GUID - } - else if m.is_present("hash") { - xchan::Type::Hash - } - else { - xchan::Type::Addr - }; - - xchan::fetch(&client, raw, t, m.value_of("ID").unwrap()); - }, - _ => { - println!("{}", matches.usage()); - }, - } -} diff --git a/examples/zot/abook.rs b/examples/zot/abook.rs new file mode 100644 index 0000000..bceaacc --- /dev/null +++ b/examples/zot/abook.rs @@ -0,0 +1,52 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +extern crate zotapi; +extern crate serde_json; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match client.abook().fetch() { + Ok(payload) => { + if raw { + println!("{}", &payload); + } + else { + process(&payload); + } + }, + Err(e) => { + println!("{:?}", e); + } + } +} + +fn process(payload: &str) { + let data: serde_json::Value = serde_json::from_str(&payload).unwrap(); + match data { + serde_json::Value::Array(v) => { + for contact in v { + println!("{} ({}, {})", + contact["xchan_name"], + contact["xchan_addr"], + contact["xchan_network"]); + } + }, + _ => { + println!("Unexpected data:\n{}", payload); + } + }; +} diff --git a/examples/zot/channel_stream.rs b/examples/zot/channel_stream.rs new file mode 100644 index 0000000..a278244 --- /dev/null +++ b/examples/zot/channel_stream.rs @@ -0,0 +1,109 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +use serde_json; +use std::iter::Iterator; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match client.channel_stream() { + Ok(payload) => { + if raw { + println!("{}", payload); + } + else { + list(&payload); + } + }, + Err(e) => { + println!("Error getting channel stream: {:?}", e); + } + } +} + +fn list(payload: &str) { + match serde_json::from_str(&payload) { + Ok(serde_json::Value::Array(v)) => { + for item in v.into_iter() { + print_item(&item); + println!("-----"); + } + }, + Ok(_) => { + println!("Wrong type returned, expected an array.") + }, + Err(e) => { + println!("Error: {}", e); + } + } +} + +fn get_str(v: &serde_json::Value) -> &str { + v.as_str().unwrap_or("") +} + +fn get_verb(item: &serde_json::Value) -> &str { + match get_str(&item["verb"]).split("/").last() { + Some("post") => "posted", + Some("like") => "liked", + Some("share") => "shared", + Some("dislike") => "disliked", + _ => "unknown verb", + } +} + +fn get_object_type(item: &serde_json::Value) -> &str { + get_str(&item["object_type"]) + .split("/") + .last() + .unwrap_or("unknown object") +} + +fn get_tags(item: &serde_json::Value) -> String { + match item["tags"] { + serde_json::Value::Array(ref v) => { + v.iter().map(|t| { + let prefix = match get_str(&t["type"]) { + "hashtag" => "#", + "forum" => "!", + "mention" => "@", + _ => "", + }; + format!("{}{}", prefix, get_str(&t["tag"])) + }).collect::>().join(", ") + }, + serde_json::Value::Null => { + String::new() + }, + _ => { + String::from("invalid tags, expected array...") + } + } +} + +fn print_item(item: &serde_json::Value) { + let author = &item["author"]; + print!("{} ", get_str(&item["created"])); + print!("{} ", get_str(&author["name"])); + println!("{} a {}", get_verb(&item), get_object_type(&item)); + println!("URL: {}", get_str(&author["url"])); + println!("Proto: {}", get_str(&author["network"])); + println!("Title: {}", get_str(&item["title"])); + println!("Tags : {}", get_tags(&item)); + println!("Message:\n{}", get_str(&item["body"])); +} diff --git a/examples/zot/mod.rs b/examples/zot/mod.rs new file mode 100644 index 0000000..089a4cb --- /dev/null +++ b/examples/zot/mod.rs @@ -0,0 +1,4 @@ +pub mod abook; +pub mod channel_stream; +pub mod network_stream; +pub mod xchan; diff --git a/examples/zot/network_stream.rs b/examples/zot/network_stream.rs new file mode 100644 index 0000000..de3b12f --- /dev/null +++ b/examples/zot/network_stream.rs @@ -0,0 +1,109 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +use serde_json; +use std::iter::Iterator; + +pub fn fetch(client: &zotapi::Client, raw: bool) { + match client.network_stream() { + Ok(payload) => { + if raw { + println!("{}", payload); + } + else { + list(&payload); + } + }, + Err(e) => { + println!("Error getting channel stream: {:?}", e); + } + } +} + +fn list(payload: &str) { + match serde_json::from_str(&payload) { + Ok(serde_json::Value::Array(v)) => { + for item in v.into_iter() { + print_item(&item); + println!("-----"); + } + }, + Ok(_) => { + println!("Wrong type returned, expected an array.") + }, + Err(e) => { + println!("Error: {}", e); + } + } +} + +fn get_str(v: &serde_json::Value) -> &str { + v.as_str().unwrap_or("") +} + +fn get_verb(item: &serde_json::Value) -> &str { + match get_str(&item["verb"]).split("/").last() { + Some("post") => "posted", + Some("like") => "liked", + Some("share") => "shared", + Some("dislike") => "disliked", + _ => "unknown verb", + } +} + +fn get_object_type(item: &serde_json::Value) -> &str { + get_str(&item["object_type"]) + .split("/") + .last() + .unwrap_or("unknown object") +} + +fn get_tags(item: &serde_json::Value) -> String { + match item["tags"] { + serde_json::Value::Array(ref v) => { + v.iter().map(|t| { + let prefix = match get_str(&t["type"]) { + "hashtag" => "#", + "forum" => "!", + "mention" => "@", + _ => "", + }; + format!("{}{}", prefix, get_str(&t["tag"])) + }).collect::>().join(", ") + }, + serde_json::Value::Null => { + String::new() + }, + _ => { + String::from("invalid tags, expected array...") + } + } +} + +fn print_item(item: &serde_json::Value) { + let author = &item["author"]; + print!("{} ", get_str(&item["created"])); + print!("{} ", get_str(&author["name"])); + println!("{} a {}", get_verb(&item), get_object_type(&item)); + println!("URL: {}", get_str(&author["url"])); + println!("Proto: {}", get_str(&author["network"])); + println!("Title: {}", get_str(&item["title"])); + println!("Tags : {}", get_tags(&item)); + println!("Message:\n{}", get_str(&item["body"])); +} diff --git a/examples/zot/xchan.rs b/examples/zot/xchan.rs new file mode 100644 index 0000000..d37328d --- /dev/null +++ b/examples/zot/xchan.rs @@ -0,0 +1,41 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +pub enum Type { + Addr, + Hash, + GUID, +} + +pub fn fetch(client: &zotapi::Client, _raw: bool, t: Type, id: &str) { + let res = match t { + Type::Addr => client.xchan().by_address(&id).fetch(), + Type::Hash => client.xchan().by_hash(&id).fetch(), + Type::GUID => client.xchan().by_guid(&id).fetch(), + }; + + match res { + Ok(payload) => { + println!("{}", payload); + }, + Err(e) => { + println!("{:?}", e); + } + } +} diff --git a/examples/zotcli.rs b/examples/zotcli.rs new file mode 100644 index 0000000..2804754 --- /dev/null +++ b/examples/zotcli.rs @@ -0,0 +1,108 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +extern crate zotapi; +extern crate dotenv; +#[macro_use] +extern crate clap; +extern crate serde_json; + +use clap::{Arg, SubCommand}; +use dotenv::dotenv; +use std::env; + +mod zot; + +fn main() { + dotenv().ok(); + let site = env::var("HZ_SITE").expect("SITE variable expected"); + let user = env::var("HZ_USER").expect("USER variable expected"); + let password = env::var("HZ_PASSWORD").expect("PASSWORD variable expected"); + + let matches = app_from_crate!() + .subcommand( + SubCommand::with_name("channel-stream") + .about("Fetch the channel stream") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("network-stream") + .about("Fetch the network stream") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("abook") + .about("Fetch address book/contact info") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("xchan") + .about("Fetch xchan info") + .arg(Arg::with_name("raw") + .long("raw") + .help("Display raw json payload")) + .arg(Arg::with_name("addr") + .long("addr") + .help("ID is given as a webbie address (default)")) + .arg(Arg::with_name("hash") + .long("hash") + .help("ID is given as a xchan hash")) + .arg(Arg::with_name("guid") + .long("guid") + .help("ID is given as a GUID")) + .arg(Arg::with_name("ID") + .help("id (email, hash or GUID) of xchan to fetch") + .required(true))) + .get_matches(); + + let client = zotapi::client(&site, &user, &password); + + match matches.subcommand() { + ("channel-stream", Some(m)) => { + let raw = m.is_present("raw"); + zot::channel_stream::fetch(&client, raw); + }, + ("network-stream", Some(m)) => { + let raw = m.is_present("raw"); + zot::network_stream::fetch(&client, raw); + }, + ("abook", Some(m)) => { + let raw = m.is_present("raw"); + zot::abook::fetch(&client, raw); + }, + ("xchan", Some(m)) => { + let raw = m.is_present("raw"); + let t = if m.is_present("guid") { + zot::xchan::Type::GUID + } + else if m.is_present("hash") { + zot::xchan::Type::Hash + } + else { + zot::xchan::Type::Addr + }; + + zot::xchan::fetch(&client, raw, t, m.value_of("ID").unwrap()); + }, + _ => { + println!("{}", matches.usage()); + }, + } +} -- cgit v1.2.3 From 3e627a2e38d296b919e0b4439f2a93b0fb1d5e21 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Sun, 28 Oct 2018 22:31:08 +0100 Subject: Import macros explicitly in zot cli example. This requires rust 1.30.0 or later. --- examples/zotcli.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) (limited to 'examples') diff --git a/examples/zotcli.rs b/examples/zotcli.rs index 2804754..6b3a97b 100644 --- a/examples/zotcli.rs +++ b/examples/zotcli.rs @@ -17,11 +17,18 @@ extern crate zotapi; extern crate dotenv; -#[macro_use] extern crate clap; extern crate serde_json; -use clap::{Arg, SubCommand}; +use clap::{ + Arg, + app_from_crate, + crate_name, + crate_version, + crate_authors, + crate_description, + SubCommand +}; use dotenv::dotenv; use std::env; -- cgit v1.2.3 From 203de7061c04ae8fbe2b57e755da00c2f66a5a97 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 6 Nov 2018 17:18:31 +0100 Subject: Move the post new item example code to new example app. --- examples/zot/item.rs | 29 +++++++++++++++++++++++++++++ examples/zot/mod.rs | 1 + examples/zotcli.rs | 10 ++++++++++ 3 files changed, 40 insertions(+) create mode 100644 examples/zot/item.rs (limited to 'examples') diff --git a/examples/zot/item.rs b/examples/zot/item.rs new file mode 100644 index 0000000..ede1876 --- /dev/null +++ b/examples/zot/item.rs @@ -0,0 +1,29 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +pub fn post(client: &zotapi::Client, body: &str) { + match client.item().body(&body).create() { + Ok(payload) => { + println!("Raw payload: {}", payload); + }, + Err(e) => { + println!("Error posting message: {:?}", e); + } + } +} diff --git a/examples/zot/mod.rs b/examples/zot/mod.rs index 089a4cb..5e3874c 100644 --- a/examples/zot/mod.rs +++ b/examples/zot/mod.rs @@ -1,4 +1,5 @@ pub mod abook; pub mod channel_stream; +pub mod item; pub mod network_stream; pub mod xchan; diff --git a/examples/zotcli.rs b/examples/zotcli.rs index 6b3a97b..c3cfc8b 100644 --- a/examples/zotcli.rs +++ b/examples/zotcli.rs @@ -77,6 +77,12 @@ fn main() { .arg(Arg::with_name("ID") .help("id (email, hash or GUID) of xchan to fetch") .required(true))) + .subcommand( + SubCommand::with_name("post") + .about("Post a new message") + .arg(Arg::with_name("BODY") + .help("body of message") + .required(true))) .get_matches(); let client = zotapi::client(&site, &user, &password); @@ -108,6 +114,10 @@ fn main() { zot::xchan::fetch(&client, raw, t, m.value_of("ID").unwrap()); }, + ("post", Some(m)) => { + let body = m.value_of("BODY").unwrap(); + zot::item::post(&client, body); + }, _ => { println!("{}", matches.usage()); }, -- cgit v1.2.3 From 1b6bc078d6fa12ee62756c02dbaba0be671307b4 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 6 Nov 2018 17:34:54 +0100 Subject: Move abconfig example to new example app. --- examples/zot/abconfig.rs | 29 +++++++++++++++++++++++++++++ examples/zot/mod.rs | 1 + examples/zotcli.rs | 6 ++++++ 3 files changed, 36 insertions(+) create mode 100644 examples/zot/abconfig.rs (limited to 'examples') diff --git a/examples/zot/abconfig.rs b/examples/zot/abconfig.rs new file mode 100644 index 0000000..cc478ef --- /dev/null +++ b/examples/zot/abconfig.rs @@ -0,0 +1,29 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +use zotapi; + +pub fn fetch(client: &zotapi::Client) { + match client.abconfig().fetch() { + Ok(payload) => { + println!("{}", payload); + }, + Err(e) => { + println!("{:?}", e); + } + } +} diff --git a/examples/zot/mod.rs b/examples/zot/mod.rs index 5e3874c..3bb3c93 100644 --- a/examples/zot/mod.rs +++ b/examples/zot/mod.rs @@ -1,3 +1,4 @@ +pub mod abconfig; pub mod abook; pub mod channel_stream; pub mod item; diff --git a/examples/zotcli.rs b/examples/zotcli.rs index c3cfc8b..672e3ef 100644 --- a/examples/zotcli.rs +++ b/examples/zotcli.rs @@ -59,6 +59,9 @@ fn main() { .arg(Arg::with_name("raw") .long("raw") .help("Display raw json payload"))) + .subcommand( + SubCommand::with_name("abconfig") + .about("Fetch abconfig")) .subcommand( SubCommand::with_name("xchan") .about("Fetch xchan info") @@ -96,6 +99,9 @@ fn main() { let raw = m.is_present("raw"); zot::network_stream::fetch(&client, raw); }, + ("abconfig", _) => { + zot::abconfig::fetch(&client); + }, ("abook", Some(m)) => { let raw = m.is_present("raw"); zot::abook::fetch(&client, raw); -- cgit v1.2.3 From 261a6b19461089406b6cad1ddd50c3e56130da8d Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Tue, 6 Nov 2018 17:36:09 +0100 Subject: Clear out old example code. --- examples/fetch-abconfig.rs | 40 --------------------------- examples/fetch-connections.rs | 53 ------------------------------------ examples/fetch-xchan.rs | 50 ---------------------------------- examples/hubzilla-channel-stream.rs | 54 ------------------------------------- examples/hubzilla-network-stream.rs | 54 ------------------------------------- examples/hubzilla-post.rs | 47 -------------------------------- 6 files changed, 298 deletions(-) delete mode 100644 examples/fetch-abconfig.rs delete mode 100644 examples/fetch-connections.rs delete mode 100644 examples/fetch-xchan.rs delete mode 100644 examples/hubzilla-channel-stream.rs delete mode 100644 examples/hubzilla-network-stream.rs delete mode 100644 examples/hubzilla-post.rs (limited to 'examples') diff --git a/examples/fetch-abconfig.rs b/examples/fetch-abconfig.rs deleted file mode 100644 index 329d3b1..0000000 --- a/examples/fetch-abconfig.rs +++ /dev/null @@ -1,40 +0,0 @@ -// zotapi - Rust wrapper for the Zot API as implemented by Hubzilla -// Copyright (C) 2018 Harald Eilertsen -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -extern crate zotapi; -extern crate dotenv; -extern crate serde_json; - -use dotenv::dotenv; -use std::env; - -fn main() { - dotenv().ok(); - let site = env::var("HUB").expect("HUB variable expected"); - let user = env::var("HZ_USER").expect("HZ_USER variable expected"); - let password = env::var("HZ_PASSWORD").expect("HZ_PASSWORD variable expected"); - - let client = zotapi::client(&site, &user, &password); - - match client.abconfig().fetch() { - Ok(payload) => { - println!("{}", payload); - }, - Err(e) => { - println!("{:?}", e); - } - } -} diff --git a/examples/fetch-connections.rs b/examples/fetch-connections.rs deleted file mode 100644 index 9a9f974..0000000 --- a/examples/fetch-connections.rs +++ /dev/null @@ -1,53 +0,0 @@ -// zotapi - Rust wrapper for the Zot API as implemented by Hubzilla -// Copyright (C) 2018 Harald Eilertsen -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -extern crate zotapi; -extern crate dotenv; -extern crate serde_json; - -use dotenv::dotenv; -use std::env; - -fn main() { - dotenv().ok(); - let site = env::var("HUB").expect("HUB variable expected"); - let user = env::var("HZ_USER").expect("HZ_USER variable expected"); - let password = env::var("HZ_PASSWORD").expect("HZ_PASSWORD variable expected"); - - let client = zotapi::client(&site, &user, &password); - - match client.abook().fetch() { - Ok(payload) => { - let data: serde_json::Value = serde_json::from_str(&payload).unwrap(); - match data { - serde_json::Value::Array(v) => { - for contact in v { - println!("{} ({}, {})", - contact["xchan_name"], - contact["xchan_addr"], - contact["xchan_network"]); - } - }, - _ => { - println!("Unexpected data:\n{}", payload); - } - }; - }, - Err(e) => { - println!("{:?}", e); - } - } -} diff --git a/examples/fetch-xchan.rs b/examples/fetch-xchan.rs deleted file mode 100644 index 94b72ad..0000000 --- a/examples/fetch-xchan.rs +++ /dev/null @@ -1,50 +0,0 @@ -// zotapi - Rust wrapper for Sot API as implemented by Hubzilla -// Copyright (C) 2018 Harald Eilertsen -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -extern crate zotapi; -extern crate dotenv; - -use dotenv::dotenv; -use std::env; - -fn main() { - dotenv().ok(); - let site = env::var("HUB").expect("HUB variable expected"); - let user = env::var("HZ_USER").expect("HZ_USER variable expected"); - let password = env::var("HZ_PASSWORD").expect("HZ_PASSWORD variable expected"); - - let mut a = env::args(); - let program = a.next().unwrap(); - let client = zotapi::client(&site, &user, &password); - - let res = match a.next() { - Some(ref addr) => { - client.xchan().by_address(&addr).fetch() - }, - None => { - Ok(format!("Usage: {} ", program)) - } - }; - - match res { - Ok(payload) => { - println!("{}", payload); - }, - Err(e) => { - println!("{:?}", e); - } - } -} diff --git a/examples/hubzilla-channel-stream.rs b/examples/hubzilla-channel-stream.rs deleted file mode 100644 index 8045481..0000000 --- a/examples/hubzilla-channel-stream.rs +++ /dev/null @@ -1,54 +0,0 @@ -// zotapi - Rust wrapper for Sot API as implemented by Hubzilla -// Copyright (C) 2018 Harald Eilertsen -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -extern crate zotapi; -extern crate dotenv; -extern crate serde_json; - -use dotenv::dotenv; -use serde_json::Value; -use std::env; - -fn main() { - dotenv().ok(); - let site = env::var("SITE").expect("SITE variable expected"); - let user = env::var("USER").expect("USER variable expected"); - let password = env::var("PASSWORD").expect("PASSWORD variable expected"); - let client = zotapi::client(&site, &user, &password); - - match client.channel_stream() { - Ok(payload) => { - println!("Raw payload: {}", payload); - - match serde_json::from_str(&payload) { - Ok(Value::Array(v)) => { - for item in v.into_iter() { - println!("{} {} {}", item["title"], item["type"], item["author"]["name"]); - } - }, - Ok(_) => { - println!("Wrong type returned, expected an array.") - }, - Err(e) => { - println!("Error: {}", e); - } - } - }, - Err(e) => { - println!("Error getting channel stream: {:?}", e); - } - } -} diff --git a/examples/hubzilla-network-stream.rs b/examples/hubzilla-network-stream.rs deleted file mode 100644 index 104e2cb..0000000 --- a/examples/hubzilla-network-stream.rs +++ /dev/null @@ -1,54 +0,0 @@ -// zotapi - Rust wrapper for Sot API as implemented by Hubzilla -// Copyright (C) 2018 Harald Eilertsen -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -extern crate zotapi; -extern crate dotenv; -extern crate serde_json; - -use dotenv::dotenv; -use serde_json::Value; -use std::env; - -fn main() { - dotenv().ok(); - let site = env::var("SITE").expect("SITE variable expected"); - let user = env::var("USER").expect("USER variable expected"); - let password = env::var("PASSWORD").expect("PASSWORD variable expected"); - let client = zotapi::client(&site, &user, &password); - - match client.network_stream() { - Ok(payload) => { - println!("Raw payload: {}", payload); - - match serde_json::from_str(&payload) { - Ok(Value::Array(v)) => { - for item in v.into_iter() { - println!("{} {} {}", item["title"], item["type"], item["author"]["name"]); - } - }, - Ok(_) => { - println!("Wrong type returned, expected an array.") - }, - Err(e) => { - println!("Error: {}", e); - } - } - }, - Err(e) => { - println!("Error getting channel stream: {:?}", e); - } - } -} diff --git a/examples/hubzilla-post.rs b/examples/hubzilla-post.rs deleted file mode 100644 index 5eb7826..0000000 --- a/examples/hubzilla-post.rs +++ /dev/null @@ -1,47 +0,0 @@ -// zotapi - Rust wrapper for Sot API as implemented by Hubzilla -// Copyright (C) 2018 Harald Eilertsen -// -// This program is free software: you can redistribute it and/or modify -// it under the terms of the GNU General Public License as published by -// the Free Software Foundation, either version 3 of the License, or -// (at your option) any later version. -// -// This program is distributed in the hope that it will be useful, -// but WITHOUT ANY WARRANTY; without even the implied warranty of -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -// GNU General Public License for more details. -// -// You should have received a copy of the GNU General Public License -// along with this program. If not, see . - -extern crate zotapi; -extern crate dotenv; - -use dotenv::dotenv; -use std::env; - -const MIN_BODY_LENGTH : usize = 8; - -fn main() { - dotenv().ok(); - let site = env::var("SITE").expect("SITE variable expected"); - let user = env::var("USER").expect("USER variable expected"); - let password = env::var("PASSWORD").expect("PASSWORD variable expected"); - let body = env::args().skip(1).collect::>().join(" "); - if body.len() < MIN_BODY_LENGTH { - println!("Message is missing or too short. At least {} chars needed.", MIN_BODY_LENGTH); - } - - println!("Message: {}", body); - - let client = zotapi::client(&site, &user, &password); - - match client.item().body(&body).create() { - Ok(payload) => { - println!("Raw payload: {}", payload); - }, - Err(e) => { - println!("Error posting message: {:?}", e); - } - } -} -- cgit v1.2.3