Skip to content

Commit 195b96a

Browse files
Update cancel_task to add clearUniqueId option (#53)
* Update cancel_task to add clearUniqueId option * new endpoints and readme updates Co-authored-by: Fatih Kurtoglu <[email protected]>
1 parent b59920f commit 195b96a

File tree

6 files changed

+121
-11
lines changed

6 files changed

+121
-11
lines changed

README.rst

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,49 @@ __ https://docs.scale.com/reference#cancel-task
228228
229229
task = client.cancel_task('30553edd0b6a93f8f05f0fee')
230230
231+
# If you also want to clear 'unique_id' of a task while canceling
232+
task = client.cancel_task('30553edd0b6a93f8f05f0fee', clear_unique_id=True)
233+
234+
# cancel() is also available on task object
235+
task = client.get_task('30553edd0b6a93f8f05f0fee')
236+
task.cancel()
237+
238+
# If you also want to clear 'unique_id' of a task while canceling
239+
task.cancel(clear_unique_id=True)
240+
241+
242+
Update A Task's Unique Id
243+
^^^^^^^^^^^^^^^^^^^^^^^^^
244+
245+
Update a given task's unique_id. Check out `Scale's API documentation`__ for more information.
246+
247+
__ https://docs.scale.com/reference/update-task-unique-id
248+
249+
.. code-block :: python
250+
251+
task = client.update_task_unique_id('30553edd0b6a93f8f05f0fee', "new_unique_id")
252+
253+
# update_unique_id() is also available on task object
254+
task = client.get_task('30553edd0b6a93f8f05f0fee')
255+
task.update_unique_id("new_unique_id")
256+
257+
258+
Clear A Task's Unique Id
259+
^^^^^^^^^^^^^^^^^^^^^^^^^
260+
261+
Clear a given task's unique_id. Check out `Scale's API documentation`__ for more information.
262+
263+
__ https://docs.scale.com/reference/delete-task-unique-id
264+
265+
.. code-block :: python
266+
267+
task = client.clear_task_unique_id('30553edd0b6a93f8f05f0fee')
268+
269+
# clear_unique_id() is also available on task object
270+
task = client.get_task('30553edd0b6a93f8f05f0fee')
271+
task.clear_unique_id()
272+
273+
231274
Batches
232275
_______
233276

@@ -351,7 +394,7 @@ __ https://docs.scale.com/reference#project-creation
351394
352395
print(project.name) # Test_Project
353396
354-
Specify ``rapid=true`` for Rapid projects and ``studio=true`` for Studio projects. Throws ``ScaleDuplicateResource`` exception if a project with the same name already exists.
397+
Specify ``rapid=true`` for Rapid projects and ``studio=true`` for Studio projects. Throws ``ScaleDuplicateResource`` exception if a project with the same name already exists.
355398

356399
Retrieve Project
357400
^^^^^^^^^^^^^^^^

scaleapi/__init__.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,20 +64,56 @@ def get_task(self, task_id: str) -> Task:
6464
endpoint = f"task/{task_id}"
6565
return Task(self.api.get_request(endpoint), self)
6666

67-
def cancel_task(self, task_id: str) -> Task:
67+
def cancel_task(self, task_id: str, clear_unique_id: bool = False) -> Task:
6868
"""Cancels a task and returns the associated task.
6969
Raises a ScaleException if it has already been canceled.
7070
7171
Args:
7272
task_id (str):
7373
Task id
74+
clear_unique_id (boolean):
75+
Option to clear unique id when the task is deleted
7476
7577
Returns:
7678
Task
7779
"""
78-
endpoint = f"task/{task_id}/cancel"
80+
if clear_unique_id:
81+
endpoint = f"task/{task_id}/cancel?clear_unique_id=true"
82+
else:
83+
endpoint = f"task/{task_id}/cancel"
7984
return Task(self.api.post_request(endpoint), self)
8085

86+
def update_task_unique_id(self, task_id: str, unique_id: str) -> Task:
87+
"""Updates a task's unique_id and returns the associated task.
88+
Raises a ScaleDuplicateResource exception if unique_id
89+
is already in use.
90+
91+
Args:
92+
task_id (str):
93+
Task id
94+
unique_id (str):
95+
unique_id to set
96+
97+
Returns:
98+
Task
99+
"""
100+
payload = dict(unique_id=unique_id)
101+
endpoint = f"task/{task_id}/unique_id"
102+
return Task(self.api.post_request(endpoint, body=payload), self)
103+
104+
def clear_task_unique_id(self, task_id: str) -> Task:
105+
"""Clears a task's unique_id and returns the associated task.
106+
107+
Args:
108+
task_id (str):
109+
Task id
110+
111+
Returns:
112+
Task
113+
"""
114+
endpoint = f"task/{task_id}/unique_id"
115+
return Task(self.api.delete_request(endpoint), self)
116+
81117
def tasks(self, **kwargs) -> Tasklist:
82118
"""Returns a list of your tasks.
83119
Returns up to 100 at a time, to get more, use the

scaleapi/_version.py

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

scaleapi/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
HTTP_TOTAL_RETRIES = 3 # Number of total retries
1414
HTTP_RETRY_BACKOFF_FACTOR = 2 # Wait 1, 2, 4 seconds between retries
1515
HTTP_STATUS_FORCE_LIST = [408, 429] + list(range(500, 531))
16-
HTTP_RETRY_ALLOWED_METHODS = frozenset({"GET", "POST"})
16+
HTTP_RETRY_ALLOWED_METHODS = frozenset({"GET", "POST", "DELETE"})
1717

1818

1919
class Api:
@@ -134,6 +134,12 @@ def post_request(self, endpoint, body=None, files=None, data=None):
134134
data=data,
135135
)
136136

137+
def delete_request(self, endpoint, params=None):
138+
"""Generic DELETE Request Wrapper"""
139+
return self._api_request(
140+
"DELETE", endpoint, headers=self._headers, auth=self._auth, params=params
141+
)
142+
137143
@staticmethod
138144
def _generate_useragent(extension: str = None) -> str:
139145
"""Generates UserAgent parameter with module, Python

scaleapi/tasks.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ def refresh(self):
8484
"""Refreshes the task details."""
8585
self._json = self._client.get_task(self.id).as_dict()
8686

87-
def cancel(self):
87+
def cancel(self, clear_unique_id: bool = False):
8888
"""Cancels the task"""
89-
self._client.cancel_task(self.id)
89+
self._client.cancel_task(self.id, clear_unique_id)
90+
91+
def update_unique_id(self, unique_id: str):
92+
"""Updates unique_id of a task"""
93+
self._client.update_task_unique_id(self.id, unique_id)
94+
95+
def clear_unique_id(self):
96+
"""Clears unique_id of a task"""
97+
self._client.clear_task_unique_id(self.id)

tests/test_client.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,28 @@ def make_a_task(unique_id: str = None, batch: str = None):
6969
return client.create_task(TaskType.ImageAnnotation, **args)
7070

7171

72-
def test_uniquekey_fail():
73-
unique_key = str(uuid.uuid4())
74-
make_a_task(unique_key)
72+
def test_unique_id_fail():
73+
unique_id = str(uuid.uuid4())
74+
make_a_task(unique_id)
7575
with pytest.raises(ScaleDuplicateResource):
76-
make_a_task(unique_key)
76+
make_a_task(unique_id)
77+
78+
79+
def test_update_unique_id():
80+
unique_id = str(uuid.uuid4())
81+
task = make_a_task(unique_id)
82+
unique_id_new = str(uuid.uuid4())
83+
84+
task = client.update_task_unique_id(task.id, unique_id_new)
85+
assert unique_id_new == task.as_dict()["unique_id"]
86+
87+
88+
def test_clear_unique_id():
89+
unique_id = str(uuid.uuid4())
90+
task = make_a_task(unique_id)
91+
92+
task = client.clear_task_unique_id(task.id)
93+
assert "unique_id" not in task.as_dict()
7794

7895

7996
def test_categorize_ok():

0 commit comments

Comments
 (0)