Add get to specific tag

This commit is contained in:
Sofia 2020-08-25 22:30:13 +03:00
parent 2af103667a
commit 2b835a72b0
5 changed files with 31 additions and 0 deletions

View File

@ -41,6 +41,7 @@ impl API {
Ok(response.json()?) Ok(response.json()?)
} }
#[allow(dead_code)]
pub fn get_tags(&self) -> Result<Vec<TagModel>, GenericError> { pub fn get_tags(&self) -> Result<Vec<TagModel>, GenericError> {
let response = self.request(API::api_url(APIUrl::Tags, &self.config))?; let response = self.request(API::api_url(APIUrl::Tags, &self.config))?;
let tags = response.json(); let tags = response.json();
@ -52,6 +53,12 @@ impl API {
} }
} }
#[allow(dead_code)]
pub fn get_tag(&self, tag_id: &String) -> Result<TagModel, GenericError> {
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<Vec<StateModel>, GenericError> { pub fn get_states(&self, tag_id: &String) -> Result<Vec<StateModel>, GenericError> {
let response = self.request(API::api_url(APIUrl::States(tag_id.clone()), &self.config))?; let response = self.request(API::api_url(APIUrl::States(tag_id.clone()), &self.config))?;
Ok(response.json()?) Ok(response.json()?)
@ -202,6 +209,7 @@ impl API {
timestamped timestamped
} }
#[allow(dead_code)]
pub fn print_list<T: Display + Timestamped>(list: Result<Vec<T>, GenericError>) { pub fn print_list<T: Display + Timestamped>(list: Result<Vec<T>, GenericError>) {
match list { match list {
Ok(items) => { Ok(items) => {
@ -229,6 +237,7 @@ impl API {
APIUrl::States(tag) => str::replace(&config.states_url, "{tag}", &tag), APIUrl::States(tag) => str::replace(&config.states_url, "{tag}", &tag),
APIUrl::Tags => config.tags_url.clone(), APIUrl::Tags => config.tags_url.clone(),
APIUrl::Sharetoken(tag) => str::replace(&config.sharetoken_url, "{tag}", &tag), 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), Locations(String, String),
States(String), States(String),
Tags, Tags,
#[allow(dead_code)]
Tag(String),
Sharetoken(String), Sharetoken(String),
} }

View File

@ -133,11 +133,23 @@ pub struct GetOpt {
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh(subcommand)] #[argh(subcommand)]
pub enum GetSub { pub enum GetSub {
Tag(GetTagOpt),
Tags(GetTagsOpt), Tags(GetTagsOpt),
States(GetStatesOpt), States(GetStatesOpt),
Locations(GetLocationsOpt), 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)] #[derive(FromArgs)]
#[argh( #[argh(
subcommand, subcommand,

View File

@ -148,6 +148,7 @@ pub fn nick_set(
} }
} }
#[allow(dead_code)]
pub fn get_locations( pub fn get_locations(
api: &API, api: &API,
tag: String, tag: String,
@ -162,6 +163,7 @@ pub fn get_locations(
} }
} }
#[allow(dead_code)]
pub fn get_states(api: &API, tag: String) -> Result<Vec<StateModel>, GenericError> { pub fn get_states(api: &API, tag: String) -> Result<Vec<StateModel>, GenericError> {
let tags = api.get_tags()?; let tags = api.get_tags()?;
let tag = find_tag(tag, &tags, &api.config)?; let tag = find_tag(tag, &tags, &api.config)?;

View File

@ -10,6 +10,7 @@ pub struct Config {
pub api_key: String, pub api_key: String,
pub tags_url: String, pub tags_url: String,
pub tag_url: String,
pub states_url: String, pub states_url: String,
pub locations_url: String, pub locations_url: String,
pub current_locations_url: String, pub current_locations_url: String,
@ -50,6 +51,7 @@ impl Default for Config {
api_key: "<your API key here from https://developers.yepzon.com/>".to_owned(), api_key: "<your API key here from https://developers.yepzon.com/>".to_owned(),
tags_url: "https://platform.yepzon.com/tags".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(), states_url: "https://platform.yepzon.com/tags/{tag}/states".to_owned(),
locations_url: "https://platform.yepzon.com/tags/{tag}/locations/{state}".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(), current_locations_url: "https://platform.yepzon.com/tags/{tag}/locations".to_owned(),

View File

@ -89,6 +89,10 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
let config = Config::from_path(&env.config)?; let config = Config::from_path(&env.config)?;
let api = API::new(config.clone()); let api = API::new(config.clone());
match opt.subcommand { match opt.subcommand {
GetSub::Tag(opt) => {
let tags = api.get_tag(&opt.device);
dbg!(&tags);
}
GetSub::Tags(_) => { GetSub::Tags(_) => {
let tags = api.get_tags()?; let tags = api.get_tags()?;
dbg!(&tags); dbg!(&tags);