Trying to get serialization working

This commit is contained in:
Sumner Evans
2019-05-14 18:52:04 -06:00
parent 3334c3d448
commit ff93b1b67f
4 changed files with 43 additions and 10 deletions

View File

@@ -16,9 +16,9 @@ def main():
username=sys.argv[1],
password=sys.argv[2])
print(server.ping())
print(server.get_license())
# print(server.get_music_folders())
# print(server.ping())
# print(server.get_license())
print(server.get_music_folders())
# print(server.get_indexes())
# print()
# print(server.get_music_directory(599))

View File

@@ -1,5 +1,7 @@
import inspect
from typing import Dict, List, Any
from datetime import datetime
from dateutil import parser
class APIObject:
@@ -33,19 +35,27 @@ class APIObject:
instance: cls = cls()
for name, value in data.items():
field_type = annotations.get(name)
if inspect.isclass(field_type) and isinstance(
value, (dict, tuple, list, set, frozenset)):
setattr(instance, name, field_type.from_json(value))
print('ohea', field_type, value)
if inspect.isclass(field_type):
if isinstance(value, (dict, tuple, list, set, frozenset)):
setattr(instance, name, field_type.from_json(value))
elif field_type == datetime:
setattr(instance, name, parser.parse(value))
else:
setattr(instance, name, value)
else:
setattr(instance, name, value)
return instance
def get(self, field, default=None):
return getattr(self, field, default)
def __repr__(self):
annotations: Dict[str, Any] = self.__annotations__
typename = type(self).__name__
fieldstr = ' '.join([
f'{field}={self.__getattribute__(field)!r}'
f'{field}={getattr(self, field)!r}'
for field in annotations.keys() if hasattr(self, field)
])
return f'<{typename} {fieldstr}>'
@@ -55,10 +65,19 @@ class SubsonicError(APIObject):
code: int
message: str
def as_exception(self):
return Exception(f'{self.code}: {self.message}')
class License(APIObject):
valid: bool
email: str
licenseExpires: datetime
class MusicFolder(APIObject):
id: int
name: str
class SubsonicResponse(APIObject):
@@ -66,3 +85,4 @@ class SubsonicResponse(APIObject):
version: str
license: License
error: SubsonicError
musicFolders: List[MusicFolder]

View File

@@ -27,8 +27,20 @@ class Server:
def _post(self, url, **params):
params = {**self._get_params(), **params}
result = requests.post(url, data=params)
# TODO error handling
return SubsonicResponse.from_json(result.json()['subsonic-response'])
subsonic_response = 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:
return self._post(self._make_url('ping'))
@@ -39,7 +51,7 @@ class Server:
def get_music_folders(self):
result = self._post(self._make_url('getMusicFolders'))
return result
return result.musicFolders
def get_indexes(self):
result = self._post(self._make_url('getIndexes'))

View File

@@ -43,6 +43,7 @@ setup(
keywords='airsonic subsonic libresonic music',
packages=find_packages(exclude=['tests']),
install_requires=[
'python-dateutil',
'requests',
'pyyaml',
'gobject',