functioning wrapper

This commit is contained in:
simonwt 2026-04-01 19:52:04 +01:00
parent 9407b9a03e
commit 78c2b2006e
4 changed files with 60 additions and 43 deletions

View file

@ -110,5 +110,20 @@ def save_response(response):
# 'sk': 'post#1774951384-98fe38df' # '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",
))

View file

@ -1 +1 @@
import post import wrappers.create_post as create_post

43
wrappers/create_post.py Normal file
View file

@ -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
}
}

View file

@ -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