From 40934b2eca29af1b3094584961458ed54bee8601 Mon Sep 17 00:00:00 2001 From: Phil Budne Date: Mon, 10 Aug 2026 12:47:10 -0400 Subject: [PATCH] Handle responses without (or with bad) JSON data --- mediacloud/api.py | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/mediacloud/api.py b/mediacloud/api.py index 8264f2d..a155559 100644 --- a/mediacloud/api.py +++ b/mediacloud/api.py @@ -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):