Added more code to the managers module
This commit is contained in:
@@ -2,7 +2,9 @@ use std::collections::HashMap;
|
||||
use std::default::Default;
|
||||
use std::fs::File;
|
||||
use std::io::{Read, Error};
|
||||
use tokio::runtime::Runtime;
|
||||
|
||||
use futures::{FutureExt, TryFutureExt};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{exit_program, managers};
|
||||
@@ -184,7 +186,10 @@ impl CommitManager {
|
||||
api: api.clone(),
|
||||
};
|
||||
|
||||
return tok_mgr.request_token();
|
||||
// let token = tok_mgr.request_token();
|
||||
let token = Runtime::new().unwrap().block_on(tok_mgr.request_token());
|
||||
|
||||
return token.unwrap();
|
||||
}
|
||||
// TODO: Implement
|
||||
fn upload_song_with_metadata(&self) {
|
||||
|
||||
@@ -7,18 +7,78 @@ pub struct TokenManager {
|
||||
pub api: models::api::API,
|
||||
}
|
||||
|
||||
impl Default for TokenManager {
|
||||
fn default() -> Self {
|
||||
let mut token = TokenManager {
|
||||
user: models::user::User::default(),
|
||||
api: models::api::API::default(),
|
||||
};
|
||||
|
||||
token.init();
|
||||
|
||||
return token;
|
||||
}
|
||||
}
|
||||
|
||||
impl TokenManager {
|
||||
pub fn request_token(&self) -> models::token::Token {
|
||||
let usr_json = self.user.to_json();
|
||||
pub async fn request_token(&self) -> Result<models::token::Token, std::io::Error> {
|
||||
println!("Sending request for a token");
|
||||
|
||||
let endpoint = self.construct_endpoint();
|
||||
let mut url: String = String::from(&self.api.url);
|
||||
url += &endpoint;
|
||||
let mut url = self.retrieve_url();
|
||||
// url += &endpoint;
|
||||
|
||||
println!("URL: {}", url);
|
||||
|
||||
return models::token::Token::default();
|
||||
let mut token = models::token::Token::default();
|
||||
|
||||
|
||||
let client = reqwest::Client::new();
|
||||
let response = client.post(&url)
|
||||
.json(&self.user)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
// on success, parse our JSON to an APIResponse
|
||||
let s = response.json::<models::token::Token>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
token = parsed;
|
||||
},
|
||||
Err(_) => println!("Hm, the response didn't match the shape we expected."),
|
||||
};
|
||||
}
|
||||
reqwest::StatusCode::UNAUTHORIZED => {
|
||||
println!("Need to grab a new token");
|
||||
}
|
||||
other => {
|
||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(token);
|
||||
}
|
||||
|
||||
pub fn init(&mut self) {
|
||||
let mut api = &mut self.api;
|
||||
api.endpoint = String::from("api/v1/login");
|
||||
api.version = String::from("v1");
|
||||
}
|
||||
|
||||
pub fn retrieve_url(&self) -> String {
|
||||
let api = &self.api;
|
||||
let mut url = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
fn construct_endpoint(&self) -> String {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use std::default::Default;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models;
|
||||
use crate::models::{self, user::User};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct UserManager {
|
||||
@@ -8,6 +10,15 @@ pub struct UserManager {
|
||||
pub ica_action: models::icarus_action::IcarusAction,
|
||||
}
|
||||
|
||||
impl Default for UserManager {
|
||||
fn default() -> Self {
|
||||
UserManager {
|
||||
user: models::user::User::default(),
|
||||
ica_action: models::icarus_action::IcarusAction::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UserManager {
|
||||
pub fn retrieve_user(&self) -> models::user::User {
|
||||
return self.user.clone();
|
||||
|
||||
Reference in New Issue
Block a user