From 86d72b1564528db29af87719340b32042e2801ec Mon Sep 17 00:00:00 2001 From: Jennifer Zhao Date: Mon, 17 Feb 2025 11:49:57 -0800 Subject: [PATCH] support for fetching resources --- edapi/edapi.py | 17 ++++++++++++ edapi/types/api_types/endpoints/resources.py | 16 +++++++++++ edapi/types/api_types/resources.py | 28 ++++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 edapi/types/api_types/endpoints/resources.py create mode 100644 edapi/types/api_types/resources.py diff --git a/edapi/edapi.py b/edapi/edapi.py index 9167a22..3026609 100644 --- a/edapi/edapi.py +++ b/edapi/edapi.py @@ -27,8 +27,10 @@ API_PutThread_Response_Thread, ) from .types.api_types.endpoints.user import API_User_Response +from .types.api_types.endpoints.resources import API_GetResource_Response from .types.api_types.thread import API_Thread_WithComments, API_Thread_WithUser from .types.api_types.user import API_User_WithEmail +from .types.api_types.resources import API_Resource ANSI_BLUE = lambda text: f"\u001b[34m{text}\u001b[0m" ANSI_GREEN = lambda text: f"\u001b[32m{text}\u001b[0m" @@ -415,3 +417,18 @@ def unlock_thread(self, thread_id: int) -> None: response = self.session.post(unlock_url) if not response.ok: _throw_error(f"Failed to unlock thread {thread_id}.", response.content) + + @_ensure_login + def get_resources(self, course_id: int) -> list[API_Resource]: + """ + Retrieve resources for a course. + + GET /api/courses//resources + """ + resources_url = urljoin(API_BASE_URL, f"courses/{course_id}/resources") + response = self.session.get(resources_url) + if response.ok: + response_json: API_GetResource_Response = response.json() + return response_json["resources"] + + _throw_error(f"Failed to get resources for course {course_id}.", response.content) \ No newline at end of file diff --git a/edapi/types/api_types/endpoints/resources.py b/edapi/types/api_types/endpoints/resources.py new file mode 100644 index 0000000..06cbd68 --- /dev/null +++ b/edapi/types/api_types/endpoints/resources.py @@ -0,0 +1,16 @@ +""" +Types for endpoints involving retrieving resources. +""" + +from typing import Any, Optional, TypedDict + +from ..resources import API_Resource + +# === GET /api/resources/ === + +class API_GetResource_Response(TypedDict): + """ + Response type for GET /api/resources/. + """ + + resources: API_Resource \ No newline at end of file diff --git a/edapi/types/api_types/resources.py b/edapi/types/api_types/resources.py new file mode 100644 index 0000000..d2134b0 --- /dev/null +++ b/edapi/types/api_types/resources.py @@ -0,0 +1,28 @@ +""" +Resource type used in the Ed API. +""" + +from typing import Optional, TypedDict + +from .content import ContentString +from .user import API_User_Short + + +class API_Resource(TypedDict): + """ + Resource type used in the Ed API. + """ + + id: int # resource id + course_id: int + name: str # resource name + session: str + category: str # resource category + extension: str + link: str + size: int + staff_only: bool + embedding: bool + release_at: Optional[str] + created_at: str + updated_at: Optional[str] \ No newline at end of file