Updated icarus-model (#33)
* Updated icarus-model * Updated icarus-model * Saving changes * Code formatting * Removing code * Updated icarus-model * Updated token secret * Added host * Fix build issue * Fix build warnings * Saving changes * Saving changes * Refactoring * Migrating over icarus-model coverart * Removed song module * Another one * Meta single upload is functional * Cleanup * More cleanup * Added test files (#37) * Added test files Need to add a coverarg image file * Updated test album file * Added coverart * Uploading meta is operational * Code cleanup * Moved function * Added string module
This commit was merged in pull request #33.
This commit is contained in:
@@ -21,8 +21,8 @@ impl Delete {
|
||||
pub async fn delete_song(
|
||||
&mut self,
|
||||
token: &icarus_models::token::AccessToken,
|
||||
song: &models::song::Song,
|
||||
) -> Result<models::song::Song, std::io::Error> {
|
||||
song: &icarus_models::song::Song,
|
||||
) -> Result<icarus_models::song::Song, std::io::Error> {
|
||||
self.api.endpoint = "song/data/delete".to_owned();
|
||||
let url = self.retrieve_url(&song);
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
@@ -33,12 +33,12 @@ impl Delete {
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
let mut sng = models::song::Song::default();
|
||||
let mut sng = icarus_models::song::Song::default();
|
||||
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
println!("Success!");
|
||||
let s = response.json::<models::song::Song>().await;
|
||||
let s = response.json::<icarus_models::song::Song>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
@@ -57,7 +57,7 @@ impl Delete {
|
||||
return Ok(sng);
|
||||
}
|
||||
|
||||
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
||||
fn retrieve_url(&self, song: &icarus_models::song::Song) -> String {
|
||||
let api = &self.api;
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
@@ -65,7 +65,7 @@ impl Delete {
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &song.id.unwrap().to_string();
|
||||
url += &song.id.to_string();
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
+20
-22
@@ -24,7 +24,7 @@ impl Download {
|
||||
pub async fn download_song(
|
||||
&mut self,
|
||||
token: &icarus_models::token::AccessToken,
|
||||
song: &models::song::Song,
|
||||
song: &icarus_models::song::Song,
|
||||
) -> Result<String, MyError> {
|
||||
self.api.endpoint = String::from("song/data/download");
|
||||
let url = self.retrieve_url(&song);
|
||||
@@ -44,27 +44,25 @@ impl Download {
|
||||
.await;
|
||||
|
||||
match response {
|
||||
Ok(rep) => {
|
||||
match rep.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
let data = rep.text();
|
||||
match data.await {
|
||||
Ok(e) => {
|
||||
return Ok(e);
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error {:?}", er);
|
||||
Ok(rep) => match rep.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
let data = rep.text();
|
||||
match data.await {
|
||||
Ok(e) => {
|
||||
return Ok(e);
|
||||
}
|
||||
Err(er) => {
|
||||
println!("Error {:?}", er);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
reqwest::StatusCode::UNAUTHORIZED => {
|
||||
println!("Need to grab a new token");
|
||||
}
|
||||
other => {
|
||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||
}
|
||||
}
|
||||
}
|
||||
reqwest::StatusCode::UNAUTHORIZED => {
|
||||
println!("Need to grab a new token");
|
||||
}
|
||||
other => {
|
||||
panic!("Uh oh! Something unexpected happened: {:?}", other);
|
||||
}
|
||||
},
|
||||
Err(er) => {
|
||||
return Err(MyError::Request(er));
|
||||
}
|
||||
@@ -73,7 +71,7 @@ impl Download {
|
||||
return Err(MyError::Other(String::from("Error downloading")));
|
||||
}
|
||||
|
||||
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
||||
fn retrieve_url(&self, song: &icarus_models::song::Song) -> String {
|
||||
let api = &self.api;
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
@@ -81,7 +79,7 @@ impl Download {
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &song.id.unwrap().to_string();
|
||||
url += &song.id.to_string();
|
||||
|
||||
return url;
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ impl RetrieveRecords {
|
||||
pub async fn get_all_songs(
|
||||
&mut self,
|
||||
token: &icarus_models::token::AccessToken,
|
||||
) -> Result<Vec<models::song::Song>, Error> {
|
||||
) -> Result<Vec<icarus_models::song::Song>, Error> {
|
||||
self.api.endpoint = String::from("song");
|
||||
let mut songs: Vec<models::song::Song> = Vec::new();
|
||||
let mut songs: Vec<icarus_models::song::Song> = Vec::new();
|
||||
let url = self.retrieve_url();
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
@@ -46,7 +46,7 @@ impl RetrieveRecords {
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
// on success, parse our JSON to an APIResponse
|
||||
let s = response.json::<Vec<models::song::Song>>().await;
|
||||
let s = response.json::<Vec<icarus_models::song::Song>>().await;
|
||||
match s {
|
||||
//
|
||||
Ok(parsed) => {
|
||||
|
||||
+42
-31
@@ -3,7 +3,6 @@ use std::default::Default;
|
||||
use http::HeaderMap;
|
||||
use http::HeaderValue;
|
||||
use reqwest;
|
||||
use reqwest::multipart::Form;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::models;
|
||||
@@ -47,14 +46,13 @@ impl Upload {
|
||||
pub async fn upload_song_with_metadata(
|
||||
&mut self,
|
||||
token: &icarus_models::token::AccessToken,
|
||||
song: &models::song::Song,
|
||||
cover: &models::song::CoverArt,
|
||||
album: &models::song::Album,
|
||||
) -> Result<reqwest::Response, std::io::Error> {
|
||||
song: &icarus_models::song::Song,
|
||||
cover: &icarus_models::coverart::CoverArt,
|
||||
album: &icarus_models::album::collection::Album,
|
||||
) -> Result<reqwest::Response, reqwest::Error> {
|
||||
self.api.endpoint = String::from("song/data/upload/with/data");
|
||||
let url = self.retrieve_url();
|
||||
let mut new_song = self.initialize_song(&song, &album);
|
||||
new_song.songpath = song.song_path();
|
||||
let new_song = self.initialize_song(&song, &album);
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
if url.is_empty() {
|
||||
@@ -63,6 +61,7 @@ impl Upload {
|
||||
|
||||
println!("Url: {}", url);
|
||||
println!("Token: {}", access_token);
|
||||
println!("Path: {:?}", new_song.songpath);
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
@@ -73,18 +72,20 @@ impl Upload {
|
||||
|
||||
let form = self.init_form(&new_song, &cover);
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let response = client
|
||||
match client
|
||||
.post(url)
|
||||
.headers(headers)
|
||||
.multipart(form)
|
||||
.send()
|
||||
.await;
|
||||
let response_text = response.unwrap();
|
||||
|
||||
println!("Something was sent");
|
||||
println!("{:?}", response_text);
|
||||
|
||||
return Ok(response_text);
|
||||
.await
|
||||
{
|
||||
Ok(r) => {
|
||||
return Ok(r);
|
||||
}
|
||||
Err(err) => {
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn _initialize_form(
|
||||
@@ -92,7 +93,7 @@ impl Upload {
|
||||
song_raw_data: Vec<u8>,
|
||||
cover_raw_data: Vec<u8>,
|
||||
song_detail: String,
|
||||
) -> Form {
|
||||
) -> reqwest::multipart::Form {
|
||||
let mut headers = HeaderMap::new();
|
||||
headers.insert(
|
||||
http::header::CONTENT_TYPE,
|
||||
@@ -109,9 +110,9 @@ impl Upload {
|
||||
let cover = reqwest::multipart::Part::bytes(cover_raw_data).headers(headers_i);
|
||||
|
||||
let mut song_filename = String::from("audio");
|
||||
song_filename += icarus_models::constants::WAV_EXTENSION;
|
||||
song_filename += icarus_models::constants::WAVEXTENSION;
|
||||
let mut cover_filename = String::from("cover");
|
||||
cover_filename += icarus_models::constants::JPG_EXTENSION;
|
||||
cover_filename += icarus_models::constants::JPGEXTENSION;
|
||||
|
||||
return reqwest::multipart::Form::new()
|
||||
.part("cover", cover.file_name(cover_filename))
|
||||
@@ -119,17 +120,22 @@ impl Upload {
|
||||
.part("file", file.file_name(song_filename));
|
||||
}
|
||||
|
||||
fn init_form(&self, song: &Song, cover: &models::song::CoverArt) -> reqwest::multipart::Form {
|
||||
fn init_form(
|
||||
&self,
|
||||
song: &Song,
|
||||
cover: &icarus_models::coverart::CoverArt,
|
||||
) -> reqwest::multipart::Form {
|
||||
let songpath = song.songpath.clone();
|
||||
let coverpath = cover.path.clone().unwrap();
|
||||
let coverpath = cover.path.clone();
|
||||
println!("Cover path: {:?}", coverpath);
|
||||
let song_detail = song.to_metadata_json().unwrap();
|
||||
|
||||
println!("\n{}\n", song_detail);
|
||||
|
||||
let mut song_filename = String::from("audio");
|
||||
song_filename += icarus_models::constants::WAV_EXTENSION;
|
||||
song_filename += icarus_models::constants::DEFAULTMUSICEXTENSION;
|
||||
let mut cover_filename = String::from("cover");
|
||||
cover_filename += icarus_models::constants::JPG_EXTENSION;
|
||||
cover_filename += icarus_models::constants::JPGEXTENSION;
|
||||
|
||||
let form = reqwest::multipart::Form::new()
|
||||
.part(
|
||||
@@ -172,23 +178,28 @@ impl Upload {
|
||||
return url;
|
||||
}
|
||||
|
||||
fn initialize_song(&self, song: &models::song::Song, album: &models::song::Album) -> Song {
|
||||
let dur = song.duration.clone().unwrap();
|
||||
fn initialize_song(
|
||||
&self,
|
||||
song: &icarus_models::song::Song,
|
||||
album: &icarus_models::album::collection::Album,
|
||||
) -> Song {
|
||||
let dur = song.duration.clone();
|
||||
println!("Duration: {}", dur);
|
||||
|
||||
return Song {
|
||||
title: String::from(&song.title.clone().unwrap()),
|
||||
title: String::from(&song.title.clone()),
|
||||
album: album.title.clone(),
|
||||
artist: String::from(&song.artist.clone().unwrap().clone()),
|
||||
album_artist: album.album_artist.clone(),
|
||||
artist: String::from(&song.artist.clone().clone()),
|
||||
album_artist: album.artist.clone(),
|
||||
year: album.year.clone(),
|
||||
genre: album.genre.clone(),
|
||||
duration: f64::round(dur) as i32,
|
||||
track: (song.track.clone().unwrap()),
|
||||
// duration: f64::round(dur) as i32,
|
||||
duration: dur,
|
||||
track: (song.track.clone()),
|
||||
track_count: album.track_count.clone(),
|
||||
disc: song.disc.clone().unwrap(),
|
||||
disc: song.disc.clone(),
|
||||
disc_count: album.disc_count.clone(),
|
||||
songpath: String::new(),
|
||||
songpath: song.directory.clone() + "/" + &song.filename.clone(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user