Queue song (#57)
* Added code to queue song * Making functions async * Able to queue song * Cleanup
This commit was merged in pull request #57.
This commit is contained in:
+3
-2
@@ -7,7 +7,8 @@ mod utilities;
|
||||
|
||||
use std::env;
|
||||
|
||||
fn main() {
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
let args_len = args.len() as i32;
|
||||
|
||||
@@ -29,7 +30,7 @@ fn main() {
|
||||
ica_action: chosen_act,
|
||||
};
|
||||
|
||||
cmt_mgr.commit_action();
|
||||
cmt_mgr.commit_action().await;
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
@@ -79,7 +79,7 @@ pub fn retrieve_song(
|
||||
}
|
||||
|
||||
impl CommitManager {
|
||||
pub fn commit_action(&mut self) {
|
||||
pub async fn commit_action(&mut self) {
|
||||
let action = &self.ica_action.action;
|
||||
println!("Committing {action} action");
|
||||
|
||||
@@ -91,11 +91,11 @@ impl CommitManager {
|
||||
// TODO: Move code to get token here and then pass it to the respective functions
|
||||
|
||||
match mapped_action {
|
||||
ActionValues::DeleteAct => self.delete_song(),
|
||||
ActionValues::DownloadAct => self.download_song(),
|
||||
ActionValues::RetrieveAct => self.retrieve_object(),
|
||||
ActionValues::DeleteAct => self.delete_song().await,
|
||||
ActionValues::DownloadAct => self.download_song().await,
|
||||
ActionValues::RetrieveAct => self.retrieve_object().await,
|
||||
ActionValues::UploadAct => self.upload_song(),
|
||||
ActionValues::UploadSongWithMetadata => self.upload_song_with_metadata(),
|
||||
ActionValues::UploadSongWithMetadata => self.upload_song_with_metadata().await,
|
||||
_ => {
|
||||
println!("Nothing good here");
|
||||
}
|
||||
@@ -132,7 +132,7 @@ impl CommitManager {
|
||||
actions
|
||||
}
|
||||
|
||||
fn delete_song(&self) {
|
||||
async fn delete_song(&self) {
|
||||
let mut prsr = parsers::api_parser::APIParser {
|
||||
apis: vec![models::api::Api::default(), models::api::Api::default()],
|
||||
ica_act: self.ica_action.clone(),
|
||||
@@ -141,7 +141,7 @@ impl CommitManager {
|
||||
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);
|
||||
let token = self.parse_token(&auth_api).await;
|
||||
|
||||
println!("Deleting song");
|
||||
|
||||
@@ -173,7 +173,7 @@ impl CommitManager {
|
||||
}
|
||||
}
|
||||
|
||||
fn download_song(&self) {
|
||||
async fn download_song(&self) {
|
||||
println!("Deleting song");
|
||||
let dwn = self.ica_action.retrieve_flag_value(&String::from("-b"));
|
||||
let song_id = uuid::Uuid::from_str(dwn.as_str()).unwrap();
|
||||
@@ -186,7 +186,7 @@ impl CommitManager {
|
||||
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);
|
||||
let token = self.parse_token(&auth_api).await;
|
||||
println!("Message: {}", token.message);
|
||||
let api = prsr.retrieve_api(parsers::api_parser::APIType::Main);
|
||||
|
||||
@@ -220,7 +220,7 @@ impl CommitManager {
|
||||
}
|
||||
}
|
||||
|
||||
fn retrieve_object(&self) {
|
||||
async fn retrieve_object(&self) {
|
||||
println!("Retrieving song");
|
||||
let rt = self.ica_action.retrieve_flag_value(&String::from("-rt"));
|
||||
|
||||
@@ -236,7 +236,7 @@ impl CommitManager {
|
||||
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);
|
||||
let token = self.parse_token(&auth_api).await;
|
||||
println!("Token {token:?}");
|
||||
|
||||
let api = prsr.retrieve_api(parsers::api_parser::APIType::Main);
|
||||
@@ -265,7 +265,7 @@ impl CommitManager {
|
||||
panic!("Not supported");
|
||||
}
|
||||
|
||||
fn parse_token(&self, api: &models::api::Api) -> icarus_models::token::AccessToken {
|
||||
async fn parse_token(&self, api: &models::api::Api) -> icarus_models::token::AccessToken {
|
||||
println!("Fetching token");
|
||||
|
||||
let mut usr_mgr: managers::user_manager::UserManager =
|
||||
@@ -282,12 +282,12 @@ impl CommitManager {
|
||||
};
|
||||
tok_mgr.init();
|
||||
|
||||
let token = Runtime::new().unwrap().block_on(tok_mgr.request_token());
|
||||
let token = tok_mgr.request_token().await;
|
||||
|
||||
token.unwrap()
|
||||
}
|
||||
|
||||
fn upload_song_with_metadata(&mut self) {
|
||||
async fn upload_song_with_metadata(&mut self) {
|
||||
println!("Uplodaring song with metadara");
|
||||
|
||||
let songpath = self.ica_action.retrieve_flag_value(&String::from("-s"));
|
||||
@@ -314,15 +314,17 @@ impl CommitManager {
|
||||
println!("metadata path: {metadata_path}");
|
||||
println!("cover art path: {coverpath}");
|
||||
|
||||
let _ = self.sing_target_upload(&songpath, &track_id, &metadata_path, &coverpath);
|
||||
let _ = self
|
||||
.sing_target_upload(&songpath, &track_id, &metadata_path, &coverpath)
|
||||
.await;
|
||||
} else if multitarget {
|
||||
let _ = self.multi_target_upload(&uni);
|
||||
let _ = self.multi_target_upload(&uni).await;
|
||||
} else {
|
||||
println!("Single or Multi target has not been chosen");
|
||||
}
|
||||
}
|
||||
|
||||
fn sing_target_upload(
|
||||
async fn sing_target_upload(
|
||||
&mut self,
|
||||
songpath: &String,
|
||||
track_id: &str,
|
||||
@@ -337,7 +339,7 @@ impl CommitManager {
|
||||
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);
|
||||
let token = self.parse_token(&auth_api).await;
|
||||
|
||||
println!("Token: {:?}", token.token);
|
||||
|
||||
@@ -376,21 +378,9 @@ impl CommitManager {
|
||||
|
||||
cover_art.data = cover_art.to_data().unwrap();
|
||||
|
||||
let mut up = syncers::upload::Upload::default();
|
||||
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
||||
up.set_api(&host);
|
||||
|
||||
let res = up.upload_song_with_metadata(&token, &s, &cover_art);
|
||||
|
||||
match Runtime::new().unwrap().block_on(res) {
|
||||
Ok(o) => {
|
||||
println!("Successfully sent {o:?}");
|
||||
Ok(())
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Some error {er:?}");
|
||||
Err(std::io::Error::other(er.to_string()))
|
||||
}
|
||||
match self.queue_song(&token, &s).await {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
@@ -408,6 +398,33 @@ impl CommitManager {
|
||||
}
|
||||
}
|
||||
|
||||
async fn queue_song(
|
||||
&self,
|
||||
token: &icarus_models::token::AccessToken,
|
||||
song: &icarus_models::song::Song,
|
||||
) -> Result<()> {
|
||||
let mut up = syncers::upload::Upload::default();
|
||||
let host = self.ica_action.retrieve_flag_value(&String::from("-h"));
|
||||
up.set_api(&host);
|
||||
|
||||
let mut queued_song_id = uuid::Uuid::nil();
|
||||
|
||||
println!("Queueing song");
|
||||
|
||||
match up.queue_song(token, song).await {
|
||||
Ok(id) => {
|
||||
queued_song_id = id;
|
||||
}
|
||||
Err(err) => {
|
||||
eprintln!("Error: {err:?}");
|
||||
}
|
||||
}
|
||||
|
||||
println!("Queued song Id: {queued_song_id:?}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn get_songs(
|
||||
&self,
|
||||
metadata_path: &String,
|
||||
@@ -455,7 +472,7 @@ impl CommitManager {
|
||||
}
|
||||
}
|
||||
|
||||
fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
||||
async fn multi_target_upload(&mut self, sourcepath: &String) -> std::io::Result<()> {
|
||||
let mut prsr = parsers::api_parser::APIParser {
|
||||
apis: vec![models::api::Api::default(), models::api::Api::default()],
|
||||
ica_act: self.ica_action.clone(),
|
||||
@@ -463,7 +480,7 @@ impl CommitManager {
|
||||
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);
|
||||
let token = self.parse_token(&auth_api).await;
|
||||
|
||||
let directory_path = std::path::Path::new(&sourcepath);
|
||||
|
||||
|
||||
@@ -11,6 +11,16 @@ pub struct Upload {
|
||||
pub api: models::api::Api,
|
||||
}
|
||||
|
||||
mod response {
|
||||
pub mod queue_song {
|
||||
#[derive(Debug, serde::Deserialize)]
|
||||
pub struct Response {
|
||||
pub message: String,
|
||||
pub data: Vec<uuid::Uuid>,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Upload {
|
||||
pub async fn upload_song_with_metadata(
|
||||
&mut self,
|
||||
@@ -51,6 +61,60 @@ impl Upload {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn queue_song(
|
||||
&self,
|
||||
token: &icarus_models::token::AccessToken,
|
||||
song: &icarus_models::song::Song,
|
||||
) -> Result<uuid::Uuid, reqwest::Error> {
|
||||
let songpath = song.song_path().unwrap();
|
||||
let file = tokio::fs::File::open(&songpath).await.unwrap();
|
||||
let stream = tokio_util::codec::FramedRead::new(file, tokio_util::codec::BytesCodec::new());
|
||||
let file_body = reqwest::Body::wrap_stream(stream);
|
||||
|
||||
let form = reqwest::multipart::Form::new().part(
|
||||
"file",
|
||||
reqwest::multipart::Part::stream(file_body)
|
||||
.file_name(song.filename.clone())
|
||||
.mime_str("application/octet-stream")
|
||||
.unwrap(),
|
||||
);
|
||||
|
||||
let endpoint = String::from("api/v2/song/queue");
|
||||
let url = format!("{}/{endpoint}", self.api.url);
|
||||
println!("Url: {url:?}");
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
HeaderValue::from_str(&token.bearer_token()).unwrap(),
|
||||
);
|
||||
headers.insert("Accept", HeaderValue::from_str("*/*").unwrap());
|
||||
headers.insert("Connection", HeaderValue::from_str("keep-alive").unwrap());
|
||||
headers.insert("Cache-Control", HeaderValue::from_str("no-cache").unwrap());
|
||||
|
||||
let client = reqwest::Client::builder()
|
||||
.timeout(std::time::Duration::from_secs(300))
|
||||
.pool_idle_timeout(std::time::Duration::from_secs(30))
|
||||
.build()
|
||||
.unwrap();
|
||||
match client
|
||||
.post(url)
|
||||
.headers(headers)
|
||||
.multipart(form)
|
||||
.send()
|
||||
.await
|
||||
{
|
||||
Ok(response) => match response.json::<response::queue_song::Response>().await {
|
||||
Ok(resp) => {
|
||||
println!("Message: {:?}", resp.message);
|
||||
Ok(resp.data[0])
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
fn init_form(
|
||||
&self,
|
||||
song: &icarus_models::song::Song,
|
||||
|
||||
Reference in New Issue
Block a user