Trying to get serialization working
This commit is contained in:
@@ -16,9 +16,9 @@ def main():
|
|||||||
username=sys.argv[1],
|
username=sys.argv[1],
|
||||||
password=sys.argv[2])
|
password=sys.argv[2])
|
||||||
|
|
||||||
print(server.ping())
|
# print(server.ping())
|
||||||
print(server.get_license())
|
# print(server.get_license())
|
||||||
# print(server.get_music_folders())
|
print(server.get_music_folders())
|
||||||
# print(server.get_indexes())
|
# print(server.get_indexes())
|
||||||
# print()
|
# print()
|
||||||
# print(server.get_music_directory(599))
|
# print(server.get_music_directory(599))
|
||||||
|
@@ -1,5 +1,7 @@
|
|||||||
import inspect
|
import inspect
|
||||||
from typing import Dict, List, Any
|
from typing import Dict, List, Any
|
||||||
|
from datetime import datetime
|
||||||
|
from dateutil import parser
|
||||||
|
|
||||||
|
|
||||||
class APIObject:
|
class APIObject:
|
||||||
@@ -33,19 +35,27 @@ class APIObject:
|
|||||||
instance: cls = cls()
|
instance: cls = cls()
|
||||||
for name, value in data.items():
|
for name, value in data.items():
|
||||||
field_type = annotations.get(name)
|
field_type = annotations.get(name)
|
||||||
if inspect.isclass(field_type) and isinstance(
|
print('ohea', field_type, value)
|
||||||
value, (dict, tuple, list, set, frozenset)):
|
if inspect.isclass(field_type):
|
||||||
|
if isinstance(value, (dict, tuple, list, set, frozenset)):
|
||||||
setattr(instance, name, field_type.from_json(value))
|
setattr(instance, name, field_type.from_json(value))
|
||||||
|
elif field_type == datetime:
|
||||||
|
setattr(instance, name, parser.parse(value))
|
||||||
|
else:
|
||||||
|
setattr(instance, name, value)
|
||||||
else:
|
else:
|
||||||
setattr(instance, name, value)
|
setattr(instance, name, value)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
|
def get(self, field, default=None):
|
||||||
|
return getattr(self, field, default)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
annotations: Dict[str, Any] = self.__annotations__
|
annotations: Dict[str, Any] = self.__annotations__
|
||||||
typename = type(self).__name__
|
typename = type(self).__name__
|
||||||
fieldstr = ' '.join([
|
fieldstr = ' '.join([
|
||||||
f'{field}={self.__getattribute__(field)!r}'
|
f'{field}={getattr(self, field)!r}'
|
||||||
for field in annotations.keys() if hasattr(self, field)
|
for field in annotations.keys() if hasattr(self, field)
|
||||||
])
|
])
|
||||||
return f'<{typename} {fieldstr}>'
|
return f'<{typename} {fieldstr}>'
|
||||||
@@ -55,10 +65,19 @@ class SubsonicError(APIObject):
|
|||||||
code: int
|
code: int
|
||||||
message: str
|
message: str
|
||||||
|
|
||||||
|
def as_exception(self):
|
||||||
|
return Exception(f'{self.code}: {self.message}')
|
||||||
|
|
||||||
|
|
||||||
class License(APIObject):
|
class License(APIObject):
|
||||||
valid: bool
|
valid: bool
|
||||||
email: str
|
email: str
|
||||||
|
licenseExpires: datetime
|
||||||
|
|
||||||
|
|
||||||
|
class MusicFolder(APIObject):
|
||||||
|
id: int
|
||||||
|
name: str
|
||||||
|
|
||||||
|
|
||||||
class SubsonicResponse(APIObject):
|
class SubsonicResponse(APIObject):
|
||||||
@@ -66,3 +85,4 @@ class SubsonicResponse(APIObject):
|
|||||||
version: str
|
version: str
|
||||||
license: License
|
license: License
|
||||||
error: SubsonicError
|
error: SubsonicError
|
||||||
|
musicFolders: List[MusicFolder]
|
||||||
|
@@ -27,8 +27,20 @@ class Server:
|
|||||||
def _post(self, url, **params):
|
def _post(self, url, **params):
|
||||||
params = {**self._get_params(), **params}
|
params = {**self._get_params(), **params}
|
||||||
result = requests.post(url, data=params)
|
result = requests.post(url, data=params)
|
||||||
# TODO error handling
|
subsonic_response = result.json()['subsonic-response']
|
||||||
return SubsonicResponse.from_json(result.json()['subsonic-response'])
|
# TODO make better
|
||||||
|
if not subsonic_response:
|
||||||
|
raise Exception('Fail!')
|
||||||
|
|
||||||
|
print(subsonic_response)
|
||||||
|
|
||||||
|
response = SubsonicResponse.from_json(subsonic_response)
|
||||||
|
|
||||||
|
# Check for an error and if it exists, raise it.
|
||||||
|
if response.get('error'):
|
||||||
|
raise response.error.as_exception()
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
def ping(self) -> SubsonicResponse:
|
def ping(self) -> SubsonicResponse:
|
||||||
return self._post(self._make_url('ping'))
|
return self._post(self._make_url('ping'))
|
||||||
@@ -39,7 +51,7 @@ class Server:
|
|||||||
|
|
||||||
def get_music_folders(self):
|
def get_music_folders(self):
|
||||||
result = self._post(self._make_url('getMusicFolders'))
|
result = self._post(self._make_url('getMusicFolders'))
|
||||||
return result
|
return result.musicFolders
|
||||||
|
|
||||||
def get_indexes(self):
|
def get_indexes(self):
|
||||||
result = self._post(self._make_url('getIndexes'))
|
result = self._post(self._make_url('getIndexes'))
|
||||||
|
1
setup.py
1
setup.py
@@ -43,6 +43,7 @@ setup(
|
|||||||
keywords='airsonic subsonic libresonic music',
|
keywords='airsonic subsonic libresonic music',
|
||||||
packages=find_packages(exclude=['tests']),
|
packages=find_packages(exclude=['tests']),
|
||||||
install_requires=[
|
install_requires=[
|
||||||
|
'python-dateutil',
|
||||||
'requests',
|
'requests',
|
||||||
'pyyaml',
|
'pyyaml',
|
||||||
'gobject',
|
'gobject',
|
||||||
|
Reference in New Issue
Block a user