Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Commit

Permalink
Added support for the InvalidSetID exception
Browse files Browse the repository at this point in the history
That, as well as some more documentation. The InvalidSetID error will be
thrown if getSet fails, which would be less than ideal. However, this is
a better situation than returning a list with one item in it.
  • Loading branch information
4Kaylum committed May 14, 2017
1 parent fbeff3e commit 5a0ae32
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
19 changes: 13 additions & 6 deletions brickfront/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from .errors import InvalidRequest as _InvalidRequest
from .errors import InvalidKey as _InvalidKey
from .errors import InvalidLogin as _InvalidLogin
from .errors import InvalidSetID as _InvalidSetID
from .build import Build as _Build
from .review import Review as _Review

Expand Down Expand Up @@ -57,7 +58,7 @@ def _isOkayRequest(self, request) -> bool:
'''

# Check the status code of the returned request
if request.status_code is not 200:
if str(request.status_code)[0] not in ['2', '3']:
w = str(request.text).split('\\r')[0][2:]
raise _InvalidRequest(w)
return
Expand Down Expand Up @@ -162,13 +163,14 @@ def getSets(self, **kwargs) -> list:
root = _ET.fromstring(returned.text)
return [_Build(i, self._userHash) for i in root]

def getSet(self, setID: str) -> list:
def getSet(self, setID: str) -> _Build:
'''
Gets the information of one build, using its Brickset set ID.
:param str setID: The ID of the build from Brickset.
:returns: A single LEGO set in a list. Will return an empty list if no sets are found.
:rtype: List[:class:`brickfront.build.Build`]
:returns: A single LEGO set object.
:rtype: :class:`brickfront.build.Build`
:raises brickfront.errors.InvalidSetID: If no sets exist by that ID.
'''

values = {
Expand All @@ -184,7 +186,10 @@ def getSet(self, setID: str) -> list:
self._isOkayRequest(returned)

root = _ET.fromstring(returned.text)
return [_Build(i, self._userHash) for i in root]
v = [_Build(i, self._userHash) for i in root]
if len(v) == 0:
raise _InvalidSetID
return v[0]

def getRecentlyUpdatedSets(self, minutesAgo: int) -> list:
'''
Expand All @@ -207,7 +212,7 @@ def getRecentlyUpdatedSets(self, minutesAgo: int) -> list:
self._isOkayRequest(returned)

root = _ET.fromstring(returned.text)
return [_Build(i) for i in root]
return [_Build(i, self._userHash) for i in root]

def getAdditionalImages(self, setID: str) -> list:
'''
Expand All @@ -216,6 +221,7 @@ def getAdditionalImages(self, setID: str) -> list:
:param str setID: The ID of the set you want to grab the images for.
:returns: A list of URL strings.
:rtype: List[`str`]
.. warning:: If a set ID is invalid, an empty list will be returned.
'''

values = {
Expand Down Expand Up @@ -244,6 +250,7 @@ def getReviews(self, setID: str) -> list:
:param str setID: The ID of the set you want to get the reviews of.
:returns: A list of reviews.
:rtype: List[:class:`brickfront.review.Review`]
.. warning:: If a set ID is invalid, an empty list will be returned.
'''

values = {
Expand Down
7 changes: 7 additions & 0 deletions brickfront/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,10 @@ class InvalidLogin(Exception):
'''

pass

class InvalidSetID(Exception):
'''
The set ID that was passed is invalid - eg it doesn't exist.
'''

pass

0 comments on commit 5a0ae32

Please sign in to comment.