Token fetch (#56)
* Added print statement and TODO * Refactoring token fetching code Adding two APIs. One for auth and the other for the main app * Fixed token code * Code formatting * Cleanup
This commit was merged in pull request #56.
This commit is contained in:
@@ -35,6 +35,7 @@ impl ActionManager {
|
|||||||
String::from("-p"),
|
String::from("-p"),
|
||||||
String::from("-t"),
|
String::from("-t"),
|
||||||
String::from("-h"),
|
String::from("-h"),
|
||||||
|
String::from("-ha"),
|
||||||
String::from("-s"),
|
String::from("-s"),
|
||||||
String::from("-sd"),
|
String::from("-sd"),
|
||||||
String::from("-sr"),
|
String::from("-sr"),
|
||||||
@@ -75,6 +76,7 @@ impl ActionManager {
|
|||||||
|
|
||||||
let mut flg = models::flags::Flags::default();
|
let mut flg = models::flags::Flags::default();
|
||||||
|
|
||||||
|
// TODO: Refactor this
|
||||||
if self.is_valid_flag(flag) && self.does_flag_have_value(flag) {
|
if self.is_valid_flag(flag) && self.does_flag_have_value(flag) {
|
||||||
println!("Flag has value");
|
println!("Flag has value");
|
||||||
flg.flag = String::from(flag);
|
flg.flag = String::from(flag);
|
||||||
|
|||||||
@@ -88,6 +88,8 @@ impl CommitManager {
|
|||||||
|
|
||||||
println!("{mapped_action:?}");
|
println!("{mapped_action:?}");
|
||||||
|
|
||||||
|
// TODO: Move code to get token here and then pass it to the respective functions
|
||||||
|
|
||||||
match mapped_action {
|
match mapped_action {
|
||||||
ActionValues::DeleteAct => self.delete_song(),
|
ActionValues::DeleteAct => self.delete_song(),
|
||||||
ActionValues::DownloadAct => self.download_song(),
|
ActionValues::DownloadAct => self.download_song(),
|
||||||
@@ -132,13 +134,14 @@ impl CommitManager {
|
|||||||
|
|
||||||
fn delete_song(&self) {
|
fn delete_song(&self) {
|
||||||
let mut prsr = parsers::api_parser::APIParser {
|
let mut prsr = parsers::api_parser::APIParser {
|
||||||
|
apis: vec![models::api::Api::default(), models::api::Api::default()],
|
||||||
ica_act: self.ica_action.clone(),
|
ica_act: self.ica_action.clone(),
|
||||||
api: models::api::Api::default(),
|
|
||||||
};
|
};
|
||||||
prsr.parse_api();
|
prsr.parse_api(parsers::api_parser::APIType::Main);
|
||||||
let api = prsr.retrieve_api();
|
prsr.parse_api(parsers::api_parser::APIType::Auth);
|
||||||
|
let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth);
|
||||||
|
|
||||||
let token = self.parse_token(&api);
|
let token = self.parse_token(&auth_api);
|
||||||
|
|
||||||
println!("Deleting song");
|
println!("Deleting song");
|
||||||
|
|
||||||
@@ -153,6 +156,7 @@ impl CommitManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let api = prsr.retrieve_api(parsers::api_parser::APIType::Main);
|
||||||
let mut del = syncers::delete::Delete { api: api.clone() };
|
let mut del = syncers::delete::Delete { api: api.clone() };
|
||||||
|
|
||||||
println!("Deleting song..");
|
println!("Deleting song..");
|
||||||
@@ -175,14 +179,16 @@ impl CommitManager {
|
|||||||
let song_id = uuid::Uuid::from_str(dwn.as_str()).unwrap();
|
let song_id = uuid::Uuid::from_str(dwn.as_str()).unwrap();
|
||||||
|
|
||||||
let mut prsr = parsers::api_parser::APIParser {
|
let mut prsr = parsers::api_parser::APIParser {
|
||||||
api: models::api::Api::default(),
|
apis: vec![models::api::Api::default(), models::api::Api::default()],
|
||||||
ica_act: self.ica_action.clone(),
|
ica_act: self.ica_action.clone(),
|
||||||
};
|
};
|
||||||
prsr.parse_api();
|
prsr.parse_api(parsers::api_parser::APIType::Main);
|
||||||
|
prsr.parse_api(parsers::api_parser::APIType::Auth);
|
||||||
|
|
||||||
let api = prsr.retrieve_api();
|
let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth);
|
||||||
let token = self.parse_token(&api);
|
let token = self.parse_token(&auth_api);
|
||||||
println!("Message: {}", token.message);
|
println!("Message: {}", token.message);
|
||||||
|
let api = prsr.retrieve_api(parsers::api_parser::APIType::Main);
|
||||||
|
|
||||||
let mut dwn_loader = syncers::download::Download { api: api.clone() };
|
let mut dwn_loader = syncers::download::Download { api: api.clone() };
|
||||||
let song = icarus_models::song::Song {
|
let song = icarus_models::song::Song {
|
||||||
@@ -223,13 +229,18 @@ impl CommitManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let mut prsr = parsers::api_parser::APIParser {
|
let mut prsr = parsers::api_parser::APIParser {
|
||||||
api: models::api::Api::default(),
|
apis: vec![models::api::Api::default(), models::api::Api::default()],
|
||||||
ica_act: self.ica_action.clone(),
|
ica_act: self.ica_action.clone(),
|
||||||
};
|
};
|
||||||
prsr.parse_api();
|
prsr.parse_api(parsers::api_parser::APIType::Main);
|
||||||
|
prsr.parse_api(parsers::api_parser::APIType::Auth);
|
||||||
|
|
||||||
|
let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth);
|
||||||
|
let token = self.parse_token(&auth_api);
|
||||||
|
println!("Token {token:?}");
|
||||||
|
|
||||||
|
let api = prsr.retrieve_api(parsers::api_parser::APIType::Main);
|
||||||
|
|
||||||
let api = prsr.retrieve_api();
|
|
||||||
let token = self.parse_token(&api);
|
|
||||||
let mut repo = syncers::retrieve_records::RetrieveRecords { api: api.clone() };
|
let mut repo = syncers::retrieve_records::RetrieveRecords { api: api.clone() };
|
||||||
let result_fut = repo.get_all_songs(&token);
|
let result_fut = repo.get_all_songs(&token);
|
||||||
|
|
||||||
@@ -248,6 +259,7 @@ impl CommitManager {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: Remove at some point
|
||||||
fn upload_song(&self) {
|
fn upload_song(&self) {
|
||||||
println!("Deleting song");
|
println!("Deleting song");
|
||||||
panic!("Not supported");
|
panic!("Not supported");
|
||||||
@@ -318,13 +330,14 @@ impl CommitManager {
|
|||||||
cover_path: &str,
|
cover_path: &str,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
let mut prsr = parsers::api_parser::APIParser {
|
let mut prsr = parsers::api_parser::APIParser {
|
||||||
api: models::api::Api::default(),
|
apis: vec![models::api::Api::default(), models::api::Api::default()],
|
||||||
ica_act: self.ica_action.clone(),
|
ica_act: self.ica_action.clone(),
|
||||||
};
|
};
|
||||||
prsr.parse_api();
|
prsr.parse_api(parsers::api_parser::APIType::Main);
|
||||||
|
prsr.parse_api(parsers::api_parser::APIType::Auth);
|
||||||
|
|
||||||
let api = prsr.retrieve_api();
|
let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth);
|
||||||
let token = self.parse_token(&api);
|
let token = self.parse_token(&auth_api);
|
||||||
|
|
||||||
println!("Token: {:?}", token.token);
|
println!("Token: {:?}", token.token);
|
||||||
|
|
||||||
@@ -444,12 +457,13 @@ impl CommitManager {
|
|||||||
|
|
||||||
fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
||||||
let mut prsr = parsers::api_parser::APIParser {
|
let mut prsr = parsers::api_parser::APIParser {
|
||||||
api: models::api::Api::default(),
|
apis: vec![models::api::Api::default(), models::api::Api::default()],
|
||||||
ica_act: self.ica_action.clone(),
|
ica_act: self.ica_action.clone(),
|
||||||
};
|
};
|
||||||
prsr.parse_api();
|
prsr.parse_api(parsers::api_parser::APIType::Main);
|
||||||
let api = prsr.retrieve_api();
|
prsr.parse_api(parsers::api_parser::APIType::Auth);
|
||||||
let token = self.parse_token(&api);
|
let auth_api = prsr.retrieve_api(parsers::api_parser::APIType::Auth);
|
||||||
|
let token = self.parse_token(&auth_api);
|
||||||
|
|
||||||
let directory_path = std::path::Path::new(&sourcepath);
|
let directory_path = std::path::Path::new(&sourcepath);
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,16 @@ use std::default::Default;
|
|||||||
|
|
||||||
use crate::models;
|
use crate::models;
|
||||||
|
|
||||||
|
mod response {
|
||||||
|
pub mod token {
|
||||||
|
#[derive(Debug, serde::Deserialize, serde::Serialize)]
|
||||||
|
pub struct Response {
|
||||||
|
pub message: String,
|
||||||
|
pub data: Vec<icarus_models::login_result::LoginResult>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct TokenManager {
|
pub struct TokenManager {
|
||||||
pub user: icarus_models::user::User,
|
pub user: icarus_models::user::User,
|
||||||
pub api: models::api::Api,
|
pub api: models::api::Api,
|
||||||
@@ -43,9 +53,15 @@ impl TokenManager {
|
|||||||
match response.status() {
|
match response.status() {
|
||||||
reqwest::StatusCode::OK => {
|
reqwest::StatusCode::OK => {
|
||||||
// on success, parse our JSON to an APIResponse
|
// on success, parse our JSON to an APIResponse
|
||||||
match response.json::<icarus_models::token::AccessToken>().await {
|
match response.json::<response::token::Response>().await {
|
||||||
Ok(parsed) => {
|
Ok(response) => {
|
||||||
token = parsed;
|
let login_result = &response.data[0];
|
||||||
|
token.user_id = login_result.id;
|
||||||
|
token.username = login_result.username.clone();
|
||||||
|
token.token = login_result.token.clone();
|
||||||
|
token.token_type = login_result.token_type.clone();
|
||||||
|
token.expiration = login_result.expiration;
|
||||||
|
token.message = response.message;
|
||||||
}
|
}
|
||||||
Err(_) => println!("Hm, the response didn't match the shape we expected."),
|
Err(_) => println!("Hm, the response didn't match the shape we expected."),
|
||||||
};
|
};
|
||||||
@@ -63,7 +79,7 @@ impl TokenManager {
|
|||||||
|
|
||||||
pub fn init(&mut self) {
|
pub fn init(&mut self) {
|
||||||
let api = &mut self.api;
|
let api = &mut self.api;
|
||||||
api.version = String::from("v1");
|
api.version = String::from(crate::parsers::api_parser::API_VERSION);
|
||||||
api.endpoint = format!("api/{}/login", api.version);
|
api.endpoint = format!("api/{}/login", api.version);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,16 +2,26 @@ use crate::models;
|
|||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct APIParser {
|
pub struct APIParser {
|
||||||
pub api: models::api::Api,
|
pub apis: Vec<models::api::Api>,
|
||||||
pub ica_act: models::icarus_action::IcarusAction,
|
pub ica_act: models::icarus_action::IcarusAction,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub const API_VERSION: &str = "v2";
|
||||||
|
|
||||||
|
pub enum APIType {
|
||||||
|
Main,
|
||||||
|
Auth,
|
||||||
|
}
|
||||||
|
|
||||||
impl APIParser {
|
impl APIParser {
|
||||||
pub fn retrieve_api(&self) -> models::api::Api {
|
pub fn retrieve_api(&self, api_type: APIType) -> models::api::Api {
|
||||||
self.api.clone()
|
match api_type {
|
||||||
|
APIType::Main => self.apis[0].clone(),
|
||||||
|
APIType::Auth => self.apis[1].clone(),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn parse_api(&mut self) {
|
pub fn parse_api(&mut self, api_type: APIType) {
|
||||||
let flags = self.ica_act.flags.clone();
|
let flags = self.ica_act.flags.clone();
|
||||||
println!("Parsing api");
|
println!("Parsing api");
|
||||||
|
|
||||||
@@ -19,16 +29,32 @@ impl APIParser {
|
|||||||
let arg = elem.flag;
|
let arg = elem.flag;
|
||||||
let value = elem.value;
|
let value = elem.value;
|
||||||
|
|
||||||
|
match api_type {
|
||||||
|
APIType::Main => {
|
||||||
if arg == "-h" {
|
if arg == "-h" {
|
||||||
if value.chars().nth(value.len() - 1) == Some('/') {
|
if value.chars().nth(value.len() - 1) == Some('/') {
|
||||||
self.api.url = value;
|
self.apis[0].url = value;
|
||||||
} else {
|
} else {
|
||||||
self.api.url = value + "/";
|
self.apis[0].url = format!("{value}/");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
APIType::Auth => {
|
||||||
|
if arg == "-ha" {
|
||||||
|
if value.chars().nth(value.len() - 1) == Some('/') {
|
||||||
|
self.apis[1].url = value;
|
||||||
|
} else {
|
||||||
|
self.apis[1].url = format!("{value}/");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
self.api.version = "v1".to_string();
|
for api in &mut self.apis {
|
||||||
|
api.version = String::from(API_VERSION);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user