1818)
1919from taskbadger .internal .models import (
2020 PatchedTaskRequest ,
21+ PatchedTaskRequestTags ,
2122 StatusEnum ,
2223 TaskRequest ,
24+ TaskRequestTags ,
2325)
2426from taskbadger .internal .types import UNSET
2527from taskbadger .mug import Badger , Session , Settings
@@ -95,6 +97,7 @@ def create_task(
9597 stale_timeout : int = None ,
9698 actions : list [Action ] = None ,
9799 monitor_id : str = None ,
100+ tags : dict [str , str ] = None ,
98101) -> "Task" :
99102 """Create a Task.
100103
@@ -108,6 +111,7 @@ def create_task(
108111 stale_timeout: Maximum allowed time between updates (seconds).
109112 actions: Task actions.
110113 monitor_id: ID of the monitor to associate this task with.
114+ tags: Dictionary of namespace -> value tags.
111115
112116 Returns:
113117 Task: The created Task object.
@@ -132,6 +136,8 @@ def create_task(
132136 task .data = {** scope_data , ** data }
133137 if actions :
134138 task .additional_properties = {"actions" : [a .to_dict () for a in actions ]}
139+ if tags :
140+ task .tags = TaskRequestTags .from_dict (tags )
135141 kwargs = _make_args (body = task )
136142 if monitor_id :
137143 kwargs ["x_taskbadger_monitor" ] = monitor_id
@@ -151,6 +157,7 @@ def update_task(
151157 max_runtime : int = None ,
152158 stale_timeout : int = None ,
153159 actions : list [Action ] = None ,
160+ tags : dict [str , str ] = None ,
154161) -> "Task" :
155162 """Update a task.
156163 Requires only the task ID and fields to update.
@@ -165,6 +172,7 @@ def update_task(
165172 max_runtime: Maximum expected runtime (seconds).
166173 stale_timeout: Maximum allowed time between updates (seconds).
167174 actions: Task actions.
175+ tags: Dictionary of namespace -> value tags.
168176
169177 Returns:
170178 Task: The updated Task object.
@@ -189,6 +197,8 @@ def update_task(
189197 )
190198 if actions :
191199 body .additional_properties = {"actions" : [a .to_dict () for a in actions ]}
200+ if tags :
201+ body .tags = PatchedTaskRequestTags .from_dict (tags )
192202 kwargs = _make_args (id = task_id , body = body )
193203 with Session () as client :
194204 response = task_partial_update .sync_detailed (client = client , ** kwargs )
@@ -245,6 +255,7 @@ def create(
245255 stale_timeout : int = None ,
246256 actions : list [Action ] = None ,
247257 monitor_id : str = None ,
258+ tags : dict [str , str ] = None ,
248259 ) -> "Task" :
249260 """Create a new task"""
250261 return create_task (
@@ -257,6 +268,7 @@ def create(
257268 stale_timeout = stale_timeout ,
258269 actions = actions ,
259270 monitor_id = monitor_id ,
271+ tags = tags ,
260272 )
261273
262274 def __init__ (self , task ):
@@ -321,6 +333,7 @@ def update(
321333 max_runtime : int = None ,
322334 stale_timeout : int = None ,
323335 actions : list [Action ] = None ,
336+ tags : dict [str , str ] = None ,
324337 data_merge_strategy : Any = None ,
325338 ):
326339 """Generic update method used to update any of the task fields.
@@ -345,13 +358,18 @@ def update(
345358 max_runtime = max_runtime ,
346359 stale_timeout = stale_timeout ,
347360 actions = actions ,
361+ tags = tags ,
348362 )
349363 self ._task = task ._task
350364
351365 def add_actions (self , actions : list [Action ]):
352366 """Add actions to the task."""
353367 self .update (actions = actions )
354368
369+ def tag (self , tags : dict [str , str ]):
370+ """Add tags to the task."""
371+ self .update (tags = tags )
372+
355373 def ping (self ):
356374 """Update the task without changing any values. This can be used in conjunction
357375 with 'stale_timeout' to indicate that the task is still running."""
0 commit comments