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 2. Should these `job` be called with dot notation? Or with slashes? Or
something else? Is it okay to have these as strings? 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? 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: ## ToDo:

View file

@ -146,4 +146,19 @@ class APIClient(BaseModel):
job_func = job_function job_func = job_function
return job_func(self, *args, **kwargs) 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.", # "This is a test post created via the create_post wrapper function.",
# "/userprofile/simon-little", # "/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.", "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. collaborative (bool, optional): Whether the post is collaborative, default is False.
Returns: Returns:
dict: The payload for creating the post, which can be passed to dict: A dictionary containing the job name and payload for creating the post
the API client's run_job method with 'post.create'. that will be processed by the API client wrapper function.
""" """
parent_pksk = None parent_pksk = None
@ -32,12 +32,15 @@ def create_post(post_text, parent_path='/', blur_label=None, card_url=None, coll
return { return {
"blurLabel": blur_label, "job_function": "post.create",
"cardUrl": card_url, "payload": {
"postText": post_text, "blurLabel": blur_label,
"collaborative": collaborative, "cardUrl": card_url,
"parent": { "postText": post_text,
"pk": parent_pksk, "collaborative": collaborative,
"sk": parent_pksk "parent": {
} "pk": parent_pksk,
} "sk": parent_pksk
}
}
}