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

feat: get grades for a course #2

Merged
merged 2 commits into from
Feb 15, 2024
Merged
Changes from all 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
24 changes: 23 additions & 1 deletion openedx_rest_api_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
URL_VALIDATION_REGISTRATION = '/api/user/v1/validation/registration'
URL_ACCOUNT_REGISTRATION = '/api/user/v1/account/registration/'

URL_COURSE_GRADES = '/api/grades/v1/courses/{course_id}/'


class OpenedxRESTAPIClient:
""" A client to access Open edX REST API endpoints.
Expand Down Expand Up @@ -382,6 +384,26 @@ def register_account(self,

return response.json()

def get_course_grades(self, course_id, username=None) -> List[dict]:
def _get_course_grades(url, _course_id, _params=None):
""" Recursively load all grades for a course. """
response = self.session.get(urljoin(url, URL_COURSE_GRADES.format(course_id=_course_id)), params=_params)
response.raise_for_status()
data = response.json()
if isinstance(data, dict):
results = data.get('results', [])
if next_page_url := data.get('next'):
results += _get_course_grades(next_page_url, _course_id, parse_qs(urlparse(next_page_url).query))
else:
results = data
return results

params = {}
if username:
params['username'] = username

return _get_course_grades(self._base_url, course_id, params)

def validation_registration(self, url: str = None, **kwargs) -> dict:
"""
Validates the account registration form.
Expand Down Expand Up @@ -416,4 +438,4 @@ def validation_registration(self, url: str = None, **kwargs) -> dict:
"""
response = self._post_json(path=URL_VALIDATION_REGISTRATION, params=kwargs, url=url)

return response.json()
return response.json()