diff options
author | Harald Eilertsen <haraldei@anduin.net> | 2018-11-06 17:37:00 +0100 |
---|---|---|
committer | Harald Eilertsen <haraldei@anduin.net> | 2018-11-06 17:37:00 +0100 |
commit | 63e1491ff66d428ccacff63ca2db2050076c9a0e (patch) | |
tree | e96043208598fcfef333dd0e0ef76995de3707f3 | |
parent | 84fea67f78f66c38066ba3d7b248cc502f55c85b (diff) | |
parent | 261a6b19461089406b6cad1ddd50c3e56130da8d (diff) | |
download | rust-zotapi-63e1491ff66d428ccacff63ca2db2050076c9a0e.tar.gz rust-zotapi-63e1491ff66d428ccacff63ca2db2050076c9a0e.tar.bz2 rust-zotapi-63e1491ff66d428ccacff63ca2db2050076c9a0e.zip |
Merge branch 'rewrite-examples'
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/fetch-abconfig.rs | 40 | ||||
-rw-r--r-- | examples/fetch-connections.rs | 53 | ||||
-rw-r--r-- | examples/fetch-xchan.rs | 50 | ||||
-rw-r--r-- | examples/hubzilla-channel-stream.rs | 54 | ||||
-rw-r--r-- | examples/hubzilla-network-stream.rs | 54 | ||||
-rw-r--r-- | examples/hubzilla-post.rs | 47 | ||||
-rw-r--r-- | examples/zot/abconfig.rs | 29 | ||||
-rw-r--r-- | examples/zot/abook.rs | 52 | ||||
-rw-r--r-- | examples/zot/channel_stream.rs | 109 | ||||
-rw-r--r-- | examples/zot/item.rs | 29 | ||||
-rw-r--r-- | examples/zot/mod.rs | 6 | ||||
-rw-r--r-- | examples/zot/network_stream.rs | 109 | ||||
-rw-r--r-- | examples/zot/xchan.rs | 41 | ||||
-rw-r--r-- | examples/zotcli.rs | 131 | ||||
-rw-r--r-- | src/lib.rs | 2 |
16 files changed, 508 insertions, 299 deletions
@@ -25,6 +25,7 @@ serde = "1.0" serde_urlencoded = "0.5.1" [dev-dependencies] +clap = "2.32.0" dotenv = "0.13" mockito = "0.12" serde_json = "1.0" 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 <haraldei@anduin.net> -// -// 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 <https://www.gnu.org/licenses/>. - -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 <haraldei@anduin.net> -// -// 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 <https://www.gnu.org/licenses/>. - -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 <haraldei@anduin.net> -// -// 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 <https://www.gnu.org/licenses/>. - -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: {} <addr>", 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 <haraldei@anduin.net> -// -// 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 <https://www.gnu.org/licenses/>. - -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 <haraldei@anduin.net> -// -// 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 <https://www.gnu.org/licenses/>. - -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 <haraldei@anduin.net> -// -// 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 <https://www.gnu.org/licenses/>. - -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::<Vec<_>>().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); - } - } -} 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 <haraldei@anduin.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +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/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 <haraldei@anduin.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +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 <haraldei@anduin.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +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::<Vec<_>>().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/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 <haraldei@anduin.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +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 new file mode 100644 index 0000000..3bb3c93 --- /dev/null +++ b/examples/zot/mod.rs @@ -0,0 +1,6 @@ +pub mod abconfig; +pub mod abook; +pub mod channel_stream; +pub mod item; +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 <haraldei@anduin.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +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::<Vec<_>>().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 <name of copyright holder> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +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..672e3ef --- /dev/null +++ b/examples/zotcli.rs @@ -0,0 +1,131 @@ +/* Example Zot API command line utility, part of zotapi. + * Copyright (C) 2018 Harald Eilertsen <haraldei@anduin.net> + * + * 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 <http://www.gnu.org/licenses/>. + */ + +extern crate zotapi; +extern crate dotenv; +extern crate clap; +extern crate serde_json; + +use clap::{ + Arg, + app_from_crate, + crate_name, + crate_version, + crate_authors, + crate_description, + 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("abconfig") + .about("Fetch abconfig")) + .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))) + .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); + + 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); + }, + ("abconfig", _) => { + zot::abconfig::fetch(&client); + }, + ("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()); + }, + ("post", Some(m)) => { + let body = m.value_of("BODY").unwrap(); + zot::item::post(&client, body); + }, + _ => { + println!("{}", matches.usage()); + }, + } +} @@ -25,7 +25,7 @@ mod error; mod item; mod xchan; -use client::Client; +pub use client::Client; pub fn client(url: &str, user: &str, pw: &str) -> Client { |