From 2b835a72b021671472d1835a40bbcaf2d98940b0 Mon Sep 17 00:00:00 2001 From: Teascade Date: Tue, 25 Aug 2020 22:30:13 +0300 Subject: [PATCH] Add get to specific tag --- src/api/mod.rs | 11 +++++++++++ src/args.rs | 12 ++++++++++++ src/commands.rs | 2 ++ src/config.rs | 2 ++ src/main.rs | 4 ++++ 5 files changed, 31 insertions(+) diff --git a/src/api/mod.rs b/src/api/mod.rs index e047098..5a08bf5 100644 --- a/src/api/mod.rs +++ b/src/api/mod.rs @@ -41,6 +41,7 @@ impl API { Ok(response.json()?) } + #[allow(dead_code)] pub fn get_tags(&self) -> Result, GenericError> { let response = self.request(API::api_url(APIUrl::Tags, &self.config))?; let tags = response.json(); @@ -52,6 +53,12 @@ impl API { } } + #[allow(dead_code)] + pub fn get_tag(&self, tag_id: &String) -> Result { + let response = self.request(API::api_url(APIUrl::Tag(tag_id.clone()), &self.config))?; + Ok(response.json()?) + } + pub fn get_states(&self, tag_id: &String) -> Result, GenericError> { let response = self.request(API::api_url(APIUrl::States(tag_id.clone()), &self.config))?; Ok(response.json()?) @@ -202,6 +209,7 @@ impl API { timestamped } + #[allow(dead_code)] pub fn print_list(list: Result, GenericError>) { match list { Ok(items) => { @@ -229,6 +237,7 @@ impl API { APIUrl::States(tag) => str::replace(&config.states_url, "{tag}", &tag), APIUrl::Tags => config.tags_url.clone(), APIUrl::Sharetoken(tag) => str::replace(&config.sharetoken_url, "{tag}", &tag), + APIUrl::Tag(tag) => str::replace(&config.tag_url, "{tag}", &tag), } } @@ -277,5 +286,7 @@ pub enum APIUrl { Locations(String, String), States(String), Tags, + #[allow(dead_code)] + Tag(String), Sharetoken(String), } diff --git a/src/args.rs b/src/args.rs index ff8c4a1..a3c08c9 100644 --- a/src/args.rs +++ b/src/args.rs @@ -133,11 +133,23 @@ pub struct GetOpt { #[derive(FromArgs)] #[argh(subcommand)] pub enum GetSub { + Tag(GetTagOpt), Tags(GetTagsOpt), States(GetStatesOpt), Locations(GetLocationsOpt), } +#[derive(FromArgs)] +#[argh( + subcommand, + name = "tag", + description = "Get a specific tag connected to this API-key" +)] +pub struct GetTagOpt { + #[argh(positional, description = "the device in question")] + pub device: String, +} + #[derive(FromArgs)] #[argh( subcommand, diff --git a/src/commands.rs b/src/commands.rs index f292383..75d0756 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -148,6 +148,7 @@ pub fn nick_set( } } +#[allow(dead_code)] pub fn get_locations( api: &API, tag: String, @@ -162,6 +163,7 @@ pub fn get_locations( } } +#[allow(dead_code)] pub fn get_states(api: &API, tag: String) -> Result, GenericError> { let tags = api.get_tags()?; let tag = find_tag(tag, &tags, &api.config)?; diff --git a/src/config.rs b/src/config.rs index 503989e..a222c37 100644 --- a/src/config.rs +++ b/src/config.rs @@ -10,6 +10,7 @@ pub struct Config { pub api_key: String, pub tags_url: String, + pub tag_url: String, pub states_url: String, pub locations_url: String, pub current_locations_url: String, @@ -50,6 +51,7 @@ impl Default for Config { api_key: "".to_owned(), tags_url: "https://platform.yepzon.com/tags".to_owned(), + tag_url: "https://platform.yepzon.com/tags/{tag}".to_owned(), states_url: "https://platform.yepzon.com/tags/{tag}/states".to_owned(), locations_url: "https://platform.yepzon.com/tags/{tag}/locations/{state}".to_owned(), current_locations_url: "https://platform.yepzon.com/tags/{tag}/locations".to_owned(), diff --git a/src/main.rs b/src/main.rs index be628b7..3114c78 100644 --- a/src/main.rs +++ b/src/main.rs @@ -89,6 +89,10 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> { let config = Config::from_path(&env.config)?; let api = API::new(config.clone()); match opt.subcommand { + GetSub::Tag(opt) => { + let tags = api.get_tag(&opt.device); + dbg!(&tags); + } GetSub::Tags(_) => { let tags = api.get_tags()?; dbg!(&tags);