Removed functions

This commit is contained in:
KD
2025-03-30 15:54:21 -04:00
parent 3a45773a9e
commit da5d62198c
3 changed files with 29 additions and 83 deletions
+9 -29
View File
@@ -1,6 +1,7 @@
use std::default::Default;
use crate::models;
use crate::syncers;
pub struct Download {
pub api: models::api::API,
@@ -27,23 +28,17 @@ impl Download {
song: &icarus_models::song::Song,
) -> Result<String, MyError> {
self.api.endpoint = String::from("song/data/download");
let url = self.retrieve_url(&song);
let url = syncers::common::retrieve_url(&self.api, true, song.id);
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
match client
.get(&url)
.header(reqwest::header::AUTHORIZATION, &access_token)
.send()
.await;
match response {
.await
{
Ok(rep) => match rep.status() {
reqwest::StatusCode::OK => {
let data = rep.text();
@@ -52,35 +47,20 @@ impl Download {
return Ok(e);
}
Err(er) => {
println!("Error {:?}", er);
return Err(MyError::Other(er.to_string()));
}
}
}
reqwest::StatusCode::UNAUTHORIZED => {
println!("Need to grab a new token");
return Err(MyError::Other(String::from("Need to grab a new token")));
}
other => {
panic!("Uh oh! Something unexpected happened: {:?}", other);
return Err(MyError::Other(other.to_string()));
}
},
Err(er) => {
return Err(MyError::Request(er));
}
}
return Err(MyError::Other(String::from("Error downloading")));
}
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/");
url += &String::from(&api.version);
url += &String::from("/");
url += &String::from(&api.endpoint);
url += &String::from("/");
url += &song.id.to_string();
return url;
}
}
+18 -35
View File
@@ -2,6 +2,7 @@ use std::default::Default;
use std::io::Error;
use crate::models;
use crate::syncers;
pub struct RetrieveRecords {
pub api: models::api::API,
@@ -21,20 +22,9 @@ impl RetrieveRecords {
token: &icarus_models::token::AccessToken,
) -> Result<Vec<icarus_models::song::Song>, Error> {
self.api.endpoint = String::from("song");
let mut songs: Vec<icarus_models::song::Song> = Vec::new();
let url = self.retrieve_url();
let url = syncers::common::retrieve_url(&self.api, false, 0);
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(),
);
headers.insert(
reqwest::header::CONTENT_TYPE,
http::header::HeaderValue::from_static("application/json"),
);
let client = reqwest::Client::builder().build().unwrap();
let response = client
.get(&url)
@@ -46,35 +36,28 @@ impl RetrieveRecords {
match response.status() {
reqwest::StatusCode::OK => {
// on success, parse our JSON to an APIResponse
let s = response.json::<Vec<icarus_models::song::Song>>().await;
match s {
//
Ok(parsed) => {
songs = parsed;
match response.json::<Vec<icarus_models::song::Song>>().await {
Ok(parsed) => Ok(parsed),
Err(err) => {
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
err.to_string(),
));
}
Err(_) => println!("Hm, the response didn't match the shape we expected."),
};
}
}
reqwest::StatusCode::UNAUTHORIZED => {
println!("Need to grab a new token");
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
"Need to grab a new token",
));
}
other => {
panic!("Uh oh! Something unexpected happened: {:?}", other);
return Err(std::io::Error::new(
std::io::ErrorKind::Other,
other.to_string(),
));
}
}
return Ok(songs);
}
fn retrieve_url(&self) -> 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("/");
return url;
}
}
+2 -19
View File
@@ -4,6 +4,7 @@ use http::HeaderValue;
use reqwest;
use crate::models;
use crate::syncers;
pub struct Upload {
pub api: models::api::API,
@@ -25,7 +26,7 @@ impl Upload {
cover: &icarus_models::coverart::CoverArt,
) -> Result<reqwest::Response, reqwest::Error> {
self.api.endpoint = String::from("song/data/upload/with/data");
let url = self.retrieve_url();
let url = syncers::common::retrieve_url(&self.api, false, 0);
let access_token = token.bearer_token();
if url.is_empty() {
@@ -106,22 +107,4 @@ impl Upload {
api.version = String::from("v1");
self.api = api;
}
fn retrieve_url(&self) -> String {
let api = &self.api;
let mut buffer = api.url.clone();
let count = buffer.len();
if buffer.chars().nth(count - 1) != Some('/') {
buffer += "/";
}
let mut url: String = String::from(&buffer);
url += &String::from("api/");
url += &String::from(&api.version);
url += &String::from("/");
url += &String::from(&api.endpoint);
return url;
}
}