Files
icarus_dm/src/managers/token_manager.rs
T
Kun Deng ee4469b385 Language Migration (#21)
* Blank slate

* Added gitignore

* Updated gitignore

* Added rust code

* Added modules

* Fixed build issues

* rustfmt

* Updated models

* Added parsers module

* Updated utilities module

* Updated main.rs

* Updated api_parser and song model

* Updated icarus_action model

* Updated structs in managers module

* Updated main.rs

* More changes

* Saving changes

* Added new struct and methods

* More changes. Not functional yet

* Changed name of the project

* Removed cmake workflow

* Adding rust workflow

* Adding code to the syncers module

* Cargo formatting

* Continuing work

* Adding code to the syncers module

* Added dependencies

* Adding more code to the syncers module

* Added more code to the syncers module

It's not 100 percent done yet, I'll work on other modules

* Added more code to the managers module

* Added code to request a token

* Cargo formatting

* Adding more code to multi target upload

* Adding more multi-target upload code and formatted code

* Added more multi-upload code

* cargo fmt

* Got uploading functional and cargo fmt-ing

* Uploading multi target works, but is buggy

Not all of the songs are being uploaded

* Added single target upload

* Single target upload is functional but need to revisit multi-target upload

Multi-target upload is broken now. Needs to be revisited

* Fixed multi target upload

* Can now retrieve songs

* Downloading functionality is working

* Delete functionality is operational

* Updated README

* More cleanup

* Added test
2024-06-15 12:22:17 -04:00

74 lines
1.9 KiB
Rust

use std::default::Default;
use crate::models;
pub struct TokenManager {
pub user: models::user::User,
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 async fn request_token(&self) -> Result<models::token::Token, 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 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 api = &mut self.api;
api.version = String::from("v1");
api.endpoint = String::from(format!("api/{}/login", api.version));
}
pub fn retrieve_url(&self) -> String {
let api = &self.api;
let mut url = String::from(&api.url);
url += &String::from(&api.endpoint);
url += &String::from("/");
return url;
}
}