Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions ayon_api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import re
import datetime
import copy
import logging
import json
import uuid
import string
import platform
Expand Down Expand Up @@ -101,8 +103,9 @@ def _get_description(response):
return HTTPStatus(response.status).description


class RestApiResponse(object):
class RestApiResponse:
"""API Response."""
log = logging.getLogger("RestApiResponse")

def __init__(self, response, data=None):
if response is None:
Expand Down Expand Up @@ -134,7 +137,7 @@ def data(self):
if self._data is None:
try:
self._data = self.orig_response.json()
except RequestsJSONDecodeError:
except (AttributeError, RequestsJSONDecodeError):
self._data = {}
return self._data

Expand Down Expand Up @@ -179,15 +182,20 @@ def raise_for_status(self, message=None):
if message is None:
message = str(exc)

# Get 'detail' from response.json() if possible because it'll be
# more descriptive than default http error message
try:
detail = exc.response.json()["detail"]
if detail:
message = f"{message} ({detail})"
except (AttributeError, KeyError, RequestsJSONDecodeError):
pass
submsg = ""
if self.data:
submsg = json.dumps(self.data, indent=4)

self.log.warning(
"HTTP request error: %s%s%s",
message,
"\n" if submsg else "",
submsg,
)

detail = self.data.get("detail")
if detail:
message = f"{message} ({detail})"
raise HTTPRequestError(message, exc.response)

def __enter__(self, *args, **kwargs):
Expand Down