summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHombreLaser <sebastian-440@live.com>2022-05-14 15:36:37 -0500
committerHombreLaser <sebastian-440@live.com>2022-05-14 15:36:37 -0500
commitc2172c94524cff1958de412eedd9efa21f60466f (patch)
tree209f8f975d8a70f480d69735a9a01564111840ca
parent74b01c4942e38afce9efdf60fa46ab573f4f6f86 (diff)
Corregidos errores
-rw-r--r--src/ampache/ampache.py37
-rw-r--r--src/ampache/exceptions.py51
-rw-r--r--src/ampache/models.py9
-rw-r--r--src/ampache/test.py4
-rw-r--r--src/test.py4
5 files changed, 88 insertions, 17 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:
diff --git a/src/ampache/exceptions.py b/src/ampache/exceptions.py
index 345d8e2..af0cbf0 100644
--- a/src/ampache/exceptions.py
+++ b/src/ampache/exceptions.py
@@ -1,6 +1,16 @@
+ACCESS_ERROR_CODE = 4700
+AUTHENTICATION_ERROR_CODE = 4701
+ACCESS_DENIED_ERROR_CODE = 4703
+NOT_FOUND_ERROR_CODE = 4704
+MISSING_ERROR_CODE = 4705
+DEPRECIATED_ERROR_CODE = 4706
+BAD_REQUEST_ERROR_CODE = 4710
+FAILED_ACCESS_ERROR_CODE = 4712
+
+
class AccessException(Exception):
"""
- Happens when the API is not enabled. Error code: 4700-
+ Happens when the API is not enabled. Error code: 4700.
"""
pass
@@ -18,3 +28,42 @@ class AccessDeniedException(Exception):
Happens when the request method is not enabled. Error code: 4703
"""
pass
+
+
+class NotFoundException(Exception):
+ """
+ Happens when the requested object couldn't be found. Error code:
+ 4704
+ """
+ pass
+
+
+class MissingMethodException(Exception):
+ """
+ Happens when the client requests a method the API doesn't implement.
+ Error code: 4705
+ """
+ pass
+
+
+class DepreciatedMethodException(Exception):
+ """
+ Happens when a given method is deprecated. Error code: 4706.
+ """
+ pass
+
+
+class BadRequestException(Exception):
+ """
+ Happens when the API recieves a wrongly formed request. Error code:
+ 4710.
+ """
+ pass
+
+
+class FailedAccessException(Exception):
+ """
+ Happens when the object or method can't be accessed by a given user.
+ Error code: 4712.
+ """
+ pass
diff --git a/src/ampache/models.py b/src/ampache/models.py
index 2e0ed1b..9508b2e 100644
--- a/src/ampache/models.py
+++ b/src/ampache/models.py
@@ -23,3 +23,12 @@ class Song:
def __str__(self):
return f"{self.title} by {self.artist}"
+
+
+@dataclass
+class Error:
+ code: int
+ message: str
+
+ def __str__(self):
+ return f"{self.message} with code {self.code}"
diff --git a/src/ampache/test.py b/src/ampache/test.py
deleted file mode 100644
index 8ca38df..0000000
--- a/src/ampache/test.py
+++ /dev/null
@@ -1,4 +0,0 @@
-from ampache import AmpacheClient
-
-my_ampache = AmpacheClient('https://music.silosneeded.com', '22efaa9fecfd8f074ef4bc95e85a1b84', '532000')
-print(my_ampache.get_token())
diff --git a/src/test.py b/src/test.py
new file mode 100644
index 0000000..fdf8c5b
--- /dev/null
+++ b/src/test.py
@@ -0,0 +1,4 @@
+from ampache.ampache import AmpacheClient
+
+my_ampache = AmpacheClient('https://music.silosneeded.com', '4dee7fe5554cf581a3f69ea023ea378a', '532000')
+print(my_ampache.get_song(4713))