Skip to content

Commit

Permalink
Merge pull request #180 from markkuleinio/master
Browse files Browse the repository at this point in the history
Fixes #165: Import AllocationError from pynetbox.core.query, also add ContentError to handle json failures
  • Loading branch information
Zach Moody authored Sep 27, 2019
2 parents a0ce25e + da91ea7 commit ec79364
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pynetbox/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from pkg_resources import get_distribution, DistributionNotFound

from pynetbox.core.query import RequestError
from pynetbox.core.query import RequestError, AllocationError, ContentError
from pynetbox.api import Api as api

try:
Expand Down
52 changes: 47 additions & 5 deletions pynetbox/core/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,29 @@ def __init__(self, message):
self.error = message


class ContentError(Exception):
"""Content Exception
If the API URL does not point to a valid NetBox API, the server may
return a valid response code, but the content is not json. This
exception is raised in those cases.
"""

def __init__(self, message):
req = message

message = (
"The server returned invalid (non-json) data. Maybe not "
"a NetBox server?"
)

super(ContentError, self).__init__(message)
self.req = req
self.request_body = req.request.body
self.url = req.url
self.error = message


class Request(object):
"""Creates requests to the Netbox API
Expand Down Expand Up @@ -157,7 +180,10 @@ def get_session_key(self):
verify=self.ssl_verify,
)
if req.ok:
return req.json()["session_key"]
try:
return req.json()["session_key"]
except json.JSONDecodeError:
raise ContentError(req)
else:
raise RequestError(req)

Expand Down Expand Up @@ -214,6 +240,7 @@ def get(self):
any paginated results.
:raises: RequestError if req.ok returns false.
:raises: ContentError if response is not json.
:Returns: List of `Response` objects returned from the
endpoint.
Expand All @@ -228,7 +255,10 @@ def make_request(url):

req = requests.get(url, headers=headers, verify=self.ssl_verify)
if req.ok:
return req.json()
try:
return req.json()
except json.JSONDecodeError:
raise ContentError(req)
else:
raise RequestError(req)

Expand Down Expand Up @@ -266,6 +296,7 @@ def put(self, data):
:param data: (dict) Contains a dict that will be turned into a
json object and sent to the API.
:raises: RequestError if req.ok returns false.
:raises: ContentError if response is not json.
:returns: Dict containing the response from NetBox's API.
"""
headers = {
Expand All @@ -281,7 +312,10 @@ def put(self, data):
verify=self.ssl_verify,
)
if req.ok:
return req.json()
try:
return req.json()
except json.JSONDecodeError:
raise ContentError(req)
else:
raise RequestError(req)

Expand All @@ -297,6 +331,7 @@ def post(self, data):
:raises: AllocationError if req.status_code is 204 (No Content)
as with available-ips and available-prefixes when there is
no room for the requested allocation.
:raises: ContentError if response is not json.
:Returns: Dict containing the response from NetBox's API.
"""
headers = {
Expand All @@ -314,7 +349,10 @@ def post(self, data):
if req.status_code == 204:
raise AllocationError(req)
elif req.ok:
return req.json()
try:
return req.json()
except json.JSONDecodeError:
raise ContentError(req)
else:
raise RequestError(req)

Expand Down Expand Up @@ -349,6 +387,7 @@ def patch(self, data):
:param data: (dict) Contains a dict that will be turned into a
json object and sent to the API.
:raises: RequestError if req.ok returns false.
:raises: ContentError if response is not json.
:returns: Dict containing the response from NetBox's API.
"""
headers = {
Expand All @@ -364,6 +403,9 @@ def patch(self, data):
verify=self.ssl_verify,
)
if req.ok:
return req.json()
try:
return req.json()
except json.JSONDecodeError:
raise ContentError(req)
else:
raise RequestError(req)

0 comments on commit ec79364

Please sign in to comment.