Save test response to json file

List public and all
This commit is contained in:
simonwt 2026-04-01 00:00:04 +01:00
parent 5e477c53cb
commit 9407b9a03e
7 changed files with 96 additions and 30 deletions

1
.gitignore vendored
View file

@ -26,3 +26,4 @@ dist/*
# Test Artifacts # Test Artifacts
token_data.json token_data.json
test_output.json

View file

@ -1,4 +1,6 @@
from .get import get from .get import get
from .listbybranch import listbybranch from .listbybranch import listbybranch
from .listbyuserprofile import listbyuserprofile from .listbyuserprofile import listbyuserprofile
from .create import create from .create import create
from .listall import listall
from .listpublic import listpublic

11
jobs/post/listall.py Normal file
View file

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

11
jobs/post/listpublic.py Normal file
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

@ -14,7 +14,7 @@ if not os.getenv("client_id") or not os.getenv("client_secret"):
API = APIClient( API = APIClient(
client_id=os.getenv("client_id"), client_id=os.getenv("client_id"),
client_secret=os.getenv("client_secret"), client_secret=os.getenv("client_secret"),
debug=True debug=False # Set to True to enable debug output
) )
# Keep a token cache to avoid unnecessary sign-ins during development. # Keep a token cache to avoid unnecessary sign-ins during development.
@ -29,7 +29,11 @@ if os.path.exists("token_data.json"):
if not API.is_token_valid(): if not API.is_token_valid():
tokendata = API.sign_in() tokendata = API.sign_in()
with open("token_data.json", "w") as f: with open("token_data.json", "w") as f:
json.dump(tokendata, f, indent=4) json.dump(tokendata, f, indent=2)
def save_response(response):
with open("test_output.json", "w") as f:
json.dump(response, f, indent=2)
# print("-----------Get a user profile----------------") # print("-----------Get a user profile----------------")
# profile = API.run_job('userprofile.get', "simon-little") # profile = API.run_job('userprofile.get', "simon-little")
@ -47,7 +51,8 @@ if not API.is_token_valid():
# feed = API.run_job('post.listbyuserprofile', "simon-little") # feed = API.run_job('post.listbyuserprofile', "simon-little")
# print(feed) # print(feed)
# print("-----------------------------") # print("-----------------------------")
# post = API.run_job('post.create', {
# save_response(API.run_job('post.create', {
# "blurLabel": None, # "blurLabel": None,
# "cardUrl": None, # "cardUrl": None,
# "postText": "This is a test post created via the API wrapper.", # "postText": "This is a test post created via the API wrapper.",
@ -56,11 +61,9 @@ if not API.is_token_valid():
# "pk": "maintrunk#maintrunk", # "pk": "maintrunk#maintrunk",
# "sk": "maintrunk#maintrunk" # "sk": "maintrunk#maintrunk"
# } # }
# }) # }))
# print(post)
# print("-----------------------------")
# post = API.run_job('comment.create', { # save_response(API.run_job('comment.create', {
# "blurLabel": None, # "blurLabel": None,
# "commentText": "This is a test comment created via the API wrapper.", # "commentText": "This is a test comment created via the API wrapper.",
# "parent": { # "parent": {
@ -73,12 +76,10 @@ if not API.is_token_valid():
# "sk": "post#1774951384-98fe38df" # "sk": "post#1774951384-98fe38df"
# }, # },
# "version": 3 # "version": 3
# }) # }))
# print(post)
# print("-----------------------------") # save_response(API.run_job('comment.listtbypostid', "1774951384-98fe38df"))
# feed = API.run_job('comment.listtbypostid', "1774951384-98fe38df")
# print(feed)
# print("-----------------------------")
# x = 1 # x = 1
# while x <= 20: # while x <= 20:
# post = API.run_job('comment.create', { # post = API.run_job('comment.create', {
@ -97,19 +98,17 @@ if not API.is_token_valid():
# }) # })
# print(post) # print(post)
# x += 1 # x += 1
# print("-----------------------------")
# notifications = API.run_job('notification.listnotifications') # save_response(API.run_job('notification.listnotifications'))
# print(notifications)
# print("-----------------------------") # save_response(API.run_job('feed.cafefeed'))
# feed = API.run_job('feed.cafefeed')
# print(feed) # save_response(API.run_job('feed.followingfeed'))
# print("-----------------------------")
# feed = API.run_job('feed.followingfeed') # save_response(API.run_job('feed.followingfeed', {
# print(feed) # 'pk': 'maintrunk#maintrunk',
# print("-----------------------------") # 'sk': 'post#1774951384-98fe38df'
feed = API.run_job('feed.followingfeed', { # }))
'pk': 'maintrunk#maintrunk',
'sk': 'post#1774951384-98fe38df' save_response(API.run_job('post.listall'))
})
print(feed)
print("-----------------------------")

1
wrappers/__init__.py Normal file
View file

@ -0,0 +1 @@
import post

41
wrappers/post.py Normal file
View file

@ -0,0 +1,41 @@
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