Add sharetoken getting

This commit is contained in:
Sofia 2020-08-25 22:18:59 +03:00
parent c3cf61efd4
commit 2af103667a
6 changed files with 71 additions and 1 deletions

View File

@ -33,6 +33,14 @@ impl API {
.to_owned()) .to_owned())
} }
pub fn get_sharetoken(&self, tag_id: &String) -> Result<SharetokenModel, GenericError> {
let response = self.request(API::api_url(
APIUrl::Sharetoken(tag_id.clone()),
&self.config,
))?;
Ok(response.json()?)
}
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();
@ -220,6 +228,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),
} }
} }
@ -268,4 +277,5 @@ pub enum APIUrl {
Locations(String, String), Locations(String, String),
States(String), States(String),
Tags, Tags,
Sharetoken(String),
} }

View File

@ -12,6 +12,39 @@ pub struct ErrorModel {
pub message: Option<String>, pub message: Option<String>,
} }
#[derive(Deserialize, Debug)]
#[serde(deny_unknown_fields)]
pub struct SharetokenModel {
token: Option<String>,
url: Option<String>,
expires: Option<String>,
}
impl Timestamped for SharetokenModel {
fn timestamp(&self) -> Option<String> {
self.expires.clone()
}
}
impl Display for SharetokenModel {
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
let mut text = String::new();
text += &format!(
" token = {}",
self.token.as_ref().unwrap_or(&"unknown".to_owned())
);
text += &format!(
"\n url = {}",
self.url.as_ref().unwrap_or(&"unknown".to_owned())
);
text += &format!(
"\n expiry time = {}",
&self.expires.as_ref().unwrap_or(&"unknown".to_owned())
);
write!(f, "{}", text)
}
}
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)] #[serde(deny_unknown_fields)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]

View File

@ -21,6 +21,7 @@ pub enum Subcommand {
Between(BetweenOpt), Between(BetweenOpt),
Init(InitOpt), Init(InitOpt),
Api(ApiOpt), Api(ApiOpt),
ShareToken(ShareTokenOpt),
Nick(NickOpt), Nick(NickOpt),
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
Get(GetOpt), Get(GetOpt),
@ -68,6 +69,17 @@ pub struct InitOpt {
)] )]
pub struct ApiOpt {} pub struct ApiOpt {}
#[derive(FromArgs)]
#[argh(
subcommand,
name = "sharetoken",
description = "retrieve share token for given device."
)]
pub struct ShareTokenOpt {
#[argh(positional, description = "the device index or nickname")]
pub device: String,
}
#[derive(FromArgs)] #[derive(FromArgs)]
#[argh( #[argh(
subcommand, subcommand,

View File

@ -1,4 +1,4 @@
use super::api::{LocationModel, StateModel, TagModel, API}; use super::api::{LocationModel, SharetokenModel, StateModel, TagModel, API};
use super::config::Config; use super::config::Config;
use super::errors::GenericError; use super::errors::GenericError;
use super::gpx; use super::gpx;
@ -84,6 +84,7 @@ pub fn check_api(api: &API) -> Result<(), GenericError> {
let tag_id = tag.get_id()?; let tag_id = tag.get_id()?;
api.get_current_locations(&tag_id)?; api.get_current_locations(&tag_id)?;
api.get_sharetoken(&tag_id)?;
if let Some(state) = api.get_states(&tag_id)?.get(0) { if let Some(state) = api.get_states(&tag_id)?.get(0) {
let state_id = state.get_id()?; let state_id = state.get_id()?;
@ -100,6 +101,12 @@ pub fn check_api(api: &API) -> Result<(), GenericError> {
} }
} }
pub fn sharetoken(api: &API, tag: String) -> Result<SharetokenModel, GenericError> {
let tags = api.get_tags()?;
let tag = find_tag(tag, &tags, &api.config)?;
Ok(api.get_sharetoken(&tag.get_id()?)?)
}
pub fn nick_list(api: &API) -> Result<Vec<((usize, String), TagModel)>, GenericError> { pub fn nick_list(api: &API) -> Result<Vec<((usize, String), TagModel)>, GenericError> {
let mut tags = api.get_tags()?; let mut tags = api.get_tags()?;
tags.sort_by(|tag1, tag2| tag1.id.cmp(&tag2.id)); tags.sort_by(|tag1, tag2| tag1.id.cmp(&tag2.id));

View File

@ -13,6 +13,7 @@ pub struct Config {
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,
pub sharetoken_url: String,
pub timestamp_format: String, pub timestamp_format: String,
pub timedate_format: String, pub timedate_format: String,
@ -52,6 +53,7 @@ impl Default for Config {
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(),
sharetoken_url: "https://platform.yepzon.com/tags/{tag}/sharetoken".to_owned(),
timestamp_format: "%Y-%m-%dT%H:%M:%S%.fZ".to_owned(), timestamp_format: "%Y-%m-%dT%H:%M:%S%.fZ".to_owned(),

View File

@ -61,6 +61,12 @@ fn from_env(env: EnvOpt) -> Result<(), GenericError> {
)), )),
} }
} }
Subcommand::ShareToken(opt) => {
let config = Config::from_path(&env.config)?;
let api = API::new(config.clone());
println!("{}", commands::sharetoken(&api, opt.device)?);
Ok(())
}
Subcommand::Nick(opt) => { Subcommand::Nick(opt) => {
let config = Config::from_path(&env.config)?; let config = Config::from_path(&env.config)?;
let mut api = API::new(config.clone()); let mut api = API::new(config.clone());