Commit 125945de by Diana Huang

Refactor get and post logic into new GradingService

parent ecf04b3e
# This class gives a common interface for logging into # This class gives a common interface for logging into the grading controller
# the graing controller
import json import json
import logging import logging
import requests import requests
...@@ -45,6 +44,35 @@ class GradingService(object): ...@@ -45,6 +44,35 @@ class GradingService(object):
return response.json return response.json
def post(self, url, allow_redirects, data):
"""
Make a post request to the grading controller
"""
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:
# reraise as promised GradingServiceError, but preserve stacktrace.
raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text
def get(self, url, allow_redirects, params):
"""
Make a get request to the grading controller
"""
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:
# reraise as promised GradingServiceError, but preserve stacktrace.
raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text
def _try_with_login(self, operation): def _try_with_login(self, operation):
""" """
......
...@@ -83,17 +83,8 @@ class StaffGradingService(GradingService): ...@@ -83,17 +83,8 @@ class StaffGradingService(GradingService):
Raises: Raises:
GradingServiceError: something went wrong with the connection. GradingServiceError: something went wrong with the connection.
""" """
op = lambda: self.session.get(self.get_problem_list_url, params = {'course_id': course_id,'grader_id': grader_id}
allow_redirects = False, return self.get(self.get_problem_list_url, False, params)
params={'course_id': course_id,
'grader_id': grader_id})
try:
r = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace.
raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text
def get_next(self, course_id, location, grader_id): def get_next(self, course_id, location, grader_id):
...@@ -114,17 +105,10 @@ class StaffGradingService(GradingService): ...@@ -114,17 +105,10 @@ class StaffGradingService(GradingService):
Raises: Raises:
GradingServiceError: something went wrong with the connection. GradingServiceError: something went wrong with the connection.
""" """
op = lambda: self.session.get(self.get_next_url, return self.get(self.get_next_url,
allow_redirects=False, allow_redirects=False,
params={'location': location, params={'location': location,
'grader_id': grader_id}) 'grader_id': grader_id})
try:
r = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace.
raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text
def save_grade(self, course_id, grader_id, submission_id, score, feedback, skipped): def save_grade(self, course_id, grader_id, submission_id, score, feedback, skipped):
...@@ -139,22 +123,15 @@ class StaffGradingService(GradingService): ...@@ -139,22 +123,15 @@ class StaffGradingService(GradingService):
Raises: Raises:
GradingServiceError if there's a problem connecting. GradingServiceError if there's a problem connecting.
""" """
try: data = {'course_id': course_id,
data = {'course_id': course_id, 'submission_id': submission_id,
'submission_id': submission_id, 'score': score,
'score': score, 'feedback': feedback,
'feedback': feedback, 'grader_id': grader_id,
'grader_id': grader_id, 'skipped': skipped}
'skipped': skipped}
return self.post(self.save_grade_url, data=data,
op = lambda: self.session.post(self.save_grade_url, data=data, allow_redirects=False)
allow_redirects=False)
r = self._try_with_login(op)
except (RequestException, ConnectionError, HTTPError) as err:
# reraise as promised GradingServiceError, but preserve stacktrace.
raise GradingServiceError, str(err), sys.exc_info()[2]
return r.text
# don't initialize until staff_grading_service() is called--means that just # don't initialize until staff_grading_service() is called--means that just
# importing this file doesn't create objects that may not have the right config # importing this file doesn't create objects that may not have the right config
......
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