Skip to content

Add task audit endpoints to SDK #64

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
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
21 changes: 21 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^

Expand Down
17 changes: 17 additions & 0 deletions scaleapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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
Expand Down
2 changes: 1 addition & 1 deletion scaleapi/_version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version__ = "2.10.1"
__version__ = "2.11.0"
__package_name__ = "scaleapi"
6 changes: 5 additions & 1 deletion scaleapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions scaleapi/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down