Updated icarus_models (#44)

* Updated icarus_models

* Constants and url changes

* Refactoring

* Removed functions

* Fixing tests
This commit was merged in pull request #44.
This commit is contained in:
KD
2025-03-30 16:00:06 -04:00
committed by GitHub
parent 24e066d8d9
commit 219b95bb2f
9 changed files with 93 additions and 125 deletions
+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;
}
}