diff --git a/edapi/edapi.py b/edapi/edapi.py index ebb429f..f840f9c 100644 --- a/edapi/edapi.py +++ b/edapi/edapi.py @@ -12,7 +12,7 @@ from edapi.types.api_types.endpoints.analytics import API_Analytics_Users_Response -from .types import EdAuthError, EdError, EditThreadParams, PostThreadParams +from .types import EdAuthError, EdError, EditThreadParams, PostThreadParams, PostCommentParams from .types.api_types.endpoints.activity import ( API_ListUserActivity_Response, API_ListUserActivity_Response_Item, @@ -302,6 +302,20 @@ def post_thread( _throw_error(f"Failed to post thread in course {course_id}.", response.content) + @_ensure_login + def post_comment( + self, thread_id: int, params: PostCommentParams + ): + """ + Creates a new comment under the given thread. + + POST /api/threads//comments + """ + thread_url = urljoin(API_BASE_URL, f"threads/{thread_id}/comments") + response = self.session.post(thread_url, json={"comment": params}) + if not response.ok: + _throw_error(f"Failed to post comment in thread {thread_id}.", response.content) + @_ensure_login def edit_thread( self, thread_id: int, params: EditThreadParams, unlock_thread=True diff --git a/edapi/types/types.py b/edapi/types/types.py index 04fd126..038871c 100644 --- a/edapi/types/types.py +++ b/edapi/types/types.py @@ -53,3 +53,13 @@ class PostThreadParams(TypedDict, total=True): is_anonymous: bool is_megathread: bool anonymous_comments: bool + +class PostCommentParams(TypedDict, total=True): + """ + Parameters for posting a new comment. + All parameters are required. + """ + content: str + is_anonymous: bool + is_private: bool + type: str