forked from retoor/devplacepy
feat: add plug-and-play devRant API client examples with Python and JavaScript
Add complete set of example scripts for the devRant REST protocol under examples/devrant/, including reusable client libraries, a rant poster, a live feed watcher with keyword-based auto-upvote, and an end-to-end smoke test that exercises every endpoint. Both Python (stdlib-only) and JavaScript (Node 18+ global fetch) implementations are provided.
This commit is contained in:
@@ -0,0 +1,87 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
import os
|
||||
import time
|
||||
|
||||
from client import DevRant
|
||||
|
||||
BASE = os.environ.get("DEVRANT_BASE", "http://localhost:10500")
|
||||
|
||||
passed = 0
|
||||
failed = 0
|
||||
|
||||
|
||||
def check(label: str, condition: bool, detail: str = "") -> None:
|
||||
global passed, failed
|
||||
if condition:
|
||||
passed += 1
|
||||
print(f" PASS {label}")
|
||||
else:
|
||||
failed += 1
|
||||
print(f" FAIL {label} {detail}")
|
||||
|
||||
|
||||
def main() -> None:
|
||||
suffix = str(int(time.time()))
|
||||
username = f"dr_smoke_{suffix}"
|
||||
api = DevRant(BASE, username, "secret6")
|
||||
|
||||
reg = api.register(f"{username}@example.test")
|
||||
check("register", reg.get("success"), str(reg))
|
||||
|
||||
login = DevRant(BASE, username, "secret6").login()
|
||||
check("login returns token", bool(login.get("token_key")))
|
||||
api.login()
|
||||
|
||||
posted = api.post_rant("Smoke test rant about Python", "python,smoke,devrant")
|
||||
check("post rant", posted.get("success"))
|
||||
rant_id = posted.get("rant_id")
|
||||
|
||||
feed = api.rants(limit=5)
|
||||
check("feed returns rants", any(r["id"] == rant_id for r in feed))
|
||||
|
||||
detail = api.rant(rant_id)
|
||||
check("get rant tags round-trip", detail.get("rant", {}).get("tags") == ["python", "smoke", "devrant"])
|
||||
check("get rant editable", detail.get("rant", {}).get("editable") is True)
|
||||
|
||||
commented = api.comment(rant_id, "Nice rant!")
|
||||
check("comment", commented.get("success"))
|
||||
|
||||
voted = api.vote_rant(rant_id, 1)
|
||||
check("vote score", voted.get("rant", {}).get("score") == 1)
|
||||
check("vote state", voted.get("rant", {}).get("vote_state") == 1)
|
||||
|
||||
check("favorite", api.favorite(rant_id).get("success"))
|
||||
check("unfavorite", api.unfavorite(rant_id).get("success"))
|
||||
|
||||
uid = api.user_id(username)
|
||||
check("get-user-id", bool(uid))
|
||||
|
||||
profile = api.profile(uid)
|
||||
check("profile score", profile.get("score") == 1)
|
||||
check("profile counts", profile.get("content", {}).get("counts", {}).get("rants") == 1)
|
||||
|
||||
api.edit_profile(profile_about="I build with Python", profile_github="smoke")
|
||||
profile = api.profile(uid)
|
||||
check("edit-profile bio", profile.get("about") == "I build with Python")
|
||||
check("skills derived from bio", profile.get("skills") == "I build with Python")
|
||||
|
||||
notifs = api.notifs()
|
||||
check("notif-feed shape", "unread" in notifs and "items" in notifs)
|
||||
|
||||
deleted = api.delete_rant(rant_id)
|
||||
check("delete rant", deleted.get("success"))
|
||||
|
||||
bad = DevRant(BASE, username, "wrongpw")
|
||||
try:
|
||||
bad.login()
|
||||
check("bad login rejected", False)
|
||||
except RuntimeError:
|
||||
check("bad login rejected", True)
|
||||
|
||||
print(f"\n{passed} passed, {failed} failed")
|
||||
raise SystemExit(1 if failed else 0)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user