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
26 changes: 23 additions & 3 deletions mediacloud/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,30 @@ def _query(self, endpoint: str, params: Optional[Dict] = None, method: str = 'GE
r = self._session.patch(endpoint_url, json=params, timeout=self.TIMEOUT_SECS)
else:
raise RuntimeError(f"Unsupported method of '{method}'")
if r.status_code // 100 != 2: # create operations return 201
raise mediacloud.error.APIResponseError(r, params, r.json())

return r.json()
status_class = r.status_code // 100
if r.text:
try:
j = r.json()
except:
# here with non/bad json response
j = {
"status": "bad-response",
"note": f"bad JSON: {r.text}" # for APIResponseError
}
status_class = 99
else:
# Here with 429 error from older servers
j = {
"status": "empty",
"note": "empty-response" # for APIResponseError
}
status_class = 99

if status_class != 2: # create operations return 201
raise mediacloud.error.APIResponseError(r, params, j)

return j


class DirectoryApi(BaseApi):
Expand Down
Loading