Commit 014f686a authored by Alex's avatar Alex
Browse files

Added classes for Courses and Reources, with auth

parent 4c2314b3
# imperial_doc_materials
Python client for the Materials API in DoC
\ No newline at end of file
Python client for the [Materials API](https://api-materials.doc.ic.ac.uk/) in DoC
\ No newline at end of file
from .client import Materials
from .courses import Course
from .resources import Resource
import requests
MATERIALS_API_LOGIN_URL = "https://api-materials.doc.ic.ac.uk/auth/login"
MATERIALS_API_LOGOUT_URL = "https://api-materials.doc.ic.ac.uk/auth/logout"
def login(username: str, password: str) -> str:
"""
Authenticates a user given their username and password. Returns an access_token to be used
for subsequent requests in the Authorization header.
"""
headers = {
"Content-Type": "application/json"
}
body = {
"username": username,
"password": password
}
resp = requests.post(MATERIALS_API_LOGIN_URL, json=body, headers=headers)
data = resp.json()
return str(data["access_token"])
def logout():
pass
\ No newline at end of file
import requests
from . import auth
from .courses import Courses, Course
from .resources import Resources, Course
MATERIALS_API_STATUS_URL = "https://api-materials.doc.ic.ac.uk/status"
class Materials:
def __init__(self, token: str):
self._access_token = token
@staticmethod
def _is_alive() -> bool:
resp = requests.get(f"{MATERIALS_API_STATUS_URL}")
data = resp.json()
return data["status"] == "alive"
@staticmethod
def create_client(username: str, password: str):
"""
Creates an authenticated Materials client object to be used for interacting with the Materials API.
"""
if not Materials._is_alive():
raise Exception("The Materials API is currently down.")
return Materials(auth.login(username, password))
from typing import List
import requests
from . import auth
from .resources import Resources, Resource
class Course:
def __init__(self, title: str, code: str, has_materials: bool, resources: List[Resource] = []):
self.title = title
self.code = code
self.has_materials = has_materials
self.resources = resources
MATERIALS_API_COURSES_URL = "https://api-materials.doc.ic.ac.uk/courses"
class Courses:
@staticmethod
def get_for_year(access_token: str, academic_year: str) -> List[Course]:
"""
Returns a list of Courses that were available for the logged in
user during the given the academic_year string.
(e.g. 2122 for the year 2021/2022).
"""
if len(academic_year) != 4 or not academic_year.isnumeric():
raise ValueError("Invalid format for academic_year")
headers = {
"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.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
def example():
print("hello")
\ No newline at end of file
from typing import List
import requests
class Resource:
pass
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]:
"""
Returns a list of Resources available for a given course.
"""
headers = {
"Authorization": f"Bearer {access_token}"
}
params = {
"course": course_code
}
resp = requests.get(MATERIALS_API_RESOURCES_URL, params=params, headers=headers)
data = resp.json()
resources = []
for resource in data:
res = Resource()
resources.append(res)
return resources
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