Commit 0513e144 by Calen Pennington

Actually push json parsing all the way down to the bottom of the GradingService…

Actually push json parsing all the way down to the bottom of the GradingService get and post methods
parent c9a5e9b8
...@@ -28,8 +28,7 @@ class ControllerQueryService(GradingService): ...@@ -28,8 +28,7 @@ class ControllerQueryService(GradingService):
params = { params = {
'location': location, 'location': location,
} }
response = self.get(self.check_eta_url, params) data = self.get(self.check_eta_url, params)
data = response.json()
self._record_result('check_for_eta', data) self._record_result('check_for_eta', data)
dog_stats_api.histogram(self._metric_name('check_for_eta.eta'), data.get('eta', 0)) dog_stats_api.histogram(self._metric_name('check_for_eta.eta'), data.get('eta', 0))
...@@ -43,8 +42,7 @@ class ControllerQueryService(GradingService): ...@@ -43,8 +42,7 @@ class ControllerQueryService(GradingService):
'last_time_viewed': last_time_viewed, 'last_time_viewed': last_time_viewed,
} }
log.debug(self.combined_notifications_url) log.debug(self.combined_notifications_url)
response = self.get(self.combined_notifications_url, params) data = self.get(self.combined_notifications_url, params)
data = response.json()
tags = [u'course_id:{}'.format(course_id), u'user_is_staff:{}'.format(user_is_staff)] tags = [u'course_id:{}'.format(course_id), u'user_is_staff:{}'.format(user_is_staff)]
tags.extend( tags.extend(
...@@ -61,8 +59,7 @@ class ControllerQueryService(GradingService): ...@@ -61,8 +59,7 @@ class ControllerQueryService(GradingService):
'course_id': course_id, 'course_id': course_id,
} }
response = self.get(self.grading_status_list_url, params) data = self.get(self.grading_status_list_url, params)
data = response.json()
tags = [u'course_id:{}'.format(course_id)] tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_grading_status_list', data, tags) self._record_result('get_grading_status_list', data, tags)
...@@ -78,8 +75,7 @@ class ControllerQueryService(GradingService): ...@@ -78,8 +75,7 @@ class ControllerQueryService(GradingService):
'course_id': course_id, 'course_id': course_id,
} }
response = self.get(self.flagged_problem_list_url, params) data = self.get(self.flagged_problem_list_url, params)
data = response.json()
tags = [u'course_id:{}'.format(course_id)] tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_flagged_problem_list', data, tags) self._record_result('get_flagged_problem_list', data, tags)
...@@ -97,8 +93,7 @@ class ControllerQueryService(GradingService): ...@@ -97,8 +93,7 @@ class ControllerQueryService(GradingService):
'action_type': action_type 'action_type': action_type
} }
response = self.post(self.take_action_on_flags_url, params) data = self.post(self.take_action_on_flags_url, params)
data = response.json()
tags = [u'course_id:{}'.format(course_id), u'action_type:{}'.format(action_type)] tags = [u'course_id:{}'.format(course_id), u'action_type:{}'.format(action_type)]
self._record_result('take_action_on_flags', data, tags) self._record_result('take_action_on_flags', data, tags)
......
...@@ -73,38 +73,38 @@ class GradingService(object): ...@@ -73,38 +73,38 @@ class GradingService(object):
def post(self, url, data, allow_redirects=False): 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: try:
op = lambda: self.session.post(url, data=data, op = lambda: self.session.post(url, data=data,
allow_redirects=allow_redirects) allow_redirects=allow_redirects)
r = self._try_with_login(op) response_json = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err: except (RequestException, ConnectionError, HTTPError, ValueError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace. # reraise as promised GradingServiceError, but preserve stacktrace.
#This is a dev_facing_error #This is a dev_facing_error
error_string = "Problem posting data to the grading controller. URL: {0}, data: {1}".format(url, data) error_string = "Problem posting data to the grading controller. URL: {0}, data: {1}".format(url, data)
log.error(error_string) log.error(error_string)
raise GradingServiceError(error_string) raise GradingServiceError(error_string)
return r.text return response_json
def get(self, url, params, allow_redirects=False): 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, op = lambda: self.session.get(url,
allow_redirects=allow_redirects, allow_redirects=allow_redirects,
params=params) params=params)
try: try:
r = self._try_with_login(op) response_json = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err: except (RequestException, ConnectionError, HTTPError, ValueError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace. # reraise as promised GradingServiceError, but preserve stacktrace.
#This is a dev_facing_error #This is a dev_facing_error
error_string = "Problem getting data from the grading controller. URL: {0}, params: {1}".format(url, params) error_string = "Problem getting data from the grading controller. URL: {0}, params: {1}".format(url, params)
log.error(error_string) log.error(error_string)
raise GradingServiceError(error_string) raise GradingServiceError(error_string)
return r.text return response_json
def _try_with_login(self, operation): def _try_with_login(self, operation):
""" """
...@@ -128,7 +128,7 @@ class GradingService(object): ...@@ -128,7 +128,7 @@ class GradingService(object):
response = operation() response = operation()
response.raise_for_status() response.raise_for_status()
return response return resp_json
def _render_rubric(self, response, view_only=False): def _render_rubric(self, response, view_only=False):
""" """
......
...@@ -31,8 +31,7 @@ class PeerGradingService(GradingService): ...@@ -31,8 +31,7 @@ class PeerGradingService(GradingService):
def get_data_for_location(self, problem_location, student_id): def get_data_for_location(self, problem_location, student_id):
params = {'location': problem_location, 'student_id': student_id} params = {'location': problem_location, 'student_id': student_id}
response = self.get(self.get_data_for_location_url, params) result = self.get(self.get_data_for_location_url, params)
result = response.json()
self._record_result('get_data_for_location', result) self._record_result('get_data_for_location', result)
for key in result.keys(): for key in result.keys():
if key in ('success', 'error', 'version'): if key in ('success', 'error', 'version'):
...@@ -45,27 +44,26 @@ class PeerGradingService(GradingService): ...@@ -45,27 +44,26 @@ class PeerGradingService(GradingService):
return result return result
def get_next_submission(self, problem_location, grader_id): def get_next_submission(self, problem_location, grader_id):
response = self.get( result = self._render_rubric(self.get(
self.get_next_submission_url, self.get_next_submission_url,
{ {
'location': problem_location, 'location': problem_location,
'grader_id': grader_id 'grader_id': grader_id
} }
) ))
result = self._render_rubric(response.json())
self._record_result('get_next_submission', result) self._record_result('get_next_submission', result)
return result return result
def save_grade(self, **kwargs): def save_grade(self, **kwargs):
data = kwargs data = kwargs
data.update({'rubric_scores_complete': True}) 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) self._record_result('save_grade', result)
return result return result
def is_student_calibrated(self, problem_location, grader_id): def is_student_calibrated(self, problem_location, grader_id):
params = {'problem_id': problem_location, 'student_id': 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( self._record_result(
'is_student_calibrated', 'is_student_calibrated',
result, result,
...@@ -75,22 +73,20 @@ class PeerGradingService(GradingService): ...@@ -75,22 +73,20 @@ class PeerGradingService(GradingService):
def show_calibration_essay(self, problem_location, grader_id): def show_calibration_essay(self, problem_location, grader_id):
params = {'problem_id': problem_location, 'student_id': grader_id} params = {'problem_id': problem_location, 'student_id': grader_id}
response = self.get(self.show_calibration_essay_url, params) result = self._render_rubric(self.get(self.show_calibration_essay_url, params))
result = self._render_rubric(response.json())
self._record_result('show_calibration_essay', result) self._record_result('show_calibration_essay', result)
return result return result
def save_calibration_essay(self, **kwargs): def save_calibration_essay(self, **kwargs):
data = kwargs data = kwargs
data.update({'rubric_scores_complete': True}) 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) self._record_result('show_calibration_essay', result)
return result return result
def get_problem_list(self, course_id, grader_id): def get_problem_list(self, course_id, grader_id):
params = {'course_id': course_id, 'student_id': grader_id} params = {'course_id': course_id, 'student_id': grader_id}
response = self.get(self.get_problem_list_url, params) result = self.get(self.get_problem_list_url, params)
result = response.json()
self._record_result('get_problem_list', result) self._record_result('get_problem_list', result)
dog_stats_api.histogram( dog_stats_api.histogram(
self._metric_name('get_problem_list.result.length'), self._metric_name('get_problem_list.result.length'),
...@@ -100,8 +96,7 @@ class PeerGradingService(GradingService): ...@@ -100,8 +96,7 @@ class PeerGradingService(GradingService):
def get_notifications(self, course_id, grader_id): def get_notifications(self, course_id, grader_id):
params = {'course_id': course_id, 'student_id': grader_id} params = {'course_id': course_id, 'student_id': grader_id}
response = self.get(self.get_notifications_url, params) result = self.get(self.get_notifications_url, params)
result = response.json()
self._record_result( self._record_result(
'get_notifications', 'get_notifications',
result, result,
......
...@@ -116,7 +116,7 @@ class StaffGradingService(GradingService): ...@@ -116,7 +116,7 @@ class StaffGradingService(GradingService):
GradingServiceError: something went wrong with the connection. GradingServiceError: something went wrong with the connection.
""" """
params = {'course_id': course_id, 'grader_id': grader_id} 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)] tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_problem_list', result, tags) self._record_result('get_problem_list', result, tags)
dog_stats_api.histogram( dog_stats_api.histogram(
...@@ -143,10 +143,15 @@ class StaffGradingService(GradingService): ...@@ -143,10 +143,15 @@ class StaffGradingService(GradingService):
Raises: Raises:
GradingServiceError: something went wrong with the connection. GradingServiceError: something went wrong with the connection.
""" """
response = self.get(self.get_next_url, result = self._render_rubric(
params={'location': location, self.get(
'grader_id': grader_id}) self.get_next_url,
result = self._render_rubric(response.json()) params={
'location': location,
'grader_id': grader_id
}
)
)
tags = [u'course_id:{}'.format(course_id)] tags = [u'course_id:{}'.format(course_id)]
self._record_result('get_next', result, tags) self._record_result('get_next', result, tags)
return result return result
...@@ -174,15 +179,14 @@ class StaffGradingService(GradingService): ...@@ -174,15 +179,14 @@ class StaffGradingService(GradingService):
'rubric_scores_complete': True, 'rubric_scores_complete': True,
'submission_flagged': submission_flagged} 'submission_flagged': submission_flagged}
response = self.post(self.save_grade_url, data=data) result = self._render_rubric(self.post(self.save_grade_url, data=data))
result = self._render_rubric(response.json())
tags = [u'course_id:{}'.format(course_id)] tags = [u'course_id:{}'.format(course_id)]
self._record_result('save_grade', result, tags) self._record_result('save_grade', result, tags)
return result return result
def get_notifications(self, course_id): def get_notifications(self, course_id):
params = {'course_id': 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 = [ tags = [
u'course_id:{}'.format(course_id), u'course_id:{}'.format(course_id),
u'staff_needs_to_grade:{}'.format(result.get('staff_needs_to_grade')) u'staff_needs_to_grade:{}'.format(result.get('staff_needs_to_grade'))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment