Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

newtask endpoint for pbclient API #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion pbclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -431,9 +431,9 @@ def get_tasks(project_id, limit=100, offset=0, last_id=None):
:param limit: Number of returned items, default 100
:type limit: integer
:param offset: Offset for the query, default 0
:type offset: integer
:param last_id: id of the last task, used for pagination. If provided, offset is ignored
:type last_id: integer
:type offset: integer
:returns: True -- the response status code

"""
Expand All @@ -454,6 +454,34 @@ def get_tasks(project_id, limit=100, offset=0, last_id=None):
raise


def get_new_task(project_id, limit=1, offset=0, last_id=None):
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I decided to drop params: limit, offset and last_id. These parameters are relevant only for getting multiple tasks. It just doesn't apply when you want to get a random task.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need your opinion on this one: is it desired use case to send multiple new tasks at once?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. It will be nice to add it. Basically think about this of getting a bunch of tasks for your user with only one request. It will save time and you can cache them.

"""Return a new task or list of new tasks for a given project ID.
:param project_id: PYBOSSA Project ID
:type project_id: integer
:param limit: Number of returned items, default 1
:type limit: integer
:param offset: Offset for the query, default 0
:type offset: integer
:param last_id: id of the last task, used for pagination. If provided, offset is ignored
:type last_id: integer
:returns: True -- the response status code
"""
if last_id is not None:
params = dict(limit=limit, last_id=last_id)
else:
params = dict(limit=limit, offset=offset)
print(OFFSET_WARNING)
try:
res = _pybossa_req('get', 'project/{0}/newtask'.format(project_id),
params=params)
if type(res).__name__ == 'list':
return [Task(task) for task in res]
else:
return res
except: # pragma: no cover
raise


def find_tasks(project_id, **kwargs):
"""Return a list of matched tasks for a given project ID.

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='pybossa-client',
version='2.0.1',
version='2.0.2',
packages=find_packages(),
install_requires=['requests>=0.13.0'],
# metadata for upload to PyPI
Expand Down
39 changes: 39 additions & 0 deletions test/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,45 @@ def test_get_tasks_errors(self, Mock):
err = self.client.get_tasks(1)
self.check_error_output(err_output, err)

@patch('pbclient.requests.get')
def test_get_new_task(self, Mock):
"""Test get_new_tasks works"""
Mock.return_value = self.create_fake_request([self.task.copy()], 200)
res = self.client.get_new_task(1)
assert len(res) == 1, len(res)
task = res[0]
assert task.id == self.task['id'], task
assert task.project_id == self.task['project_id'], task

@patch('pbclient.requests.get')
def test_get_tasks_with_keyset_pagination(self, Mock):
"""Test get_new_task uses keyset pagination if a last_id argument is
provided"""
Mock.return_value = self.create_fake_request([], 200)
self.client.get_new_task(1, last_id=1, limit=3)

Mock.assert_called_once_with('http://localhost:5000/api/project/1/newtask',
params={'api_key': 'tester',
'limit': 3,
'last_id': 1})

@patch('pbclient.requests.get')
def test_get_tasks_errors(self, Mock):
"""Test get new task errors works"""
targets = ['newtask']
errors = {'Unauthorized': 401, 'NotFound': 404, 'Forbidden': 401,
'TypeError': 415}
for target in targets:
for error in errors.keys():
err_output = self.create_error_output(action='GET',
status_code=errors[error],
target=target,
exception_cls=error)
Mock.return_value = self.create_fake_request(err_output,
errors[error])
err = self.client.get_new_task(1)
self.check_error_output(err_output, err)

@patch('pbclient.requests.get')
def test_find_tasks(self, Mock):
"""Test find_tasks works"""
Expand Down