Actually push json parsing all the way down to the bottom of the GradingService get and post methods

This commit is contained in:
Calen Pennington
2014-03-28 15:26:19 -04:00
parent c9a5e9b88d
commit 0513e144b1
4 changed files with 35 additions and 41 deletions

View File

@@ -28,8 +28,7 @@ class ControllerQueryService(GradingService):
params = {
'location': location,
}
response = self.get(self.check_eta_url, params)
data = response.json()
data = self.get(self.check_eta_url, params)
self._record_result('check_for_eta', data)
dog_stats_api.histogram(self._metric_name('check_for_eta.eta'), data.get('eta', 0))
@@ -43,8 +42,7 @@ class ControllerQueryService(GradingService):
'last_time_viewed': last_time_viewed,
}
log.debug(self.combined_notifications_url)
response = self.get(self.combined_notifications_url, params)
data = response.json()
data = self.get(self.combined_notifications_url, params)
tags = [u'course_id:{}'.format(course_id), u'user_is_staff:{}'.format(user_is_staff)]
tags.extend(
@@ -61,8 +59,7 @@ class ControllerQueryService(GradingService):
'course_id': course_id,
}
response = self.get(self.grading_status_list_url, params)
data = response.json()
data = self.get(self.grading_status_list_url, params)
tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_grading_status_list', data, tags)
@@ -78,8 +75,7 @@ class ControllerQueryService(GradingService):
'course_id': course_id,
}
response = self.get(self.flagged_problem_list_url, params)
data = response.json()
data = self.get(self.flagged_problem_list_url, params)
tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_flagged_problem_list', data, tags)
@@ -97,8 +93,7 @@ class ControllerQueryService(GradingService):
'action_type': action_type
}
response = self.post(self.take_action_on_flags_url, params)
data = response.json()
data = self.post(self.take_action_on_flags_url, params)
tags = [u'course_id:{}'.format(course_id), u'action_type:{}'.format(action_type)]
self._record_result('take_action_on_flags', data, tags)

View File

@@ -73,38 +73,38 @@ class GradingService(object):
def post(self, url, data, allow_redirects=False):
"""
Make a post request to the grading controller
Make a post request to the grading controller. Returns the parsed json results of that request.
"""
try:
op = lambda: self.session.post(url, data=data,
allow_redirects=allow_redirects)
r = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err:
response_json = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError, ValueError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace.
#This is a dev_facing_error
error_string = "Problem posting data to the grading controller. URL: {0}, data: {1}".format(url, data)
log.error(error_string)
raise GradingServiceError(error_string)
return r.text
return response_json
def get(self, url, params, allow_redirects=False):
"""
Make a get request to the grading controller
Make a get request to the grading controller. Returns the parsed json results of that request.
"""
op = lambda: self.session.get(url,
allow_redirects=allow_redirects,
params=params)
try:
r = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err:
response_json = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError, ValueError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace.
#This is a dev_facing_error
error_string = "Problem getting data from the grading controller. URL: {0}, params: {1}".format(url, params)
log.error(error_string)
raise GradingServiceError(error_string)
return r.text
return response_json
def _try_with_login(self, operation):
"""
@@ -128,7 +128,7 @@ class GradingService(object):
response = operation()
response.raise_for_status()
return response
return resp_json
def _render_rubric(self, response, view_only=False):
"""

View File

@@ -31,8 +31,7 @@ class PeerGradingService(GradingService):
def get_data_for_location(self, problem_location, student_id):
params = {'location': problem_location, 'student_id': student_id}
response = self.get(self.get_data_for_location_url, params)
result = response.json()
result = self.get(self.get_data_for_location_url, params)
self._record_result('get_data_for_location', result)
for key in result.keys():
if key in ('success', 'error', 'version'):
@@ -45,27 +44,26 @@ class PeerGradingService(GradingService):
return result
def get_next_submission(self, problem_location, grader_id):
response = self.get(
result = self._render_rubric(self.get(
self.get_next_submission_url,
{
'location': problem_location,
'grader_id': grader_id
}
)
result = self._render_rubric(response.json())
))
self._record_result('get_next_submission', result)
return result
def save_grade(self, **kwargs):
data = kwargs
data.update({'rubric_scores_complete': True})
result = self.post(self.save_grade_url, data).json()
result = self.post(self.save_grade_url, data)
self._record_result('save_grade', result)
return result
def is_student_calibrated(self, problem_location, grader_id):
params = {'problem_id': problem_location, 'student_id': grader_id}
result = self.get(self.is_student_calibrated_url, params).json()
result = self.get(self.is_student_calibrated_url, params)
self._record_result(
'is_student_calibrated',
result,
@@ -75,22 +73,20 @@ class PeerGradingService(GradingService):
def show_calibration_essay(self, problem_location, grader_id):
params = {'problem_id': problem_location, 'student_id': grader_id}
response = self.get(self.show_calibration_essay_url, params)
result = self._render_rubric(response.json())
result = self._render_rubric(self.get(self.show_calibration_essay_url, params))
self._record_result('show_calibration_essay', result)
return result
def save_calibration_essay(self, **kwargs):
data = kwargs
data.update({'rubric_scores_complete': True})
result = self.post(self.save_calibration_essay_url, data).json()
result = self.post(self.save_calibration_essay_url, data)
self._record_result('show_calibration_essay', result)
return result
def get_problem_list(self, course_id, grader_id):
params = {'course_id': course_id, 'student_id': grader_id}
response = self.get(self.get_problem_list_url, params)
result = response.json()
result = self.get(self.get_problem_list_url, params)
self._record_result('get_problem_list', result)
dog_stats_api.histogram(
self._metric_name('get_problem_list.result.length'),
@@ -100,8 +96,7 @@ class PeerGradingService(GradingService):
def get_notifications(self, course_id, grader_id):
params = {'course_id': course_id, 'student_id': grader_id}
response = self.get(self.get_notifications_url, params)
result = response.json()
result = self.get(self.get_notifications_url, params)
self._record_result(
'get_notifications',
result,

View File

@@ -116,7 +116,7 @@ class StaffGradingService(GradingService):
GradingServiceError: something went wrong with the connection.
"""
params = {'course_id': course_id, 'grader_id': grader_id}
result = self.get(self.get_problem_list_url, params).json()
result = self.get(self.get_problem_list_url, params)
tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_problem_list', result, tags)
dog_stats_api.histogram(
@@ -143,10 +143,15 @@ class StaffGradingService(GradingService):
Raises:
GradingServiceError: something went wrong with the connection.
"""
response = self.get(self.get_next_url,
params={'location': location,
'grader_id': grader_id})
result = self._render_rubric(response.json())
result = self._render_rubric(
self.get(
self.get_next_url,
params={
'location': location,
'grader_id': grader_id
}
)
)
tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_next', result, tags)
return result
@@ -174,15 +179,14 @@ class StaffGradingService(GradingService):
'rubric_scores_complete': True,
'submission_flagged': submission_flagged}
response = self.post(self.save_grade_url, data=data)
result = self._render_rubric(response.json())
result = self._render_rubric(self.post(self.save_grade_url, data=data))
tags = [u'course_id:{}'.format(course_id)]
self._record_result('save_grade', result, tags)
return result
def get_notifications(self, course_id):
params = {'course_id': course_id}
result = self.get(self.get_notifications_url, params).json()
result = self.get(self.get_notifications_url, params)
tags = [
u'course_id:{}'.format(course_id),
u'staff_needs_to_grade:{}'.format(result.get('staff_needs_to_grade'))