10
10
from influxdb_client import TasksService , Task , TaskCreateRequest , TaskUpdateRequest , LabelResponse , LabelMapping , \
11
11
AddResourceMemberRequestBody , RunManually , Run , LogEvent
12
12
13
+ class TasksIterator :
14
+ def __init__ (self , values , next ) -> None :
15
+ self .values = values
16
+ self .next = next
17
+ self .no_values = False if values else True
18
+
19
+ def __iter__ (self ):
20
+ return self
21
+
22
+ def __next__ (self ):
23
+ if self .no_values :
24
+ raise StopIteration
25
+ if not self .values :
26
+ self .values , self .next = self .next ()
27
+ if not self .values :
28
+ raise StopIteration
29
+ return self .values .pop (0 )
30
+
13
31
14
32
class TasksApi (object ):
15
33
"""Implementation for '/api/v2/tasks' endpoint."""
@@ -25,7 +43,7 @@ def find_task_by_id(self, task_id) -> Task:
25
43
return task
26
44
27
45
def find_tasks (self , ** kwargs ):
28
- """List all tasks.
46
+ """List all tasks up to set limit (max 500) .
29
47
30
48
:key str name: only returns tasks with the specified name
31
49
:key str after: returns tasks after specified ID
@@ -37,6 +55,45 @@ def find_tasks(self, **kwargs):
37
55
"""
38
56
return self ._service .get_tasks (** kwargs ).tasks
39
57
58
+ def _find_tasks_paged (self , ** kwargs ):
59
+ """List all tasks with ability to list next tasks after limit.
60
+
61
+ :key str name: only returns tasks with the specified name
62
+ :key str after: returns tasks after specified ID
63
+ :key str user: filter tasks to a specific user ID
64
+ :key str org: filter tasks to a specific organization name
65
+ :key str org_id: filter tasks to a specific organization ID
66
+ :key int limit: the number of tasks to return in one page
67
+ :return: Tasks, Next
68
+ """
69
+ tasks = self ._service .get_tasks (** kwargs ).tasks
70
+
71
+ last_id = tasks [- 1 ].id if tasks else None
72
+ def next ():
73
+ if last_id is not None :
74
+ return self ._find_tasks_paged (** {** kwargs , 'after' : last_id })
75
+ else :
76
+ def func ():
77
+ raise Exception ("There are no additional pages remaining for tasks." )
78
+ return [], func
79
+
80
+ return tasks , next
81
+
82
+ def find_tasks_iter (self , ** kwargs ):
83
+ """Iterate over all tasks with pagination.
84
+
85
+ :key str name: only returns tasks with the specified name
86
+ :key str after: returns tasks after specified ID
87
+ :key str user: filter tasks to a specific user ID
88
+ :key str org: filter tasks to a specific organization name
89
+ :key str org_id: filter tasks to a specific organization ID
90
+ :key int limit: the number of tasks in one page
91
+ :return: Tasks iterator
92
+ """
93
+ tasks , next = self ._find_tasks_paged (** kwargs )
94
+
95
+ return iter (TasksIterator (tasks , next ))
96
+
40
97
def create_task (self , task : Task = None , task_create_request : TaskCreateRequest = None ) -> Task :
41
98
"""Create a new task."""
42
99
if task_create_request is not None :
0 commit comments