diff --git a/src/trustcafeapiwrapper/wrappers/comment/create_comment.py b/src/trustcafeapiwrapper/wrappers/comment/create_comment.py index 9664053..5c86c1e 100644 --- a/src/trustcafeapiwrapper/wrappers/comment/create_comment.py +++ b/src/trustcafeapiwrapper/wrappers/comment/create_comment.py @@ -1,7 +1,7 @@ from trustcafeapiwrapper.utils.get_parent_pksk_from_path import get_parent_pksk_from_path from trustcafeapiwrapper.utils.get_post_pksk import get_post_pksk -def create_comment(comment_text, post_slug, parent_path, blur_label=None, version=3): +def create_comment(comment_text, post_slug=None, parent_path=None, post_key=None, blur_label=None, version=3): """ Creates a new comment. @@ -15,9 +15,20 @@ def create_comment(comment_text, post_slug, parent_path, blur_label=None, versio dict: A dictionary containing the job name and payload for creating the comment that will be processed by the API client wrapper function. """ - - parent_pksk = get_parent_pksk_from_path(parent_path) - post_pksk = get_post_pksk(parent_pksk, post_slug) + if post_key is not None: + post_pksk = { + "pk": post_key.get('pk'), + "sk": post_key.get('sk') + } + elif parent_path is not None and post_slug is not None: + parent_pksk = get_parent_pksk_from_path(parent_path) + post_pksk = get_post_pksk(parent_pksk, post_slug) + else: + raise ValueError("Either post_key or both parent_path and post_slug must be provided.") + + if comment_text is None or comment_text.strip() == "": + raise ValueError("Comment text cannot be empty.") + return { "job_function": "comment.create", "payload": { @@ -26,7 +37,7 @@ def create_comment(comment_text, post_slug, parent_path, blur_label=None, versio "parent": { "pk": post_pksk['pk'], "sk": post_pksk['sk'], - "slug": parent_path.split('/')[-1] + "slug": post_pksk.get('sk', '').replace('post#', ''), }, "topLevel": { "pk": post_pksk['pk'], diff --git a/tests/wrappers/create_comment.py b/tests/wrappers/create_comment.py index 3156937..8f2df2a 100644 --- a/tests/wrappers/create_comment.py +++ b/tests/wrappers/create_comment.py @@ -19,4 +19,23 @@ class TestCreateComment(unittest.TestCase): self.assertEqual(result["payload"]["commentText"], self.comment_text) self.assertEqual(result["payload"]["blurLabel"], self.blur_label) self.assertEqual(result["payload"]["parent"]["pk"], "maintrunk#maintrunk") - self.assertEqual(result["payload"]["parent"]["sk"], "post#1774951384-98fe38df") \ No newline at end of file + self.assertEqual(result["payload"]["parent"]["sk"], "post#1774951384-98fe38df") + + def test_create_comment_with_post_key(self): + post_key = { + "pk": "maintrunk#maintrunk", + "sk": "post#1774951384-98fe38df" + } + result = create_comment( + comment_text=self.comment_text, + post_key=post_key, + blur_label=self.blur_label + ) + self.assertIsInstance(result, dict) + self.assertIn("job_function", result) + self.assertIn("payload", result) + self.assertEqual(result["job_function"], "comment.create") + self.assertEqual(result["payload"]["commentText"], self.comment_text) + self.assertEqual(result["payload"]["blurLabel"], self.blur_label) + self.assertEqual(result["payload"]["parent"]["pk"], post_key["pk"]) + self.assertEqual(result["payload"]["parent"]["sk"], post_key["sk"]) \ No newline at end of file