From 78c2b2006ef951244306ad43eaecd13a08f824ab Mon Sep 17 00:00:00 2001 From: simonwt Date: Wed, 1 Apr 2026 19:52:04 +0100 Subject: [PATCH] functioning wrapper --- testing.py | 17 +++++++++++++++- wrappers/__init__.py | 2 +- wrappers/create_post.py | 43 +++++++++++++++++++++++++++++++++++++++++ wrappers/post.py | 41 --------------------------------------- 4 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 wrappers/create_post.py delete mode 100644 wrappers/post.py diff --git a/testing.py b/testing.py index 7095fe7..a91cca7 100644 --- a/testing.py +++ b/testing.py @@ -110,5 +110,20 @@ def save_response(response): # 'sk': 'post#1774951384-98fe38df' # })) -save_response(API.run_job('post.listall')) +# save_response(API.run_job('post.listall')) +from wrappers.create_post import create_post + +# save_response(create_post( +# post_text="This is a test post created via the create_post wrapper function.", +# )) + +# save_response(create_post( +# post_text="This is a test post created via the create_post wrapper function.", +# parent_path="/branch/music", +# )) + +save_response(create_post( + "This is a test post created via the create_post wrapper function.", + "/userprofile/simon-little", +)) diff --git a/wrappers/__init__.py b/wrappers/__init__.py index 6a3cb9e..695bd39 100644 --- a/wrappers/__init__.py +++ b/wrappers/__init__.py @@ -1 +1 @@ -import post \ No newline at end of file +import wrappers.create_post as create_post \ No newline at end of file diff --git a/wrappers/create_post.py b/wrappers/create_post.py new file mode 100644 index 0000000..d667a41 --- /dev/null +++ b/wrappers/create_post.py @@ -0,0 +1,43 @@ +def create_post(post_text, parent_path='/', blur_label=None, card_url=None, collaborative=False): + """ + Creates a new post. + + Args: + post_text (str): The text content of the post. + parent_path (str, optional): The parent path for the post, default is None. + blur_label (str, optional): An optional label for blurring the post content. + card_url (str, optional): An optional URL to include as a card in the post. + collaborative (bool, optional): Whether the post is collaborative, default is False. + + Returns: + dict: The payload for creating the post, which can be passed to + the API client's run_job method with 'post.create'. + """ + + parent_pksk = None + + if parent_path == '/': + parent_pksk = 'maintrunk#maintrunk' + else: + parent_path = parent_path.strip('/') + entity, slug = parent_path.split('/') + if entity == 'branch': + entity = 'subwiki' + + if entity not in ['userprofile', 'subwiki']: + raise ValueError(f"Invalid parent entity: {entity}. Must be 'userprofile' or 'branch'.") + + parent_pksk = f"{entity}#{slug}" + + + + return { + "blurLabel": blur_label, + "cardUrl": card_url, + "postText": post_text, + "collaborative": collaborative, + "parent": { + "pk": parent_pksk, + "sk": parent_pksk + } + } \ No newline at end of file diff --git a/wrappers/post.py b/wrappers/post.py deleted file mode 100644 index 6bd7ef3..0000000 --- a/wrappers/post.py +++ /dev/null @@ -1,41 +0,0 @@ -def create_post(post_text, parent_pksk="maintrunk#maintrunk", blur_label=None, card_url=None, collaborative=False): - """ - Creates a new post. - - Args: - post_text (str): The text content of the post. - parent_pksk (str): The parent PKSK for the post, default is "maintrunk#maintrunk". - blur_label (str, optional): An optional label for blurring the post content. - card_url (str, optional): An optional URL to include as a card in the post. - collaborative (bool, optional): Whether the post is collaborative, default is False. - - Returns: - dict: The response from the API containing details of the created post. - """ - - - - ''' - WIP - - How best to handle the API client here? - - - Should this be part of a big class? - - Should we pass the API client as an argument to this function? - - We can't create a new instance of the API client within this function - because it would need the credentials which can be dynamic and - don't want to tangle up this wrapper. - - - - ''' - response = API.run_job('post.create', { - "blurLabel": blur_label, - "cardUrl": card_url, - "postText": post_text, - "collaborative": collaborative, - "parent": { - "pk": parent_pksk, - "sk": parent_pksk - } - }) - return response \ No newline at end of file