From af3d3e42bf8fabbd1a5f11072ec28f53afadc02f Mon Sep 17 00:00:00 2001 From: Fatih Kurtoglu Date: Fri, 9 Dec 2022 16:01:06 -0800 Subject: [PATCH 1/2] implement task audit api --- README.rst | 21 +++++++++++++++++++++ scaleapi/__init__.py | 17 +++++++++++++++++ scaleapi/_version.py | 2 +- scaleapi/tasks.py | 4 ++++ 4 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.rst b/README.rst index a21b5d2..bd15810 100644 --- a/README.rst +++ b/README.rst @@ -239,6 +239,27 @@ __ https://docs.scale.com/reference#cancel-task task.cancel(clear_unique_id=True) +Audit a Task +^^^^^^^^^^^^ + +This method allows you to ``accept`` or ``reject`` completed tasks, along with support for adding comments about the reason for the given audit status, mirroring our Audit UI. +Check out `Scale's API documentation`__ for more information. + +__ https://docs.scale.com/reference/audit-a-task + +.. code-block :: python + + # Accept a completed task by submitting an audit + client.audit_task('30553edd0b6a93f8f05f0fee', True) + + # Reject a completed task by submitting a comment with the audit + client.audit_task('30553edd0b6a93f8f05f0fee', False, 'Rejected due to quality') + + # audit() is also available on Task object + task = client.get_task('30553edd0b6a93f8f05f0fee') + task.audit(True) + + Update A Task's Unique Id ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/scaleapi/__init__.py b/scaleapi/__init__.py index e6bb922..082cfa7 100644 --- a/scaleapi/__init__.py +++ b/scaleapi/__init__.py @@ -83,6 +83,23 @@ def cancel_task(self, task_id: str, clear_unique_id: bool = False) -> Task: endpoint = f"task/{task_id}/cancel" return Task(self.api.post_request(endpoint), self) + def audit_task(self, task_id: str, accepted: bool, comments: str = None): + """Allows you to accept or reject completed tasks. + Along with support for adding comments about the reason + for the given audit status, mirroring our Audit UI. + + Args: + task_id (str): + Task id + accepted (boolean): + Optional, additional feedback to record the reason + for the audit status + """ + + payload = dict(accepted=accepted, comments=comments) + endpoint = f"task/{task_id}/audit" + return self.api.post_request(endpoint, body=payload) + def update_task_unique_id(self, task_id: str, unique_id: str) -> Task: """Updates a task's unique_id and returns the associated task. Raises a ScaleDuplicateResource exception if unique_id diff --git a/scaleapi/_version.py b/scaleapi/_version.py index bcc5c8a..e2af5ab 100644 --- a/scaleapi/_version.py +++ b/scaleapi/_version.py @@ -1,2 +1,2 @@ -__version__ = "2.10.1" +__version__ = "2.11.0" __package_name__ = "scaleapi" diff --git a/scaleapi/tasks.py b/scaleapi/tasks.py index 6cc1513..8efa1b6 100644 --- a/scaleapi/tasks.py +++ b/scaleapi/tasks.py @@ -88,6 +88,10 @@ def cancel(self, clear_unique_id: bool = False): """Cancels the task""" self._client.cancel_task(self.id, clear_unique_id) + def audit(self, accepted: bool, comments: str = None): + """Submit an audit to a completed task""" + self._client.audit_task(self.id, accepted, comments) + def update_unique_id(self, unique_id: str): """Updates unique_id of a task""" self._client.update_task_unique_id(self.id, unique_id) From 77478362abe00f8d5f2ae85f0346b0738b5cfeb1 Mon Sep 17 00:00:00 2001 From: Fatih Kurtoglu Date: Tue, 13 Dec 2022 11:42:35 -0800 Subject: [PATCH 2/2] handle 200 responses without JSON data --- scaleapi/__init__.py | 2 +- scaleapi/api.py | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/scaleapi/__init__.py b/scaleapi/__init__.py index 082cfa7..cc5a58d 100644 --- a/scaleapi/__init__.py +++ b/scaleapi/__init__.py @@ -98,7 +98,7 @@ def audit_task(self, task_id: str, accepted: bool, comments: str = None): payload = dict(accepted=accepted, comments=comments) endpoint = f"task/{task_id}/audit" - return self.api.post_request(endpoint, body=payload) + self.api.post_request(endpoint, body=payload) def update_task_unique_id(self, task_id: str, unique_id: str) -> Task: """Updates a task's unique_id and returns the associated task. diff --git a/scaleapi/api.py b/scaleapi/api.py index 078f305..acc065e 100644 --- a/scaleapi/api.py +++ b/scaleapi/api.py @@ -108,7 +108,11 @@ def _api_request( json = None if res.status_code == 200: - json = res.json() + try: + json = res.json() + except ValueError: + # Some endpoints only return 'OK' message without JSON + return json elif res.status_code == 409 and "task" in endpoint and body.get("unique_id"): retry_history = res.raw.retries.history # Example RequestHistory tuple