A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1use async_graphql::*;
2
3use crate::rockbox_url;
4
5use super::objects::device::Device;
6
7#[derive(Default)]
8pub struct DeviceQuery;
9
10#[Object]
11impl DeviceQuery {
12 async fn devices(&self, _ctx: &Context<'_>) -> Result<Vec<Device>, Error> {
13 let client = reqwest::Client::new();
14 let url = format!("{}/devices", rockbox_url());
15 let response = client.get(&url).send().await?;
16 let response = response.json::<Vec<Device>>().await?;
17 Ok(response)
18 }
19
20 async fn device(&self, _ctx: &Context<'_>, id: String) -> Result<Option<Device>, Error> {
21 let client = reqwest::Client::new();
22 let url = format!("{}/devices/{}", rockbox_url(), id);
23 let response = client.get(&url).send().await?;
24
25 if response.status() == 404 {
26 return Ok(None);
27 }
28
29 let response = response.json::<Option<Device>>().await?;
30 Ok(response)
31 }
32}
33
34#[derive(Default)]
35pub struct DeviceMutation;
36
37#[Object]
38impl DeviceMutation {
39 async fn connect(&self, _ctx: &Context<'_>, id: String) -> Result<bool, Error> {
40 let client = reqwest::Client::new();
41 let url = format!("{}/devices/{}/connect", rockbox_url(), id);
42 client.put(&url).send().await?;
43 Ok(true)
44 }
45
46 async fn disconnect(&self, _ctx: &Context<'_>, id: String) -> Result<bool, Error> {
47 let client = reqwest::Client::new();
48 let url = format!("{}/devices/{}/disconnect", rockbox_url(), id);
49 client.put(&url).send().await?;
50 Ok(true)
51 }
52}