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,64 @@
|
||||
# retoor <retoor@molodetz.nl>
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import os
|
||||
from typing import Iterator
|
||||
|
||||
from client import DevPlace
|
||||
|
||||
URL = os.environ.get("DEVPLACE_URL", "https://devplace.net")
|
||||
API_KEY = os.environ.get("DEVPLACE_API_KEY", "YOUR_API_KEY")
|
||||
|
||||
|
||||
def all_feed_posts(dp: DevPlace) -> Iterator[dict]:
|
||||
cursor = None
|
||||
while True:
|
||||
page = dp.call("feed.list", before=cursor) if cursor else dp.call("feed.list")
|
||||
for item in page["posts"]:
|
||||
yield item
|
||||
cursor = page.get("next_cursor")
|
||||
if not cursor:
|
||||
return
|
||||
|
||||
|
||||
def create_post(dp: DevPlace, content: str, title: str = "") -> str:
|
||||
result = dp.call("posts.create", content=content, title=title)
|
||||
return result["data"]["slug"]
|
||||
|
||||
|
||||
def reply_to_post(dp: DevPlace, post_slug: str, content: str) -> None:
|
||||
detail = dp.call("posts.detail", post_slug=post_slug)
|
||||
dp.call("comments.create", content=content, target_uid=detail["post"]["uid"])
|
||||
|
||||
|
||||
def upvote_post(dp: DevPlace, post_uid: str) -> None:
|
||||
dp.call("votes.cast", target_type="post", target_uid=post_uid, value="1")
|
||||
|
||||
|
||||
def react_to_post(dp: DevPlace, post_uid: str, emoji: str = "rocket") -> None:
|
||||
dp.call("reactions.toggle", target_type="post", target_uid=post_uid, emoji=emoji)
|
||||
|
||||
|
||||
def follow(dp: DevPlace, username: str) -> None:
|
||||
dp.call("follow.user", username=username)
|
||||
|
||||
|
||||
def send_message(dp: DevPlace, receiver_uid: str, content: str) -> None:
|
||||
dp.call("messages.send", content=content, receiver_uid=receiver_uid)
|
||||
|
||||
|
||||
def main() -> None:
|
||||
dp = DevPlace(URL, api_key=API_KEY)
|
||||
slug = create_post(dp, "Posted from the XML-RPC recipes example.", title="Hello")
|
||||
print("created post:", slug)
|
||||
reply_to_post(dp, slug, "And here is an automated reply.")
|
||||
print("listing the first few feed posts:")
|
||||
for index, item in enumerate(all_feed_posts(dp)):
|
||||
print("-", item["author"]["username"], ":", item["post"].get("title") or item["post"]["uid"])
|
||||
if index >= 4:
|
||||
break
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user