Commit 8128fd78 authored by Alex's avatar Alex
Browse files

Added method to get full list of resources for a year

parent a4d933f3
......@@ -2,6 +2,7 @@ from typing import List
import requests
from . import auth
from .courses import Courses, Course
from .resources import Resources, Resource
MATERIALS_API_STATUS_URL = "https://api-materials.doc.ic.ac.uk/status"
......@@ -32,3 +33,11 @@ class Materials:
(e.g. 2122 for the year 2021/2022).
"""
return Courses.get_for_year(self._access_token, academic_year)
def get_resources_for_year(self, academic_year: str) -> List[Resource]:
"""
Returns a list of Resources that were available for the logged in
user during the given the academic_year string.
(e.g. 2122 for the year 2021/2022).
"""
return Resources.get_for_year(self._access_token, academic_year)
\ No newline at end of file
......@@ -31,13 +31,16 @@ class Courses:
"Authorization": f"Bearer {access_token}"
}
resp = requests.get(f"{MATERIALS_API_COURSES_URL}/{academic_year}", headers=headers)
data = resp.json()
courses = []
for course in data:
resources = Resources.get_resources_for_course(access_token, course["code"])
c = Course(course["title"], course["code"], course["has_materials"], resources=resources)
courses.append(c)
return courses
\ No newline at end of file
try:
resp = requests.get(f"{MATERIALS_API_COURSES_URL}/{academic_year}", headers=headers)
data = resp.json()
courses = []
for course in data:
resources = Resources.get_for_course(access_token, course["code"])
c = Course(course["title"], course["code"], course["has_materials"], resources=resources)
courses.append(c)
return courses
except:
raise ValueError("Could not get courses for the given academic_year")
......@@ -25,7 +25,7 @@ MATERIALS_API_RESOURCES_URL = "https://api-materials.doc.ic.ac.uk/resources"
class Resources:
@staticmethod
def get_resources_for_course(access_token: str, course_code: str) -> List[Resource]:
def get_for_course(access_token: str, course_code: str) -> List[Resource]:
"""
Returns a list of Resources available for a given course.
"""
......@@ -37,20 +37,60 @@ class Resources:
"course": course_code
}
resp = requests.get(MATERIALS_API_RESOURCES_URL, params=params, headers=headers)
data = resp.json()
resources = []
for resource in data:
res = Resource(
resource["id"],
resource["title"],
ResourceType(resource["type"]),
resource["category"],
resource["path"],
resource["course"]
)
resources.append(res)
return resources
try:
resp = requests.get(MATERIALS_API_RESOURCES_URL, params=params, headers=headers)
data = resp.json()
resources = []
for resource in data:
res = Resource(
resource["id"],
resource["title"],
ResourceType(resource["type"]),
resource["category"],
resource["path"],
resource["course"]
)
resources.append(res)
return resources
except:
raise ValueError("Could not get resources for the given course_code")
@staticmethod
def get_for_year(access_token: str, academic_year: str) -> List[Resource]:
"""
Returns a list of Resources available for all the courses in the year.
"""
if len(academic_year) != 4 or not academic_year.isnumeric():
raise ValueError("Invalid format for academic_year")
headers = {
"Authorization": f"Bearer {access_token}"
}
params = {
"year": academic_year
}
try:
resp = requests.get(MATERIALS_API_RESOURCES_URL, params=params, headers=headers)
data = resp.json()
resources = []
for resource in data:
res = Resource(
resource["id"],
resource["title"],
ResourceType(resource["type"]),
resource["category"],
resource["path"],
resource["course"]
)
resources.append(res)
return resources
except:
raise ValueError("Could not get resources for the given academic_year")
Supports Markdown
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