Refactoring (#31)
* Refactoring code * Code formatting * More changes * Moved to icarus-models::user * Some refactoring * Updated icarus-models * Removing constants * Replaced with icarus-models constants * Formatting * Switched to constants from icarus-models: * Replaced Token with AccessToken from icarus-models * Updated icarus-models * Formatting * Removing code
This commit was merged in pull request #31.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models;
|
||||
use crate::{models, utilities};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ActionManager {
|
||||
@@ -10,6 +10,17 @@ pub struct ActionManager {
|
||||
pub param_count: i32,
|
||||
}
|
||||
|
||||
impl Default for ActionManager {
|
||||
fn default() -> Self {
|
||||
ActionManager {
|
||||
action: String::new(),
|
||||
flags: Vec::new(),
|
||||
params: Vec::new(),
|
||||
param_count: -1,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ActionManager {
|
||||
pub fn retrieve_icarus_action(&self) -> models::icarus_action::IcarusAction {
|
||||
return models::icarus_action::IcarusAction {
|
||||
@@ -45,6 +56,11 @@ impl ActionManager {
|
||||
self.action = self.action.to_lowercase();
|
||||
}
|
||||
|
||||
pub fn set_params(&mut self, args: &Vec<String>) {
|
||||
self.params = args.clone();
|
||||
self.param_count = self.params.len() as i32;
|
||||
}
|
||||
|
||||
fn validate_flags(&mut self) {
|
||||
println!("Validating flags");
|
||||
|
||||
@@ -70,7 +86,7 @@ impl ActionManager {
|
||||
flg.flag = String::from(flag);
|
||||
} else {
|
||||
println!("Flag {} is not valid", flag);
|
||||
std::process::exit(-1);
|
||||
utilities::checks::exit_program(-1);
|
||||
}
|
||||
|
||||
self.flags.push(flg);
|
||||
@@ -102,7 +118,6 @@ impl ActionManager {
|
||||
|
||||
fn does_flag_have_value(&self, flag: &String) -> bool {
|
||||
let flags_tmp = self.parsed_flags();
|
||||
|
||||
let mut i_found: i32 = -1;
|
||||
|
||||
for i in 0..flags_tmp.len() {
|
||||
|
||||
@@ -10,9 +10,9 @@ use tokio::runtime::Runtime;
|
||||
use crate::managers;
|
||||
use crate::models::song::Album;
|
||||
use crate::models::{self};
|
||||
use crate::parsers;
|
||||
use crate::syncers;
|
||||
use crate::utilities;
|
||||
use crate::{constants, parsers};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct CommitManager {
|
||||
@@ -175,6 +175,7 @@ impl CommitManager {
|
||||
|
||||
let api = prsr.retrieve_api();
|
||||
let token = self.parse_token(&api);
|
||||
println!("Message: {}", token.message);
|
||||
|
||||
let mut dwn_loader = syncers::download::Download { api: api.clone() };
|
||||
let mut song = models::song::Song::default();
|
||||
@@ -185,7 +186,7 @@ impl CommitManager {
|
||||
Ok(o) => {
|
||||
println!("Success");
|
||||
let mut filename = String::from("audio");
|
||||
filename += constants::file_extensions::WAV_FILE_EXTENSION;
|
||||
filename += icarus_models::constants::WAV_EXTENSION;
|
||||
let data = o.as_bytes();
|
||||
let mut file = std::fs::File::create(filename).expect("Failed to save");
|
||||
file.write_all(&data).expect("ff");
|
||||
@@ -233,15 +234,12 @@ impl CommitManager {
|
||||
panic!("Not supported");
|
||||
}
|
||||
|
||||
fn parse_token(&self, api: &models::api::API) -> models::token::Token {
|
||||
fn parse_token(&self, api: &models::api::API) -> icarus_models::token::AccessToken {
|
||||
println!("Fetching token");
|
||||
|
||||
let mut usr_mgr: managers::user_manager::UserManager =
|
||||
managers::user_manager::UserManager {
|
||||
user: models::user::User {
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
},
|
||||
user: icarus_models::user::User::default(),
|
||||
ica_action: self.ica_action.clone(),
|
||||
};
|
||||
usr_mgr.parse_user_from_actions();
|
||||
|
||||
@@ -3,14 +3,14 @@ use std::default::Default;
|
||||
use crate::models;
|
||||
|
||||
pub struct TokenManager {
|
||||
pub user: models::user::User,
|
||||
pub user: icarus_models::user::User,
|
||||
pub api: models::api::API,
|
||||
}
|
||||
|
||||
impl Default for TokenManager {
|
||||
fn default() -> Self {
|
||||
let mut token = TokenManager {
|
||||
user: models::user::User::default(),
|
||||
user: icarus_models::user::User::default(),
|
||||
api: models::api::API::default(),
|
||||
};
|
||||
|
||||
@@ -21,14 +21,21 @@ impl Default for TokenManager {
|
||||
}
|
||||
|
||||
impl TokenManager {
|
||||
pub async fn request_token(&self) -> Result<models::token::Token, std::io::Error> {
|
||||
pub async fn request_token(&self) -> Result<icarus_models::token::AccessToken, std::io::Error> {
|
||||
println!("Sending request for a token");
|
||||
|
||||
let url = self.retrieve_url();
|
||||
|
||||
println!("URL: {}", url);
|
||||
|
||||
let mut token = models::token::Token::default();
|
||||
let mut token = icarus_models::token::AccessToken {
|
||||
user_id: -1,
|
||||
username: String::new(),
|
||||
token: String::new(),
|
||||
token_type: String::new(),
|
||||
expiration: -1,
|
||||
message: String::new(),
|
||||
};
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let response = client.post(&url).json(&self.user).send().await.unwrap();
|
||||
@@ -36,7 +43,7 @@ impl TokenManager {
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
// on success, parse our JSON to an APIResponse
|
||||
let s = response.json::<models::token::Token>().await;
|
||||
let s = response.json::<icarus_models::token::AccessToken>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
@@ -66,7 +73,6 @@ impl TokenManager {
|
||||
let api = &self.api;
|
||||
let mut url = String::from(&api.url);
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -6,36 +6,42 @@ use crate::models::{self};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct UserManager {
|
||||
pub user: models::user::User,
|
||||
pub user: icarus_models::user::User,
|
||||
pub ica_action: models::icarus_action::IcarusAction,
|
||||
}
|
||||
|
||||
impl Default for UserManager {
|
||||
fn default() -> Self {
|
||||
UserManager {
|
||||
user: models::user::User::default(),
|
||||
user: icarus_models::user::User::default(),
|
||||
ica_action: models::icarus_action::IcarusAction::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserManager {
|
||||
pub fn retrieve_user(&self) -> models::user::User {
|
||||
pub fn retrieve_user(&self) -> icarus_models::user::User {
|
||||
return self.user.clone();
|
||||
}
|
||||
|
||||
pub fn parse_user_from_actions(&mut self) {
|
||||
let args = &self.ica_action.flags;
|
||||
|
||||
// Quit the loop when two are found
|
||||
let mut amt: i32 = 0;
|
||||
for arg in args {
|
||||
let flag = &arg.flag;
|
||||
|
||||
if flag == "-u" {
|
||||
self.user.username = String::from(&arg.value);
|
||||
amt += 1;
|
||||
} else if flag == "-p" {
|
||||
self.user.password = String::from(&arg.value);
|
||||
amt += 1;
|
||||
}
|
||||
|
||||
if flag == "-p" {
|
||||
self.user.password = String::from(&arg.value);
|
||||
if amt == 2 {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user