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
This commit was merged in pull request #21.
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
use std::default::Default;
|
||||
|
||||
use crate::models;
|
||||
|
||||
pub struct Download {
|
||||
pub api: models::api::API,
|
||||
}
|
||||
|
||||
impl Default for Download {
|
||||
fn default() -> Self {
|
||||
Download {
|
||||
api: models::api::API::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum MyError {
|
||||
_Request(reqwest::Error),
|
||||
Other(String),
|
||||
}
|
||||
|
||||
impl Download {
|
||||
pub async fn download_song(
|
||||
&mut self,
|
||||
token: &models::token::Token,
|
||||
song: &models::song::Song,
|
||||
) -> Result<String, MyError> {
|
||||
self.api.endpoint = String::from("song/data/download");
|
||||
let url = self.retrieve_url(&song);
|
||||
let access_token = token.bearer_token();
|
||||
|
||||
let mut headers = reqwest::header::HeaderMap::new();
|
||||
headers.insert(
|
||||
reqwest::header::AUTHORIZATION,
|
||||
http::header::HeaderValue::from_str(&access_token.clone()).unwrap(),
|
||||
);
|
||||
|
||||
let client = reqwest::Client::builder().build().unwrap();
|
||||
let response = client
|
||||
.get(&url)
|
||||
.header(reqwest::header::AUTHORIZATION, &access_token)
|
||||
.send()
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
match response.status() {
|
||||
reqwest::StatusCode::OK => {
|
||||
let data = response.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);
|
||||
}
|
||||
}
|
||||
|
||||
return Err(MyError::Other(String::from("Error downloading")));
|
||||
}
|
||||
|
||||
fn retrieve_url(&self, song: &models::song::Song) -> String {
|
||||
let api = &self.api;
|
||||
let mut url: String = String::from(&api.url);
|
||||
url += &String::from("api/");
|
||||
url += &String::from(&api.version);
|
||||
url += &String::from("/");
|
||||
url += &String::from(&api.endpoint);
|
||||
url += &String::from("/");
|
||||
url += &song.id.unwrap().to_string();
|
||||
|
||||
return url;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user