trustcafe-api-wrapper/testing.py

199 lines
6.3 KiB
Python

"""
Demonstrate basic usage
"""
import sys
# Add the src directory to the Python path so we can import the API client and wrappers
sys.path.insert(0, './src/')
from trustcafeapiwrapper 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
'''
THIS IS THE IMPORTANT BIT
'''
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)
API.handle_token() # This will load the token from file if it exists and is valid, or sign in to get a new one if not.
'''
END IMPORTANT BIT
'''
# This is just to stop bunging up the console with output whilst
# doing these tests below
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 = save_response(API.run_job('post.listremoved'))
# print(feed)
# print("----------------------------z
# branchlist = API.run_job('branch.listbyname')
# print(branchlist)
# print("-----------------------------")
# branchlist = API.run_job('branch.listbyname', {
# 'GSIByName': 'boopscope test',
# 'sk': 'subwiki#boopscope-test',
# 'pk': 'subwiki#boopscope-test',
# 'entity': 'subwiki'
# })
# print(branchlist)
# 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('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('notification.markallasread'))
# 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'))
# save_response(API.run_job('post.listpublic'))
# from trustcafeapiwrapper.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",
# ))
# from trustcafeapiwrapper.wrappers.reaction import react
# save_response(create_post(
# "This is a test post created via the create_post wrapper function.",
# "/userprofile/simon-little",
# ))
# 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.",
# )))
# save_response(API.wrapped(react(
# "thumbs_up",
# "/",
# "/post/1775075313-63ffb852"
# )))
# from trustcafeapiwrapper.wrappers.vote import votecast
# save_response(API.wrapped(votecast(
# "up",
# "/",
# "/post/1775075313-63ffb852"
# )))
# print("-----------Get a list of trusted users for a specific user----------------")
# users = API.run_job('trust.listbyuserinit', "simon-little")
# print(users)
# users = API.run_job('trust.listbyuserhas', "simon-little ")
# print(users)
# from trustcafeapiwrapper.wrappers.trust import trust
# save_response(API.wrapped(trust(
# 100,
# "/user/bossman"
# )))
# from trustcafeapiwrapper.wrappers.post.update_post import update_post
# save_response(API.wrapped(update_post(
# post_text="This is an updated version of the test post created via the create_post wrapper function.",
# post_path="/post/1775143460-ef45186a",
# parent_path="/",
# )))