Skip to content

Commit abba827

Browse files
authored
Merge pull request #8 from scaleapi/un-fragile
Refactor to make less fragile and more DRY
2 parents b73d6a1 + 11a1b24 commit abba827

File tree

1 file changed

+22
-82
lines changed

1 file changed

+22
-82
lines changed

scaleapi/__init__.py

Lines changed: 22 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,43 +3,23 @@
33

44
from .tasks import Task
55

6-
DEFAULT_FIELDS = {'callback_url', 'instruction', 'urgency', 'metadata'}
7-
ALLOWED_FIELDS = {'categorization': {'attachment', 'attachment_type', 'categories',
8-
'category_ids', 'allow_multiple', 'layers'},
9-
'transcription': {'attachment', 'attachment_type',
10-
'fields', 'repeatable_fields'},
11-
'phonecall': {'attachment', 'attachment_type', 'phone_number',
12-
'script', 'entity_name', 'fields', 'choices'},
13-
'comparison': {'attachments', 'attachment_type',
14-
'fields', 'choices'},
15-
'annotation': {'attachment', 'attachment_type', 'instruction',
16-
'objects_to_annotate', 'with_labels', 'examples',
17-
'min_width', 'min_height', 'layers'},
18-
'polygonannotation': {'attachment', 'attachment_type', 'instruction',
19-
'objects_to_annotate', 'with_labels', 'layers'},
20-
'cuboidannotation': {'attachment', 'attachment_type', 'instruction',
21-
'objects_to_annotate', 'min_width', 'min_height', 'with_labels', 'layers'},
22-
'audiotranscription': {'attachment', 'attachment_type', 'verbatim', 'phrases'},
23-
'annotation': {'attachment', 'attachment_type', 'instruction', 'objects_to_annotate', 'with_labels', 'examples', 'min_width', 'min_height', 'layers', 'annotation_attributes'},
24-
'polygonannotation': {'attachment', 'attachment_type', 'instruction', 'objects_to_annotate', 'with_labels', 'examples', 'layers', 'annotation_attributes'},
25-
'lineannotation':
26-
{'attachment', 'attachment_type', 'instruction', 'objects_to_annotate', 'with_labels', 'examples', 'splines', 'layers', 'annotation_attributes'},
27-
'datacollection': {'attachment', 'attachment_type', 'fields'},
28-
'pointannotation': {'attachment_type','attachment', 'objects_to_annotate','with_labels', 'examples', 'layers','annotation_attributes'},
29-
'segmentannotation': {'attachment_type','attachment', 'labels', 'allow_unlabeled'}}
6+
TASK_TYPES = [
7+
'categorization',
8+
'transcription',
9+
'phonecall',
10+
'comparison',
11+
'annotation',
12+
'polygonannotation',
13+
'lineannotation',
14+
'datacollection',
15+
'audiotranscription',
16+
'pointannotation',
17+
'segmentannotation'
18+
]
3019
SCALE_ENDPOINT = 'https://api.scaleapi.com/v1/'
3120
DEFAULT_LIMIT = 100
3221
DEFAULT_OFFSET = 0
3322

34-
35-
def validate_payload(task_type, kwargs):
36-
allowed_fields = DEFAULT_FIELDS | ALLOWED_FIELDS[task_type]
37-
for k in kwargs:
38-
if k not in allowed_fields:
39-
raise ScaleInvalidRequest('Illegal parameter %s for task_type %s'
40-
% (k, task_type), None)
41-
42-
4323
class ScaleException(Exception):
4424
def __init__(self, message, errcode):
4525
super(ScaleException, self).__init__(message)
@@ -131,56 +111,16 @@ def tasks(self, **kwargs):
131111
docs = [Task(json, self) for json in response['docs']]
132112
return Tasklist(docs, response['total'], response['limit'],
133113
response['offset'], response['has_more'])
134-
135-
def create_categorization_task(self, **kwargs):
136-
validate_payload('categorization', kwargs)
137-
taskdata = self._postrequest('task/categorize', payload=kwargs)
138-
return Task(taskdata, self)
139-
140-
def create_transcription_task(self, **kwargs):
141-
validate_payload('transcription', kwargs)
142-
taskdata = self._postrequest('task/transcription', payload=kwargs)
143-
return Task(taskdata, self)
144-
145-
def create_phonecall_task(self, **kwargs):
146-
raise ScaleException('Phone call tasks have been deprecated and are no longer available.', 400)
147-
148-
def create_comparison_task(self, **kwargs):
149-
validate_payload('comparison', kwargs)
150-
taskdata = self._postrequest('task/comparison', payload=kwargs)
151-
return Task(taskdata, self)
152-
153-
def create_annotation_task(self, **kwargs):
154-
validate_payload('annotation', kwargs)
155-
taskdata = self._postrequest('task/annotation', payload=kwargs)
156-
return Task(taskdata, self)
157-
158-
def create_polygonannotation_task(self, **kwargs):
159-
validate_payload('polygonannotation', kwargs)
160-
taskdata = self._postrequest('task/polygonannotation', payload=kwargs)
161-
return Task(taskdata, self)
162-
163-
def create_lineannotation_task(self, **kwargs):
164-
validate_payload('lineannotation', kwargs)
165-
taskdata = self._postrequest('task/lineannotation', payload=kwargs)
114+
def create_task(self, task_type, **kwargs):
115+
endpoint = 'task/' + task_type
116+
taskdata = self._postrequest(endpoint, payload=kwargs)
166117
return Task(taskdata, self)
167118

168-
def create_datacollection_task(self, **kwargs):
169-
validate_payload('datacollection', kwargs)
170-
taskdata = self._postrequest('task/datacollection', payload=kwargs)
171-
return Task(taskdata, self)
172119

173-
def create_audiotranscription_task(self, **kwargs):
174-
validate_payload('audiotranscription', kwargs)
175-
taskdata = self._postrequest('task/audiotranscription', payload=kwargs)
176-
return Task(taskdata, self)
120+
def _AddTaskTypeCreator(task_type):
121+
def create_task_wrapper(self, **kwargs):
122+
return self.create_task(task_type, **kwargs)
123+
setattr(ScaleClient, 'create_' + task_type + '_task', create_task_wrapper)
177124

178-
def create_pointannotation_task(self, **kwargs):
179-
validate_payload('pointannotation', kwargs)
180-
taskdata = self._postrequest('task/pointannotation', payload=kwargs)
181-
return Task(taskdata, self)
182-
183-
def create_segmentannotation_task(self, **kwargs):
184-
validate_payload('segmentannotation', kwargs)
185-
taskdata = self._postrequest('task/segmentannotation', payload=kwargs)
186-
return Task(taskdata, self)
125+
for taskType in TASK_TYPES:
126+
_AddTaskTypeCreator(taskType)

0 commit comments

Comments
 (0)