diff --git a/scaleapi/api.py b/scaleapi/api.py index a440f43..078f305 100644 --- a/scaleapi/api.py +++ b/scaleapi/api.py @@ -109,9 +109,28 @@ def _api_request( json = None if res.status_code == 200: json = res.json() + elif res.status_code == 409 and "task" in endpoint and body.get("unique_id"): + retry_history = res.raw.retries.history + # Example RequestHistory tuple + # RequestHistory(method='POST', + # url='/v1/task/imageannotation', + # error=None, + # status=409, + # redirect_location=None) + if retry_history != (): + # See if the first retry was a 500 error + if retry_history[0][3] == 500: + uuid = body["unique_id"] + newUrl = f"{self.base_api_url}/tasks?unique_id={uuid}" + # grab task from api + newRes = self._http_request( + "GET", newUrl, headers=headers, auth=auth + ) + json = newRes.json()["docs"][0] + else: + self._raise_on_respose(res) else: self._raise_on_respose(res) - return json def get_request(self, endpoint, params=None):