summaryrefslogtreecommitdiff
path: root/src/ampache/ampache.py
diff options
context:
space:
mode:
Diffstat (limited to 'src/ampache/ampache.py')
-rw-r--r--src/ampache/ampache.py37
1 files changed, 25 insertions, 12 deletions
diff --git a/src/ampache/ampache.py b/src/ampache/ampache.py
index 9282040..12ccbd7 100644
--- a/src/ampache/ampache.py
+++ b/src/ampache/ampache.py
@@ -1,9 +1,5 @@
-from http import client
-from urllib import parse
from datetime import datetime
-import exceptions
-import models
-import json
+from ampache import models, exceptions
import requests
@@ -36,19 +32,32 @@ class AmpacheClient:
self._api_key = api_key
self.authenticate()
- def raise_by_status(self, error_code):
+ def raise_by_status(self, error):
"""
The ampache API has a bunch of error codes that can help us diagnose
the problem. Depending of the error code, we'll raise an appropiate exception.
"""
- pass
+ if error.code == exceptions.ACCESS_ERROR_CODE:
+ raise exceptions.AccessException(error.message)
+ elif error.code == exceptions.AUTHENTICATION_ERROR_CODE:
+ raise exceptions.AuthenticationException(error.message)
+ elif error.code == exceptions.ACCESS_DENIED_ERROR_CODE:
+ raise exceptions.AccessDeniedException(error.message)
+ elif error.code == exceptions.NOT_FOUND_ERROR_CODE:
+ raise exceptions.NotFoundException(error.message)
+ elif error.code == exceptions.MISSING_ERROR_CODE:
+ raise exceptions.MissingMethodException(error.message)
+ elif error.code == exceptions.DEPRECIATED_ERROR_CODE:
+ raise exceptions.DepreciatedMethodException(error.message)
+ elif error.code == exceptions.BAD_REQUEST_ERROR_CODE:
+ raise exceptions.BadRequestException(error.message)
+ elif error.code == exceptions.FAILED_ACCESS_ERROR_CODE:
+ raise exceptions.FailedAccessException(error.message)
def request(self, params, headers):
"""
All in one function to pass JSON requests to the API.
"""
- self.renew_token()
-
response = requests.get(self._host + self._endpoint, params=params,
headers=headers)
if not response.ok:
@@ -57,11 +66,11 @@ class AmpacheClient:
data = response.json()
if 'error' in data:
- raise possible_exception(data['error']['errorMessage'])
+ self.raise_by_status(models.Error(int(data['error']['errorCode']),
+ data['error']['errorMessage']))
return data
-
def authenticate(self):
"""
Authenticate with the API, setting the token.
@@ -87,7 +96,7 @@ class AmpacheClient:
params = {
'action': 'song',
- 'auth': self._auth,
+ 'auth': self._auth.auth,
'filter': song_id,
'version': self._version
}
@@ -95,6 +104,10 @@ class AmpacheClient:
'Content-type': 'application/json'
}
data = self.request(params, headers)
+ song = models.Song(data['id'], data['title'], data['album']['name'],
+ data['albumartist']['name'])
+
+ return song
def renew_token(self):
if datetime.now(self._auth.expires.tzinfo) > self._auth.expires: