Replace axum with Rocket
Axum is shit and doesn't work
This commit is contained in:
1 parent
ace0f4927a
commit
996504373e
7 files changed
+982
-467
No files matched your search
+55
-14
@@ -2,9 +2,11 @@ use crate::playlist::Playlist;
|
||||
use crate::track::Track;
|
||||
use reqwest::Client;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::{Display, Formatter};
|
||||
use thiserror::Error;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Subsonic {
|
||||
username: String,
|
||||
password: String,
|
||||
@@ -34,11 +36,24 @@ struct GetPlaylistResponse {
|
||||
playlist: Playlist,
|
||||
}
|
||||
|
||||
type CreatePlaylistResponse = GetPlaylistResponse;
|
||||
|
||||
struct AuthParams {
|
||||
salt: String,
|
||||
hash: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum SubsonicError {
|
||||
Reqwest(reqwest::Error),
|
||||
}
|
||||
|
||||
impl Display for SubsonicError {
|
||||
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}", self)
|
||||
}
|
||||
}
|
||||
|
||||
impl Subsonic {
|
||||
fn auth_params(&self) -> AuthParams {
|
||||
let salt = Uuid::new_v4().simple().to_string();
|
||||
@@ -50,10 +65,7 @@ impl Subsonic {
|
||||
AuthParams { salt, hash }
|
||||
}
|
||||
|
||||
pub async fn song_search(
|
||||
&self,
|
||||
query: String,
|
||||
) -> Result<Vec<Track>, Box<dyn std::error::Error>> {
|
||||
pub async fn song_search(&self, query: String) -> Result<Vec<Track>, SubsonicError> {
|
||||
let auth_params = self.auth_params();
|
||||
let query_params = [
|
||||
("query", query.as_str()),
|
||||
@@ -72,18 +84,17 @@ impl Subsonic {
|
||||
.get(format!("{}/search3", self.base))
|
||||
.query(&query_params)
|
||||
.send()
|
||||
.await?
|
||||
.await
|
||||
.map_err(|e| SubsonicError::Reqwest(e))?
|
||||
.json::<SubsonicResponse<SearchResponse>>()
|
||||
.await?
|
||||
.await
|
||||
.map_err(|e| SubsonicError::Reqwest(e))?
|
||||
.subsonic_response
|
||||
.search_results
|
||||
.song)
|
||||
}
|
||||
|
||||
pub async fn get_playlist(
|
||||
&self,
|
||||
playlist_id: String,
|
||||
) -> Result<Playlist, Box<dyn std::error::Error>> {
|
||||
pub async fn get_playlist(&self, playlist_id: String) -> Result<Playlist, SubsonicError> {
|
||||
let auth_params = self.auth_params();
|
||||
|
||||
let get_params = [
|
||||
@@ -101,9 +112,38 @@ impl Subsonic {
|
||||
.get(format!("{}/getPlaylist", self.base))
|
||||
.query(&get_params)
|
||||
.send()
|
||||
.await?
|
||||
.await
|
||||
.map_err(|e| SubsonicError::Reqwest(e))?
|
||||
.json::<SubsonicResponse<GetPlaylistResponse>>()
|
||||
.await?
|
||||
.await
|
||||
.map_err(|e| SubsonicError::Reqwest(e))?
|
||||
.subsonic_response
|
||||
.playlist)
|
||||
}
|
||||
|
||||
pub async fn create_playlist(&self, playlist_name: String) -> Result<Playlist, SubsonicError> {
|
||||
let auth_params = self.auth_params();
|
||||
|
||||
let get_params = [
|
||||
("u", self.username.to_string()),
|
||||
("s", auth_params.salt.to_string()),
|
||||
("t", auth_params.hash.to_string()),
|
||||
("v", String::from("16")),
|
||||
("c", String::from("playlistd")),
|
||||
("f", String::from("json")),
|
||||
("name", playlist_name),
|
||||
];
|
||||
|
||||
Ok(self
|
||||
.client
|
||||
.get(format!("{}/createPlaylist", self.base))
|
||||
.query(&get_params)
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| SubsonicError::Reqwest(e))?
|
||||
.json::<SubsonicResponse<CreatePlaylistResponse>>()
|
||||
.await
|
||||
.map_err(|e| SubsonicError::Reqwest(e))?
|
||||
.subsonic_response
|
||||
.playlist)
|
||||
}
|
||||
@@ -113,7 +153,7 @@ impl Subsonic {
|
||||
playlist_id: String,
|
||||
add: Vec<String>,
|
||||
remove: Vec<usize>,
|
||||
) -> Result<Playlist, Box<dyn std::error::Error>> {
|
||||
) -> Result<Playlist, SubsonicError> {
|
||||
let auth_params = self.auth_params();
|
||||
|
||||
let mut update_query = vec![
|
||||
@@ -138,7 +178,8 @@ impl Subsonic {
|
||||
.get(format!("{}/updatePlaylist", self.base))
|
||||
.query(&update_query)
|
||||
.send()
|
||||
.await?;
|
||||
.await
|
||||
.map_err(|e| SubsonicError::Reqwest(e))?;
|
||||
|
||||
self.get_playlist(playlist_id).await
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user