2
2
import requests
3
3
4
4
from .tasks import Task
5
+ from .batches import Batch
5
6
6
7
TASK_TYPES = [
7
8
'annotation' ,
10
11
'comparison' ,
11
12
'cuboidannotation' ,
12
13
'datacollection' ,
13
- 'imageannotation' ,
14
+ 'imageannotation' ,
14
15
'lineannotation' ,
15
16
'namedentityrecognition' ,
16
17
'pointannotation' ,
17
18
'polygonannotation' ,
18
19
'segmentannotation' ,
19
20
'transcription' ,
20
- 'videoannotation' ,
21
- 'videoboxannotation' ,
21
+ 'videoannotation' ,
22
+ 'videoboxannotation' ,
22
23
'videocuboidannotation'
23
24
]
24
25
SCALE_ENDPOINT = 'https://api.scale.com/v1/'
@@ -35,27 +36,37 @@ class ScaleInvalidRequest(ScaleException, ValueError):
35
36
pass
36
37
37
38
38
- class Tasklist (list ):
39
+ class Paginator (list ):
39
40
def __init__ (self , docs , total , limit , offset , has_more , next_token = None ):
40
- super (Tasklist , self ).__init__ (docs )
41
+ super (Paginator , self ).__init__ (docs )
41
42
self .docs = docs
42
43
self .total = total
43
44
self .limit = limit
44
45
self .offset = offset
45
46
self .has_more = has_more
46
47
self .next_token = next_token
47
48
49
+
50
+ class Tasklist (Paginator ):
51
+ pass
52
+
53
+
54
+ class Batchlist (Paginator ):
55
+ pass
56
+
57
+
48
58
class ScaleClient (object ):
49
59
def __init__ (self , api_key ):
50
60
self .api_key = api_key
51
61
52
- def _getrequest (self , endpoint , params = {} ):
62
+ def _getrequest (self , endpoint , params = None ):
53
63
"""Makes a get request to an endpoint.
54
64
55
65
If an error occurs, assumes that endpoint returns JSON as:
56
66
{ 'status_code': XXX,
57
67
'error': 'I failed' }
58
68
"""
69
+ params = params or {}
59
70
r = requests .get (SCALE_ENDPOINT + endpoint ,
60
71
headers = {"Content-Type" : "application/json" },
61
72
auth = (self .api_key , '' ), params = params )
@@ -114,7 +125,7 @@ def cancel_task(self, task_id):
114
125
def tasks (self , ** kwargs ):
115
126
"""Returns a list of your tasks.
116
127
Returns up to 100 at a time, to get more, use the next_token param passed back.
117
-
128
+
118
129
Note that offset is deprecated.
119
130
120
131
start/end_time are ISO8601 dates, the time range of tasks to fetch.
@@ -125,7 +136,7 @@ def tasks(self, **kwargs):
125
136
offset (deprecated) is the number of results to skip (for showing more pages).
126
137
"""
127
138
allowed_kwargs = {'start_time' , 'end_time' , 'status' , 'type' , 'project' ,
128
- 'batch' , 'limit' , 'offset' , 'completed_before' , 'completed_after' ,
139
+ 'batch' , 'limit' , 'offset' , 'completed_before' , 'completed_after' ,
129
140
'next_token' }
130
141
for key in kwargs :
131
142
if key not in allowed_kwargs :
@@ -140,6 +151,29 @@ def create_task(self, task_type, **kwargs):
140
151
taskdata = self ._postrequest (endpoint , payload = kwargs )
141
152
return Task (taskdata , self )
142
153
154
+ def create_batch (self , project , batch_name , callback ):
155
+ payload = dict (project = project , name = batch_name , callback = callback )
156
+ batchdata = self ._postrequest ('batches' , payload )
157
+ return Batch (batchdata , self )
158
+
159
+ def get_batch (self , batch_name : str ):
160
+ batchdata = self ._getrequest ('batches/%s' % batch_name )
161
+ return Batch (batchdata , self )
162
+
163
+ def list_batches (self , ** kwargs ):
164
+ allowed_kwargs = { 'start_time' , 'end_time' , 'status' , 'project' ,
165
+ 'batch' , 'limit' , 'offset' , }
166
+ for key in kwargs :
167
+ if key not in allowed_kwargs :
168
+ raise ScaleInvalidRequest ('Illegal parameter %s for ScaleClient.tasks()'
169
+ % key , None )
170
+ response = self ._getrequest ('tasks' , params = kwargs )
171
+ docs = [Batch (doc , self ) for doc in response ['docs' ]]
172
+ return Batchlist (
173
+ docs , response ['total' ], response ['limit' ], response ['offset' ],
174
+ response ['has_more' ], response .get ('next_token' ),
175
+ )
176
+
143
177
144
178
def _AddTaskTypeCreator (task_type ):
145
179
def create_task_wrapper (self , ** kwargs ):
0 commit comments