Skip to content

Commit dce3274

Browse files
authored
Merge pull request #3 from MusicBoxRaspberryPi/develop
Add and Fix Endpoints
2 parents a18010c + 54bff67 commit dce3274

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

app/spotify/router.py

Lines changed: 70 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,79 @@
22
from fastapi import APIRouter, Depends
33

44
from app.container import Container
5-
from app.spotify.schemas import Device
5+
from app.spotify.exceptions import TrackNotFoundError, DeviceNotFoundError
6+
from app.spotify.schemas import Device, CurrentDevice, Track
67
from app.spotify.service import SpotifyService
78

89
spotify_router = APIRouter()
910

1011

11-
@spotify_router.get("/devices")
12+
@spotify_router.get("/device")
1213
@inject
13-
def get_devices(
14-
spotify_service: SpotifyService = Depends(Provide[Container.spotify_service]),
15-
) -> list[Device]:
16-
return spotify_service.refresh_devices()
14+
def get_current_device(
15+
spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
16+
) -> CurrentDevice | dict:
17+
spotify_service.refresh_devices()
18+
current_device = spotify_service.get_current_device()
19+
20+
if current_device is None:
21+
return {}
22+
23+
return CurrentDevice(
24+
device=current_device,
25+
index=spotify_service.get_current_device_index(),
26+
total=spotify_service.get_devices_count()
27+
)
28+
29+
30+
@spotify_router.post("/device/next")
31+
@inject
32+
def next_device(
33+
spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
34+
) -> CurrentDevice | dict:
35+
spotify_service.refresh_devices()
36+
spotify_service.next_device()
37+
current_device = spotify_service.get_current_device()
38+
39+
if current_device is None:
40+
return {}
41+
42+
return CurrentDevice(
43+
device=current_device,
44+
index=spotify_service.get_current_device_index(),
45+
total=spotify_service.get_devices_count()
46+
)
47+
48+
49+
@spotify_router.post("/device/previous")
50+
@inject
51+
def previous_device(
52+
spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
53+
) -> CurrentDevice | dict:
54+
spotify_service.refresh_devices()
55+
spotify_service.previous_device()
56+
current_device = spotify_service.get_current_device()
57+
58+
if current_device is None:
59+
return {}
60+
61+
return CurrentDevice(
62+
device=current_device,
63+
index=spotify_service.get_current_device_index(),
64+
total=spotify_service.get_devices_count()
65+
)
66+
67+
68+
@spotify_router.post("/play")
69+
@inject
70+
def play(
71+
track: Track,
72+
spotify_service: SpotifyService = Depends(Provide[Container.spotify_service])
73+
) -> Device | dict:
74+
try:
75+
spotify_service.play(track)
76+
except TrackNotFoundError:
77+
return {"error": "Track not found"}
78+
except DeviceNotFoundError:
79+
return {"error": "Device not found"}
80+
return {"status": "ok"}

app/spotify/schemas.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ class Device(BaseModel):
1212
volume_percent: int
1313

1414

15+
class CurrentDevice(BaseModel):
16+
device: Device
17+
index: int
18+
total: int
19+
20+
1521
class Track(BaseModel):
1622
id: str
1723

app/spotify/service.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ def play(self, track: Track) -> None:
4444
raise TrackNotFoundError(e.msg)
4545

4646
def refresh_devices(self) -> list[Device]:
47-
self.__devices = self.__get_devices_from_api()
48-
self.__current_device_index = 0
47+
devices_from_api = self.__get_devices_from_api()
48+
49+
if devices_from_api != self.__devices:
50+
self.__devices = devices_from_api
51+
self.__current_device_index = 0
52+
4953
return self.__devices
5054

5155
def next_device(self) -> Device | None:
@@ -68,10 +72,15 @@ def get_current_device(self) -> Device | None:
6872

6973
return self.__devices[self.__current_device_index]
7074

75+
def get_current_device_index(self) -> int:
76+
return self.__current_device_index
77+
7178
def get_devices(self) -> list[Device]:
72-
print(self.__devices)
7379
return self.__devices
7480

81+
def get_devices_count(self) -> int:
82+
return len(self.__devices)
83+
7584
def __get_devices_from_api(self) -> list[Device]:
7685
devices_json = self.__api.devices()["devices"]
7786
return [Device(**device) for device in devices_json]

app/system/router.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66

77
@system_router.get("/", include_in_schema=False)
8-
async def root(self) -> dict[str, str]:
8+
async def root() -> dict[str, str]:
99
return {"message": "MusicBox API"}
1010

1111

1212
@system_router.get("/health", include_in_schema=False)
13-
async def health(self) -> JSONResponse:
13+
async def health() -> JSONResponse:
1414
data = {"music-box-api": status.HTTP_200_OK}
1515
return JSONResponse(data, status_code=status.HTTP_200_OK)

0 commit comments

Comments
 (0)