Skip to content

Commit b621d5d

Browse files
committed
get request params, testing
1 parent 493fc39 commit b621d5d

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

scaleapi/__init__.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def __init__(self, api_key, endpoint='https://api.scaleapi.com/v1/'):
3838
self.api_key = api_key
3939
self.endpoint = endpoint
4040

41-
def _getrequest(self, endpoint):
41+
def _getrequest(self, endpoint, params={}):
4242
"""Makes a get request to an endpoint.
4343
4444
If an error occurs, assumes that endpoint returns JSON as:
@@ -47,7 +47,7 @@ def _getrequest(self, endpoint):
4747
"""
4848
r = requests.get(self.endpoint + endpoint,
4949
headers={"Content-Type": "application/json"},
50-
auth=(self.api_key, ''))
50+
auth=(self.api_key, ''), params=params)
5151

5252
if r.status_code == 200:
5353
return r.json()
@@ -102,7 +102,7 @@ def tasks(self, **kwargs):
102102
raise ScaleInvalidRequest('Illegal parameter %s for ScaleClient.tasks()'
103103
% key, None)
104104
return [Task(json, self) for json in
105-
self._getrequest('tasks', payload=kwargs)['docs']]
105+
self._getrequest('tasks', params=kwargs)['docs']]
106106

107107
def create_categorization_task(self, **kwargs):
108108
validate_payload('categorization', kwargs)

scaleapi/tasks.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@ def __init__(self, param_dict, client):
88
def __getattr__(self, name):
99
if name in self.param_dict:
1010
return self.param_dict[name]
11+
if name in self.params:
12+
return self.params[name]
1113
raise AttributeError("'%s' object has no attribute %s"
1214
% (type(self).__name__, name))
1315

16+
def __hash__(self):
17+
return hash(self.id)
18+
1419
def __str__(self):
1520
return 'Task(id=%s)' % self.id
1621

tests/test_client.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,52 @@ def test_cancel():
116116
# raises a scaleexception, because test tasks complete instantly
117117
with pytest.raises(scaleapi.ScaleException):
118118
task.cancel()
119+
120+
121+
def test_task_retrieval():
122+
task = client.create_annotation_task(
123+
callback_url='http://www.example.com/callback',
124+
instruction='Draw a box around each **baby cow** and **big cow**',
125+
attachment_type='image',
126+
attachment='http://i.imgur.com/v4cBreD.jpg',
127+
objects_to_annotate=['baby cow', 'big cow'],
128+
with_labels=True)
129+
130+
task2 = client.fetch_task(task.id)
131+
assert task.status == 'pending'
132+
assert task2.status == 'completed'
133+
assert task2.id == task.id
134+
assert task2.callback_url == task.callback_url
135+
assert task2.instruction == task.instruction
136+
assert task2.attachment_type == task.attachment_type
137+
assert task2.attachment == task.attachment
138+
assert task2.objects_to_annotate == task.objects_to_annotate
139+
assert task2.with_labels == task.with_labels
140+
assert task2.metadata == task.metadata
141+
assert task2.type == task.type
142+
assert task2.created_at == task.created_at
143+
144+
145+
def test_task_retrieval_fail():
146+
with pytest.raises(scaleapi.ScaleException):
147+
client.fetch_task('fake_id_qwertyuiop')
148+
149+
150+
def test_tasks():
151+
tasks = []
152+
for i in range(3):
153+
tasks.append(client.create_annotation_task(
154+
callback_url='http://www.example.com/callback',
155+
instruction='Draw a box around each **baby cow** and **big cow**',
156+
attachment_type='image',
157+
attachment='http://i.imgur.com/v4cBreD.jpg',
158+
objects_to_annotate=['baby cow', 'big cow'],
159+
with_labels=True))
160+
task_ids = {task.id for task in tasks}
161+
for task in client.tasks(limit=3):
162+
assert task.id in task_ids
163+
164+
165+
def test_tasks_invalid():
166+
with pytest.raises(scaleapi.ScaleException):
167+
client.tasks(bogus=0)

0 commit comments

Comments
 (0)