React to something
This commit is contained in:
parent
7bca418530
commit
60351f7cdb
14 changed files with 159 additions and 7 deletions
2
src/trustcafeapiwrapper/jobs/reaction/__init__.py
Normal file
2
src/trustcafeapiwrapper/jobs/reaction/__init__.py
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
from .react import create
|
||||
from .listbyparent import listbyparent
|
||||
10
src/trustcafeapiwrapper/jobs/reaction/getbyparent.py
Normal file
10
src/trustcafeapiwrapper/jobs/reaction/getbyparent.py
Normal file
|
|
@ -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
|
||||
11
src/trustcafeapiwrapper/jobs/reaction/listbyparent.py
Normal file
11
src/trustcafeapiwrapper/jobs/reaction/listbyparent.py
Normal file
|
|
@ -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
|
||||
12
src/trustcafeapiwrapper/jobs/reaction/react.py
Normal file
12
src/trustcafeapiwrapper/jobs/reaction/react.py
Normal file
|
|
@ -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
|
||||
|
|
@ -1,2 +1,4 @@
|
|||
from .get_parent_pksk_from_path import get_parent_pksk_from_path
|
||||
from .get_post_pksk import get_post_pksk
|
||||
from .get_post_pksk import get_post_pksk
|
||||
from .make_comment_sk import make_comment_sk
|
||||
from .make_post_sk import make_post_sk
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
}
|
||||
|
||||
|
|
|
|||
10
src/trustcafeapiwrapper/utils/make_comment_sk.py
Normal file
10
src/trustcafeapiwrapper/utils/make_comment_sk.py
Normal file
|
|
@ -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}"
|
||||
11
src/trustcafeapiwrapper/utils/make_post_sk.py
Normal file
11
src/trustcafeapiwrapper/utils/make_post_sk.py
Normal file
|
|
@ -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}"
|
||||
47
src/trustcafeapiwrapper/wrappers/reaction/react.py
Normal file
47
src/trustcafeapiwrapper/wrappers/reaction/react.py
Normal file
|
|
@ -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),
|
||||
}
|
||||
}
|
||||
}
|
||||
8
tests/utils/make_comment_sk.py
Normal file
8
tests/utils/make_comment_sk.py
Normal file
|
|
@ -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)
|
||||
8
tests/utils/make_post_sk.py
Normal file
8
tests/utils/make_post_sk.py
Normal file
|
|
@ -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)
|
||||
31
tests/wrappers/react.py
Normal file
31
tests/wrappers/react.py
Normal file
|
|
@ -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")
|
||||
|
|
@ -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()
|
||||
|
|
|
|||
2
uv.lock
generated
2
uv.lock
generated
|
|
@ -213,7 +213,7 @@ wheels = [
|
|||
|
||||
[[package]]
|
||||
name = "trustcafeapiwrapper"
|
||||
version = "0.1.0.1"
|
||||
version = "0.1.0.3"
|
||||
source = { editable = "." }
|
||||
dependencies = [
|
||||
{ name = "dotenv" },
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue