diff --git a/src/trustcafeapiwrapper/jobs/reaction/__init__.py b/src/trustcafeapiwrapper/jobs/reaction/__init__.py new file mode 100644 index 0000000..9783e26 --- /dev/null +++ b/src/trustcafeapiwrapper/jobs/reaction/__init__.py @@ -0,0 +1,2 @@ +from .react import create +from .listbyparent import listbyparent diff --git a/src/trustcafeapiwrapper/jobs/reaction/getbyparent.py b/src/trustcafeapiwrapper/jobs/reaction/getbyparent.py new file mode 100644 index 0000000..88c2e74 --- /dev/null +++ b/src/trustcafeapiwrapper/jobs/reaction/getbyparent.py @@ -0,0 +1,10 @@ +def getbyparent(API, parent_sk: str) -> dict: + """ + Fetches the users reactions for a given parent from the API. + Args: + parent_sk (str): sk of the parent to fetch reactions for. + Returns: + dict: The list of reactions. + """ + reaction_list = API.make_request("GET", "content", f"getreactionbysk/{parent_sk}", authenticate=True) + return reaction_list \ No newline at end of file diff --git a/src/trustcafeapiwrapper/jobs/reaction/listbyparent.py b/src/trustcafeapiwrapper/jobs/reaction/listbyparent.py new file mode 100644 index 0000000..6e55b20 --- /dev/null +++ b/src/trustcafeapiwrapper/jobs/reaction/listbyparent.py @@ -0,0 +1,11 @@ +def listbyparent(API, parent_sk: str) -> dict: + """ + Fetches the list of reactions for a given parent from the API. + + Args: + parent_sk (str): sk of the parent to fetch reactions for. + Returns: + dict: The list of reactions. + """ + reaction_list = API.make_request("GET", "content", f"listreactionsbysk/{parent_sk}", authenticate=True) + return reaction_list \ No newline at end of file diff --git a/src/trustcafeapiwrapper/jobs/reaction/react.py b/src/trustcafeapiwrapper/jobs/reaction/react.py new file mode 100644 index 0000000..5baaef1 --- /dev/null +++ b/src/trustcafeapiwrapper/jobs/reaction/react.py @@ -0,0 +1,12 @@ +def reacttosomething(API, payload: dict) -> dict: + """ + Creates a new reaction in the API. + + Args: + payload (dict): The data for the new reaction. + + Returns: + dict: The reaction data. + """ + reaction_data = API.make_request("POST", "content", "reacttosomething", data=payload, authenticate=True) + return reaction_data \ No newline at end of file diff --git a/src/trustcafeapiwrapper/utils/__init__.py b/src/trustcafeapiwrapper/utils/__init__.py index 2747861..bc12c4e 100644 --- a/src/trustcafeapiwrapper/utils/__init__.py +++ b/src/trustcafeapiwrapper/utils/__init__.py @@ -1,2 +1,4 @@ from .get_parent_pksk_from_path import get_parent_pksk_from_path -from .get_post_pksk import get_post_pksk \ No newline at end of file +from .get_post_pksk import get_post_pksk +from .make_comment_sk import make_comment_sk +from .make_post_sk import make_post_sk diff --git a/src/trustcafeapiwrapper/utils/get_post_pksk.py b/src/trustcafeapiwrapper/utils/get_post_pksk.py index a399768..624b767 100644 --- a/src/trustcafeapiwrapper/utils/get_post_pksk.py +++ b/src/trustcafeapiwrapper/utils/get_post_pksk.py @@ -1,9 +1,7 @@ +from .make_post_sk import make_post_sk def get_post_pksk(parent_pksk, post_url): - - post_slug = post_url.strip('/post/') - return { "pk": parent_pksk, - "sk": f"post#{post_slug}" + "sk": make_post_sk(post_url) } diff --git a/src/trustcafeapiwrapper/utils/make_comment_sk.py b/src/trustcafeapiwrapper/utils/make_comment_sk.py new file mode 100644 index 0000000..0d24455 --- /dev/null +++ b/src/trustcafeapiwrapper/utils/make_comment_sk.py @@ -0,0 +1,10 @@ +def make_comment_sk(comment_url): + """ + Generates the sk for a comment given the comment url. + Args: + comment_url (str): The url of the comment. + Returns: + str: The sk for the comment. + """ + comment_slug = comment_url.strip('/comment/') + return f"comment#{comment_slug}" \ No newline at end of file diff --git a/src/trustcafeapiwrapper/utils/make_post_sk.py b/src/trustcafeapiwrapper/utils/make_post_sk.py new file mode 100644 index 0000000..ba43302 --- /dev/null +++ b/src/trustcafeapiwrapper/utils/make_post_sk.py @@ -0,0 +1,11 @@ +def make_post_sk(post_url): + """ + Generates the sk for a post given the post url. + Args: + post_url (str): The url of the post. + + Returns: + str: The sk for the post. + """ + post_slug = post_url.strip('/post/') + return f"post#{post_slug}" diff --git a/src/trustcafeapiwrapper/wrappers/reaction/react.py b/src/trustcafeapiwrapper/wrappers/reaction/react.py new file mode 100644 index 0000000..8419fb2 --- /dev/null +++ b/src/trustcafeapiwrapper/wrappers/reaction/react.py @@ -0,0 +1,47 @@ +from trustcafeapiwrapper.utils import get_parent_pksk_from_path, get_post_pksk, make_comment_sk, make_post_sk + +def react(reaction_type: str, parent_path: str, item_path: str): + """ + React to something. ie a post or a comment. + This is one endpoint for creating, updating and deleting reactions. + The logic is handled in the backend. + + Args: + + Returns: + dict: A dictionary containing the job name and payload for creating the post + that will be processed by the API client wrapper function. + """ + + ''' + + If it's a post we want to create a pk/sk like: + '{entity}#{parent_slug}' / 'post#{post_slug}' + + If it's a comment we want to create a pk/sk like: + 'post#{post_slug}' / 'comment#{comment_slug}' + + + ''' + + if item_path.startswith('/post'): + # It's a reaction on a post + top_level_parent_pk = get_parent_pksk_from_path(parent_path) + item_pksk = get_post_pksk(top_level_parent_pk, item_path) + else: + # It's a reaction on a comment + item_pksk = { + 'pk': make_post_sk(parent_path), + 'sk': make_comment_sk(item_path) + } + + return { + "job_function": "reaction.react", + "payload": { + "reaction": reaction_type, + "parent": { + "pk": item_pksk.get('pk', None), + "sk": item_pksk.get('sk', None), + } + } + } diff --git a/tests/utils/make_comment_sk.py b/tests/utils/make_comment_sk.py new file mode 100644 index 0000000..6b984f5 --- /dev/null +++ b/tests/utils/make_comment_sk.py @@ -0,0 +1,8 @@ +import unittest +from trustcafeapiwrapper.utils.make_comment_sk import make_comment_sk + +class TestMakeCommentSk(unittest.TestCase): + def test_make_comment_sk(self): + comment_url = '/comment/12345' + expected_result = 'comment#12345' + self.assertEqual(make_comment_sk(comment_url), expected_result) diff --git a/tests/utils/make_post_sk.py b/tests/utils/make_post_sk.py new file mode 100644 index 0000000..a430d4e --- /dev/null +++ b/tests/utils/make_post_sk.py @@ -0,0 +1,8 @@ +import unittest +from trustcafeapiwrapper.utils.make_post_sk import make_post_sk + +class TestMakePostSk(unittest.TestCase): + def test_make_post_sk(self): + post_url = '/post/12345' + expected_result = 'post#12345' + self.assertEqual(make_post_sk(post_url), expected_result) diff --git a/tests/wrappers/react.py b/tests/wrappers/react.py new file mode 100644 index 0000000..59d7a6a --- /dev/null +++ b/tests/wrappers/react.py @@ -0,0 +1,31 @@ +import unittest +from trustcafeapiwrapper.wrappers.reaction.react import react + +class TestReact(unittest.TestCase): + def test_react_to_post(self): + reaction_type = 'like' + parent_path = '/' + item_path = '/post/12345' + result = react(reaction_type, parent_path, item_path) + + self.assertIsInstance(result, dict) + self.assertIn("job_function", result) + self.assertIn("payload", result) + self.assertEqual(result["job_function"], "reaction.react") + self.assertEqual(result["payload"]["reaction"], reaction_type) + self.assertEqual(result["payload"]["parent"]["pk"], "maintrunk#maintrunk") + self.assertEqual(result["payload"]["parent"]["sk"], "post#12345") + + def test_react_to_comment(self): + reaction_type = 'like' + parent_path = '/post/12345' + item_path = '/comment/67890' + result = react(reaction_type, parent_path, item_path) + + self.assertIsInstance(result, dict) + self.assertIn("job_function", result) + self.assertIn("payload", result) + self.assertEqual(result["job_function"], "reaction.react") + self.assertEqual(result["payload"]["reaction"], reaction_type) + self.assertEqual(result["payload"]["parent"]["pk"], "post#12345") + self.assertEqual(result["payload"]["parent"]["sk"], "comment#67890") \ No newline at end of file diff --git a/unittests.py b/unittests.py index ccfe741..6390de7 100644 --- a/unittests.py +++ b/unittests.py @@ -4,7 +4,9 @@ import unittest from tests.wrappers.create_post import TestCreatePost from tests.wrappers.create_comment import TestCreateComment +from tests.wrappers.react import TestReact from tests.utils.get_post_pksk import TestGetPostPksk from tests.utils.get_parent_pksk_from_path import TestGetParentPkskFromPath - +from tests.utils.make_comment_sk import TestMakeCommentSk +from tests.utils.make_post_sk import TestMakePostSk unittest.main() diff --git a/uv.lock b/uv.lock index 5cfb325..b69a27c 100644 --- a/uv.lock +++ b/uv.lock @@ -213,7 +213,7 @@ wheels = [ [[package]] name = "trustcafeapiwrapper" -version = "0.1.0.1" +version = "0.1.0.3" source = { editable = "." } dependencies = [ { name = "dotenv" },