Skip to content

Commit 55c52c2

Browse files
Add task audit endpoints to SDK (#64)
* implement task audit api * handle 200 responses without JSON data
1 parent 9bc4fef commit 55c52c2

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

README.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,27 @@ __ https://docs.scale.com/reference#cancel-task
239239
task.cancel(clear_unique_id=True)
240240
241241
242+
Audit a Task
243+
^^^^^^^^^^^^
244+
245+
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.
246+
Check out `Scale's API documentation`__ for more information.
247+
248+
__ https://docs.scale.com/reference/audit-a-task
249+
250+
.. code-block :: python
251+
252+
# Accept a completed task by submitting an audit
253+
client.audit_task('30553edd0b6a93f8f05f0fee', True)
254+
255+
# Reject a completed task by submitting a comment with the audit
256+
client.audit_task('30553edd0b6a93f8f05f0fee', False, 'Rejected due to quality')
257+
258+
# audit() is also available on Task object
259+
task = client.get_task('30553edd0b6a93f8f05f0fee')
260+
task.audit(True)
261+
262+
242263
Update A Task's Unique Id
243264
^^^^^^^^^^^^^^^^^^^^^^^^^
244265

scaleapi/__init__.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ def cancel_task(self, task_id: str, clear_unique_id: bool = False) -> Task:
8383
endpoint = f"task/{task_id}/cancel"
8484
return Task(self.api.post_request(endpoint), self)
8585

86+
def audit_task(self, task_id: str, accepted: bool, comments: str = None):
87+
"""Allows you to accept or reject completed tasks.
88+
Along with support for adding comments about the reason
89+
for the given audit status, mirroring our Audit UI.
90+
91+
Args:
92+
task_id (str):
93+
Task id
94+
accepted (boolean):
95+
Optional, additional feedback to record the reason
96+
for the audit status
97+
"""
98+
99+
payload = dict(accepted=accepted, comments=comments)
100+
endpoint = f"task/{task_id}/audit"
101+
self.api.post_request(endpoint, body=payload)
102+
86103
def update_task_unique_id(self, task_id: str, unique_id: str) -> Task:
87104
"""Updates a task's unique_id and returns the associated task.
88105
Raises a ScaleDuplicateResource exception if unique_id

scaleapi/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "2.10.1"
1+
__version__ = "2.11.0"
22
__package_name__ = "scaleapi"

scaleapi/api.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,11 @@ def _api_request(
108108

109109
json = None
110110
if res.status_code == 200:
111-
json = res.json()
111+
try:
112+
json = res.json()
113+
except ValueError:
114+
# Some endpoints only return 'OK' message without JSON
115+
return json
112116
elif res.status_code == 409 and "task" in endpoint and body.get("unique_id"):
113117
retry_history = res.raw.retries.history
114118
# Example RequestHistory tuple

scaleapi/tasks.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ def cancel(self, clear_unique_id: bool = False):
8888
"""Cancels the task"""
8989
self._client.cancel_task(self.id, clear_unique_id)
9090

91+
def audit(self, accepted: bool, comments: str = None):
92+
"""Submit an audit to a completed task"""
93+
self._client.audit_task(self.id, accepted, comments)
94+
9195
def update_unique_id(self, unique_id: str):
9296
"""Updates unique_id of a task"""
9397
self._client.update_task_unique_id(self.id, unique_id)

0 commit comments

Comments
 (0)