add endpoints
Seto Elkahfi committed
Jul 31, 2024 at 23:33 UTC
bf66acefeb08988644a5bf1165bc21c95ff1b533
7 files changed
+136
-8
frontend/splitfire-desktop/src-tauri/src/command/constants.rs
+9
@@ -18,8 +18,17 @@ pub const API_HOST: &str = "api.musik88.com";
18
19
// Paths
20
pub const PATH_AUDIO: &str = "api/v1/splitfire";
21
+
22
+// Account Paths
23
pub const PATH_ACCOUNT_LOGIN: &str = "api/v1/login";
24
pub const PATH_ACCOUNT_REGISTER: &str = "api/v1/register";
25
+pub const PATH_ACCOUNT_LOGOUT: &str = "api/v1/logout";
26
+
27
+// Contents Paths
28
+pub const PATH_CAROUSEL: &str = "api/v1/carousel";
29
+pub const PATH_READY_TO_PLAY: &str = "api/v1/ready-to-play";
30
+pub const PATH_SEARCH: &str = "api/v1//search";
31
+pub const PATH_TOP_VOTES: &str = "api/v1/top-votes";
32
33
pub fn base_url_builder() -> URLBuilder {
34
let mut url_builder = URLBuilder::new();
frontend/splitfire-desktop/src-tauri/src/main.rs
+6
-6
@@ -2,7 +2,6 @@
2
all(not(debug_assertions), target_os = "windows"),
3
windows_subsystem = "windows"
4
)]
5
-
5
use app::{
6
command::{
7
pages::{__cmd__open_lyrics_editor, __cmd__open_player, open_lyrics_editor, open_player},
@@ -18,8 +17,9 @@ use app::{
17
player_set_volume::{__cmd__player_set_volume, player_set_volume},
18
player_unmount::{__cmd__player_unmount, player_unmount},
19
},
21
- rest::account::{
22
- __cmd__account_login, __cmd__account_register, account_login, account_register,
20
+ rest::{
21
+ account::{__cmd__account_login, __cmd__account_register, account_login, account_register},
22
+ content::{__cmd__content_carousel, __cmd__content_ready_to_play, content_carousel, content_ready_to_play},
23
},
24
sfai_home_dir_path,
25
};
@@ -40,9 +40,7 @@ fn main() {
40
)
41
.build(),
42
)
43
- .plugin(
44
- tauri_plugin_store::Builder::default().build(),
45
- )
43
+ .plugin(tauri_plugin_store::Builder::default().build())
44
.setup(|app| {
45
let mut store = StoreBuilder::new(app.handle(), "splitfire.bin".parse()?).build();
46
match store.load() {
@@ -69,6 +67,8 @@ fn main() {
67
player_play,
68
player_stop,
69
player_recording_length,
70
+ content_carousel,
71
+ content_ready_to_play,
72
])
73
.run(tauri::generate_context!())
74
.expect("Error while running tauri application.");
frontend/splitfire-desktop/src-tauri/src/models/content.rs
new
+38
@@ -0,0 +1,38 @@
1
+use serde::{Deserialize, Serialize};
2
+use super::player::{AudioFile, TauriResponse};
3
+
4
+#[derive(Serialize)]
5
+#[derive(Debug)]
6
+pub struct ContentCarouselResponse {
7
+ pub status: TauriResponse,
8
+ pub message: String,
9
+ pub audio_files: Vec<SongProvider>,
10
+}
11
+
12
+#[derive(Serialize, Deserialize)]
13
+#[derive(Debug)]
14
+pub struct CarouselResponse {
15
+ code: i32,
16
+ pub message: String,
17
+ pub audio_files: Vec<SongProvider>,
18
+}
19
+
20
+#[derive(Serialize, Deserialize)]
21
+#[derive(Debug)]
22
+pub struct SongProvider {
23
+ pub id: i32,
24
+ pub name: String,
25
+ pub provider_id: String,
26
+ pub provider_type: ProviderType,
27
+ pub image_url: String,
28
+ pub audio_file: Option<AudioFile>
29
+}
30
+
31
+#[derive(Serialize, Deserialize)]
32
+#[derive(Debug)]
33
+pub enum ProviderType {
34
+ #[serde(rename = "youtube")]
35
+ Youtube,
36
+ #[serde(rename = "spotify")]
37
+ Spotify,
38
+}
\ No newline at end of file
frontend/splitfire-desktop/src-tauri/src/models/mod.rs
+2
-1
@@ -1,2 +1,3 @@
1
pub mod account;
2
-pub mod player;
\ No newline at end of file
2
+pub mod player;
3
+pub mod content;
\ No newline at end of file
frontend/splitfire-desktop/src-tauri/src/models/player.rs
+2
@@ -23,6 +23,7 @@ impl Display for ResultFile {
23
}
24
25
#[derive(Serialize, Deserialize)]
26
+#[derive(Debug)]
27
enum Status {
28
#[serde(rename = "downloading")]
29
Downloading,
@@ -33,6 +34,7 @@ enum Status {
34
}
35
36
#[derive(Serialize, Deserialize)]
37
+#[derive(Debug)]
38
pub struct AudioFile {
39
pub id: i32,
40
pub name: String,
frontend/splitfire-desktop/src-tauri/src/rest/content.rs
new
+77
@@ -0,0 +1,77 @@
1
+use crate::{
2
+ command::constants::{base_url_builder, PATH_CAROUSEL, PATH_READY_TO_PLAY},
3
+ models::{
4
+ content::{CarouselResponse, ContentCarouselResponse},
5
+ player::TauriResponse,
6
+ },
7
+};
8
+use reqwest::Client;
9
+
10
+#[tauri::command]
11
+pub async fn content_carousel() -> ContentCarouselResponse {
12
+ let response = Client::new()
13
+ .get(content_url_builder(PATH_CAROUSEL))
14
+ .send()
15
+ .await;
16
+
17
+ let response = match response {
18
+ Ok(response) => response,
19
+ Err(e) => {
20
+ println!("Failed to get response: {:?}", e);
21
+ return ContentCarouselResponse {
22
+ status: TauriResponse::Error,
23
+ message: "Failed to get response".to_string(),
24
+ audio_files: vec![],
25
+ };
26
+ }
27
+ };
28
+ let res: CarouselResponse = match response.json().await {
29
+ Ok(json) => json,
30
+ Err(e) => {
31
+ println!("Failed to parse response: {:?}", e);
32
+ return ContentCarouselResponse {
33
+ status: TauriResponse::Error,
34
+ message: "Failed to parse response".to_string(),
35
+ audio_files: vec![],
36
+ };
37
+ }
38
+ };
39
+
40
+ ContentCarouselResponse {
41
+ status: TauriResponse::Success,
42
+ message: res.message,
43
+ audio_files: res.audio_files,
44
+ }
45
+}
46
+
47
+#[tauri::command]
48
+pub async fn content_ready_to_play() -> Result<(), String> {
49
+ let response = Client::new()
50
+ .get(content_url_builder(PATH_READY_TO_PLAY))
51
+ .send()
52
+ .await;
53
+
54
+ let response = match response {
55
+ Ok(response) => response,
56
+ Err(e) => {
57
+ println!("Failed to get response: {:?}", e);
58
+ return Err("Failed to get response".to_string());
59
+ }
60
+ };
61
+ let res: CarouselResponse = match response.json().await {
62
+ Ok(json) => json,
63
+ Err(e) => {
64
+ println!("Failed to parse response: {:?}", e);
65
+ return Err("Failed to parse response".to_string());
66
+ }
67
+ };
68
+
69
+ println!("Ready to play: {:?}", res);
70
+ Ok(())
71
+}
72
+
73
+fn content_url_builder(path: &str) -> String {
74
+ let mut url_builder = base_url_builder();
75
+ url_builder.add_route(path);
76
+ url_builder.build()
77
+}
frontend/splitfire-desktop/src-tauri/src/rest/mod.rs
+2
-1
@@ -1 +1,2 @@
1
-pub mod account;
\ No newline at end of file
1
+pub mod account;
2
+pub mod content;
\ No newline at end of file