wrapped function to condense code

This commit is contained in:
simonwt 2026-04-01 21:31:15 +01:00
parent 7fa6d640e4
commit c27a89b8f9
5 changed files with 49 additions and 14 deletions

View file

@ -23,6 +23,7 @@ to find.
2. Should these `job` be called with dot notation? Or with slashes? Or
something else? Is it okay to have these as strings?
3. Should we do validation in the `jobs` or let the server do that all?
4. `wrapped` is an awkward name. What's more proper? But also obvious?
## ToDo:

View file

@ -147,3 +147,18 @@ class APIClient(BaseModel):
return job_func(self, *args, **kwargs)
def wrapped(self, wrapped_data):
"""
Utility method to run a job function based on a wrapped data dictionary
containing 'job' and 'payload' keys as expected by the API client wrapper functions.
Args:
wrapped_data (dict): A dictionary with 'job' (string) and 'payload' (dict) keys.
Returns:
The result of the job function execution.
"""
return self.run_job(wrapped_data.get("job_function"), wrapped_data.get("payload", {}))

View file

@ -0,0 +1,11 @@
def listpublic(API) -> dict:
"""
Fetches the list of public posts from the API.
Args:
branch_slug (str): Slug of the branch to fetch posts for.
Returns:
dict: The list of public posts.
"""
post_list = API.make_request("GET", "content", f"post/public", authenticate=True)
return post_list

View file

@ -127,7 +127,12 @@ from wrappers.post.create_post import create_post
# "This is a test post created via the create_post wrapper function.",
# "/userprofile/simon-little",
# ))
save_response(API.run_job('post.create', create_post(
# wrapped = create_post(
# "This is a test post created via the create_post wrapper function without using the `wrapped` method.",
# )
# save_response(API.run_job(**wrapped))
save_response(API.wrapped(create_post(
"This is a test post created via the create_post wrapper function.",
"/userprofile/simon-little",
)))

View file

@ -10,8 +10,8 @@ def create_post(post_text, parent_path='/', blur_label=None, card_url=None, coll
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'.
dict: A dictionary containing the job name and payload for creating the post
that will be processed by the API client wrapper function.
"""
parent_pksk = None
@ -32,6 +32,8 @@ def create_post(post_text, parent_path='/', blur_label=None, card_url=None, coll
return {
"job_function": "post.create",
"payload": {
"blurLabel": blur_label,
"cardUrl": card_url,
"postText": post_text,
@ -41,3 +43,4 @@ def create_post(post_text, parent_path='/', blur_label=None, card_url=None, coll
"sk": parent_pksk
}
}
}