trustcafe-api-wrapper/testing.py
2026-04-01 19:55:21 +01:00

129 lines
4 KiB
Python

"""
Demonstrate basic usage
"""
from apiclient import APIClient
import os, simplejson as json
# Handle environment variables
from dotenv import load_dotenv
load_dotenv()
if not os.getenv("client_id") or not os.getenv("client_secret"):
raise Exception("Please set client_id and client_secret in your environment variables.")
# Initialize API client with credentials from environment variables
API = APIClient(
client_id=os.getenv("client_id"),
client_secret=os.getenv("client_secret"),
debug=False # Set to True to enable debug output
)
# Keep a token cache to avoid unnecessary sign-ins during development.
# (In production, you'd handle this more robustly and securely)
if os.path.exists("token_data.json"):
with open("token_data.json", "r") as f:
token_data = json.load(f)
API.set_token(token_data)
# Get a new one if we don't have one or if the existing one is expired
if not API.is_token_valid():
tokendata = API.sign_in()
with open("token_data.json", "w") as f:
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----------------")
# profile = API.run_job('userprofile.get', "simon-little")
# print(profile)
# print("-------------- Get a branch -----------------")
# branch = API.run_job('branch.get', "music")
# print(branch)
# print("-------------- Get a post -------------------")
# post = API.run_job('post.get', "1774951384-98fe38df")
# print(post)
# print("-----------------------------")
# feed = API.run_job('post.listbybranch', "music")
# print(feed)
# print("-----------------------------")
# feed = API.run_job('post.listbyuserprofile', "simon-little")
# print(feed)
# print("-----------------------------")
# save_response(API.run_job('post.create', {
# "blurLabel": None,
# "cardUrl": None,
# "postText": "This is a test post created via the API wrapper.",
# "collaborative": False,
# "parent": {
# "pk": "maintrunk#maintrunk",
# "sk": "maintrunk#maintrunk"
# }
# }))
# save_response(API.run_job('comment.create', {
# "blurLabel": None,
# "commentText": "This is a test comment created via the API wrapper.",
# "parent": {
# "pk": "maintrunk#maintrunk",
# "sk": "post#1774951384-98fe38df",
# "slug": "1774951384-98fe38df"
# },
# "topLevel": {
# "pk": "maintrunk#maintrunk",
# "sk": "post#1774951384-98fe38df"
# },
# "version": 3
# }))
# save_response(API.run_job('comment.listtbypostid', "1774951384-98fe38df"))
# x = 1
# while x <= 20:
# post = API.run_job('comment.create', {
# "blurLabel": None,
# "commentText": f"Making loads of comment #{x}",
# "parent": {
# "pk": "maintrunk#maintrunk",
# "sk": "post#1774951384-98fe38df",
# "slug": "1774951384-98fe38df"
# },
# "topLevel": {
# "pk": "maintrunk#maintrunk",
# "sk": "post#1774951384-98fe38df"
# },
# "version": 3
# })
# print(post)
# x += 1
# save_response(API.run_job('notification.listnotifications'))
# save_response(API.run_job('feed.cafefeed'))
# save_response(API.run_job('feed.followingfeed'))
# save_response(API.run_job('feed.followingfeed', {
# 'pk': 'maintrunk#maintrunk',
# 'sk': 'post#1774951384-98fe38df'
# }))
# save_response(API.run_job('post.listall'))
from wrappers.post.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",
))