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

Adds bonus history functionality to wrapper, increases page size #537

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 36 additions & 2 deletions psiturk/amt_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,8 @@ def get_assignments(self, assignment_status=None, hit_ids=None):
for hit_id in hit_ids:
paginator = self.mtc.get_paginator('list_assignments_for_hit')
args = dict(
HITId=hit_id
HITId=hit_id,
PaginationConfig={'PageSize':100}
)
if assignment_status:
args['AssignmentStatuses'] = [assignment_status]
Expand Down Expand Up @@ -220,6 +221,38 @@ def get_assignment(self, assignment_id):
'status': assignment['AssignmentStatus'],
}
return worker_data

@amt_service_response
def get_bonuses(self, hit_id=None, assignment_ids=None):
"""Get a list of paid bonuses."""
bonuses = []
if hit_id:
paginator = self.mtc.get_paginator('list_bonus_payments')
args = dict(
HITId=hit_id,
PaginationConfig={'PageSize':100}
)
for page in paginator.paginate(**args):
bonuses.extend(page['BonusPayments'])
elif assignment_ids:
if not isinstance(assignment_ids, list):
assignment_ids = [assignment_ids]
for assignment_id in assignment_ids:
paginator = self.mtc.get_paginator('list_bonus_payments')
args = dict(
AssignmentId=assignment_id,
PaginationConfig={'PageSize':100}
)
for page in paginator.paginate(**args):
bonuses.extend(page['BonusPayments'])
bonus_data = [{
'workerId': bonus['WorkerId'],
'bonusAmount': bonus['BonusAmount'],
'assignmentId': bonus['AssignmentId'],
'reason': bonus['Reason'],
'grantTime': bonus['GrantTime']
} for bonus in bonuses]
return bonus_data

@amt_service_response
def bonus_assignment(self, assignment_id, worker_id, amount, reason=""):
Expand Down Expand Up @@ -387,7 +420,8 @@ def list_qualification_types(self, Query='', MustBeRequestable=False,
qualification_types = []
kwargs = {
'MustBeRequestable': MustBeRequestable,
'MustBeOwnedByCaller': MustBeOwnedByCaller
'MustBeOwnedByCaller': MustBeOwnedByCaller,
'PaginationConfig': {'PageSize':100}
}
if Query:
kwargs['Query'] = Query
Expand Down
11 changes: 11 additions & 0 deletions psiturk/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,15 @@ def post(self):
raise APIException(message='task name `{}` not recognized!'.format(data['name']))


class BonusList(Resource):
def post(self):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be GET, not POST? my understanding of rest apis is that post is reserved for creation of new records, and GET for retrieval.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought about this for a bit, and it's very possible to make it into a GET request. However, we'd then be unable to query for multiple assignment IDs.

Actually, now that I think about it, I guess that that fits better with the MTurk boto3 client functionality itself. We'd just have to remove the multiple assignment ID workflows altogether. Will do when i find the time.

data = request.json
hit_id = data['hit_id'] if 'hit_id' in data else None
assignment_ids = data['assignment_ids'] if 'assignment_ids' in data else None
bonuses = services_manager.amt_services_wrapper.get_bonuses(
hit_id=hit_id, assignment_ids=assignment_ids).data
return bonuses, 201

api.add_resource(ServicesManager, '/services_manager', '/services_manager/')

api.add_resource(AssignmentList, '/assignments', '/assignments/')
Expand All @@ -293,4 +302,6 @@ def post(self):
api.add_resource(TaskList, '/tasks', '/tasks/')
api.add_resource(Tasks, '/tasks/<task_id>')

api.add_resource(BonusList, '/bonuses', '/bonuses/')

api.init_app(api_blueprint)