Skip to content

Commit

Permalink
fix: Improved types with mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
robsonke committed Jan 11, 2025
1 parent 8c86306 commit 54dfcea
Show file tree
Hide file tree
Showing 6 changed files with 217 additions and 78 deletions.
4 changes: 4 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@ repos:
rev: v0.8.1
hooks:
- id: ruff
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.910
hooks:
- id: mypy
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# iBroadcastAIO

[![Upload the iBroadcastAIO Package to PyPI when a Release is created](https://github.com/robsonke/ibroadcastaio/actions/workflows/publish.yml/badge.svg)](https://github.com/robsonke/ibroadcastaio/actions/workflows/publish.yml)

A Python library inspired by [ibroadcast-python](https://github.com/ctrueden/ibroadcast-python) for interacting with the iBroadcast API in an async manner.
This library is initially built for usage in an iBroadcast music provider in [Music Assistant](https://music-assistant.io/).

Expand Down
54 changes: 29 additions & 25 deletions ibroadcastaio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class IBroadcastClient:
def __init__(self, http_session: ClientSession) -> None:
"""Main constructor"""
self.http_session = http_session
self._albums = None
self._artists = None
self._playlists = None
self._tags = None
self._tracks = None
self._settings = None
self._status = None
self._albums: Dict[int, Any] = {}
self._artists: Dict[int, Any] = {}
self._playlists: Dict[int, Any] = {}
self._tags: Dict[int, Any] = {}
self._tracks: Dict[int, Any] = {}
self._settings: Dict[str, Any] = {}
self._status: Dict[str, Any] = {}

async def login(self, username: str, password: str) -> Dict[str, Any]:
"""Login to the iBroadcast API and return the status dict"""
Expand Down Expand Up @@ -53,7 +53,7 @@ def get_version(self) -> str:

async def refresh_library(self) -> None:
"""Fetch the library to cache it locally"""
data = {
data: Dict[str, Any] = {
"_token": self._status["user"]["token"],
"_userid": self._status["user"]["id"],
"client": REFERER,
Expand Down Expand Up @@ -191,49 +191,49 @@ async def get_full_stream_url(
f"&version={self.get_version()}"
)

async def get_artist(self, artist_id: int):
async def get_artist(self, artist_id: int) -> Dict[str, Any]:
"""Get an artist by ID"""
self._check_library_loaded()
return self._artists.get(artist_id)
return self._artists.get(artist_id, {})

async def get_artists(self):
async def get_artists(self) -> Dict[int, Any]:
"""Get all artists"""
self._check_library_loaded()
return self._artists

async def get_tag(self, tag_id: int):
async def get_tag(self, tag_id: int) -> Dict[str, Any]:
self._check_library_loaded()
return self._tags.get(tag_id)
return self._tags.get(tag_id, {})

async def get_tags(self):
async def get_tags(self) -> Dict[int, Any]:
self._check_library_loaded()
return self._tags

async def get_settings(self):
async def get_settings(self) -> Dict[str, Any]:
self._check_library_loaded()
return self._settings

async def get_album(self, album_id: int):
async def get_album(self, album_id: int) -> Dict[str, Any]:
self._check_library_loaded()
return self._albums.get(album_id)
return self._albums.get(album_id, {})

async def get_albums(self):
async def get_albums(self) -> Dict[int, Any]:
self._check_library_loaded()
return self._albums

async def get_track(self, track_id: int):
async def get_track(self, track_id: int) -> Dict[str, Any]:
self._check_library_loaded()
return self._tracks.get(track_id)
return self._tracks.get(track_id, {})

async def get_tracks(self):
async def get_tracks(self) -> Dict[int, Any]:
self._check_library_loaded()
return self._tracks

async def get_playlist(self, playlist_id: int):
async def get_playlist(self, playlist_id: int) -> Dict[str, Any]:
self._check_library_loaded()
return self._playlists.get(playlist_id)
return self._playlists.get(playlist_id, {})

async def get_playlists(self):
async def get_playlists(self) -> Dict[int, Any]:
self._check_library_loaded()
return self._playlists

Expand Down Expand Up @@ -303,7 +303,11 @@ async def __json_to_dict(
}
}
"""
if "map" not in data or type(data["map"]) is not dict:
if (
not isinstance(data, dict)
or "map" not in data
or not isinstance(data["map"], dict)
):
return

keymap = {v: k for (k, v) in data["map"].items() if not isinstance(v, dict)}
Expand Down
Loading

0 comments on commit 54dfcea

Please sign in to comment.