Skip to content

Commit

Permalink
Merge pull request #3 from ycd/dev
Browse files Browse the repository at this point in the history
Create library specific errors with a generic exception class
  • Loading branch information
scientific-dev authored Sep 26, 2020
2 parents 5932cb0 + 3aafdc5 commit ab1f7a0
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 12 deletions.
10 changes: 6 additions & 4 deletions spotifyapi/Album.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Import Packages
import requests
from .Util import encodeURIComponent
from .exceptions import LimitOutOfRangeError


# Album Class
class Album():
Expand All @@ -12,8 +14,8 @@ def search(self, query: str, limit: int = 1):
link = 'https://api.spotify.com/v1/search'
header = {'Authorization': 'Bearer ' + self.token}

if limit >= 50:
raise ValueError('limit must be under 50')
if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
Expand Down Expand Up @@ -41,8 +43,8 @@ def get_tracks(self, albumID: str, limit: int = 1):
link = 'https://api.spotify.com/v1/albums/' + albumID + '/tracks'
header = {'Authorization': 'Bearer ' + self.token}

if limit >= 50:
raise ValueError('limit must be under 50')
if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
Expand Down
9 changes: 5 additions & 4 deletions spotifyapi/Artist.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Import Packages
import requests
from .Util import encodeURIComponent
from .exceptions import LimitOutOfRangeError

# Artist Class
class Artist():
Expand All @@ -12,8 +13,8 @@ def search(self, query: str, limit: int = 1):
link = 'https://api.spotify.com/v1/search'
header = {'Authorization': 'Bearer ' + self.token}

if limit >= 50:
raise ValueError('limit must be under 50')
if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
Expand Down Expand Up @@ -41,8 +42,8 @@ def albums(self, artistID: str, limit: int = 1):
link = 'https://api.spotify.com/v1/artists/' + artistID + '/albums'
header = {'Authorization': 'Bearer ' + self.token}

if limit >= 50:
raise ValueError('limit must be under 50')
if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
Expand Down
9 changes: 5 additions & 4 deletions spotifyapi/Track.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Import Packages
from .Util import encodeURIComponent
import requests
from .exceptions import LimitOutOfRangeError, InvalidTrackIdError

# Track Class
class Track():
Expand All @@ -12,8 +13,8 @@ def search(self, query: str, limit: int = 1):
link = 'https://api.spotify.com/v1/search'
header = {'Authorization': 'Bearer ' + self.token}

if limit >= 50:
raise ValueError('limit must be under 50')
if not 0 < limit < 50:
raise LimitOutOfRangeError('Limit must be under 50.')

return requests.request(
'GET',
Expand Down Expand Up @@ -49,7 +50,7 @@ def auido_features(self, trackID: str):
header = {'Authorization': 'Bearer ' + self.token}

if ' ' in [char for char in trackID]:
raise TypeError('invalid track id provided')
raise InvalidTrackIdError("Invalid track has been provided.")

link = 'https://api.spotify.com/v1/audio-features/' + trackID

Expand All @@ -63,7 +64,7 @@ def auido_analysis(self, trackID: str):
header = {'Authorization': 'Bearer ' + self.token}

if ' ' in [char for char in trackID]:
raise TypeError('invalid track id provided')
raise InvalidTrackIdError("Invalid track has been provided.")

link = 'https://api.spotify.com/v1/audio-analysis/' + trackID

Expand Down
19 changes: 19 additions & 0 deletions spotifyapi/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class SpotifyError(Exception):
"""
Generic base class for exceptions.
"""
def __init__(self, message):
self.message = message

def __str__(self):
class_name = self.__class__.__name__
return f"{class_name}: {self.message}"

class LimitOutOfRangeError(SpotifyError):
def __init__(self, message):
super().__init__(message)

class InvalidTrackIdError(SpotifyError):
def __init__(self, message):
super().__init__(message)

0 comments on commit ab1f7a0

Please sign in to comment.