Fetch next queued song #125

Merged
kdeng00 merged 6 commits from fetch_next_queued_song into v0.2 2025-04-26 18:38:17 -04:00
2 changed files with 35 additions and 28 deletions
Showing only changes of commit a8076ddcf4 - Show all commits
+6 -5
View File
@@ -226,7 +226,8 @@ pub mod endpoint {
pub async fn download_flac( pub async fn download_flac(
axum::Extension(pool): axum::Extension<sqlx::PgPool>, axum::Extension(pool): axum::Extension<sqlx::PgPool>,
axum::extract::Path(id): axum::extract::Path<uuid::Uuid>) -> (StatusCode, axum::response::Response) { axum::extract::Path(id): axum::extract::Path<uuid::Uuid>,
) -> (StatusCode, axum::response::Response) {
println!("Id: {:?}", id); println!("Id: {:?}", id);
match song_queue::get_data(&pool, &id).await { match song_queue::get_data(&pool, &id).await {
@@ -240,14 +241,14 @@ pub mod endpoint {
); );
headers.insert( headers.insert(
axum::http::header::CONTENT_DISPOSITION, axum::http::header::CONTENT_DISPOSITION,
format!("attachment; filename=\"{}.flac\"", id).parse().unwrap(), format!("attachment; filename=\"{}.flac\"", id)
.parse()
.unwrap(),
); );
(StatusCode::OK, response) (StatusCode::OK, response)
} }
Err(_err) => { Err(_err) => (StatusCode::BAD_REQUEST, axum::response::Response::default()),
(StatusCode::BAD_REQUEST, axum::response::Response::default())
}
} }
} }
} }
+29 -23
View File
@@ -231,17 +231,22 @@ mod tests {
app.clone().oneshot(req).await app.clone().oneshot(req).await
} }
async fn fetch_queue_req(app: &axum::Router,) -> Result<axum::response::Response, std::convert::Infallible> { async fn fetch_queue_req(
let fetch_req = axum::http::Request::builder() app: &axum::Router,
.method(axum::http::Method::GET) ) -> Result<axum::response::Response, std::convert::Infallible> {
.uri(crate::callers::endpoints::NEXTQUEUESONG) let fetch_req = axum::http::Request::builder()
.header(axum::http::header::CONTENT_TYPE, "application/json") .method(axum::http::Method::GET)
.body(axum::body::Body::empty()) .uri(crate::callers::endpoints::NEXTQUEUESONG)
.unwrap(); .header(axum::http::header::CONTENT_TYPE, "application/json")
.body(axum::body::Body::empty())
.unwrap();
app.clone().oneshot(fetch_req).await app.clone().oneshot(fetch_req).await
} }
async fn fetch_queue_data_req(app: &axum::Router, id: &uuid::Uuid) -> Result<axum::response::Response, std::convert::Infallible> { async fn fetch_queue_data_req(
app: &axum::Router,
id: &uuid::Uuid,
) -> Result<axum::response::Response, std::convert::Infallible> {
let raw_uri = String::from(crate::callers::endpoints::QUEUESONGDATA); let raw_uri = String::from(crate::callers::endpoints::QUEUESONGDATA);
let end_index = raw_uri.len() - 4; let end_index = raw_uri.len() - 4;
let mut uri: String = (&raw_uri[..end_index]).to_string(); let mut uri: String = (&raw_uri[..end_index]).to_string();
@@ -256,18 +261,17 @@ mod tests {
app.clone().oneshot(req).await app.clone().oneshot(req).await
} }
pub async fn resp_to_bytes(response: axum::response::Response) -> Result<axum::body::Bytes, axum::Error> { pub async fn resp_to_bytes(
axum::body::to_bytes(response.into_body(), usize::MAX) response: axum::response::Response,
.await ) -> Result<axum::body::Bytes, axum::Error> {
axum::body::to_bytes(response.into_body(), usize::MAX).await
} }
pub async fn get_resp_data<Data>(response: axum::response::Response) -> Data pub async fn get_resp_data<Data>(response: axum::response::Response) -> Data
where where
Data: for<'a> serde::Deserialize<'a>, Data: for<'a> serde::Deserialize<'a>,
{ {
let body = resp_to_bytes(response) let body = resp_to_bytes(response).await.unwrap();
.await
.unwrap();
serde_json::from_slice(&body).unwrap() serde_json::from_slice(&body).unwrap()
} }
@@ -391,16 +395,18 @@ mod tests {
let id = resp.data[0].id; let id = resp.data[0].id;
match fetch_queue_data_req(&app, &id).await { match fetch_queue_data_req(&app, &id).await {
Ok(response) => { Ok(response) => match resp_to_bytes(response).await {
match resp_to_bytes(response).await { Ok(bytes) => {
Ok(bytes) => { assert_eq!(
assert_eq!(false, bytes.is_empty(), "Queued data should not be empty"); false,
} bytes.is_empty(),
Err(err) => { "Queued data should not be empty"
assert!(false, "Error: {:?}", err); );
}
} }
} Err(err) => {
assert!(false, "Error: {:?}", err);
}
},
Err(err) => { Err(err) => {
assert!(false, "Error: {:?}", err); assert!(false, "Error: {:?}", err);
} }