APIarium / test_gist.py
3v324v23's picture
Clean snapshot
fc0b00d
Raw
History Blame Contribute Delete
2.12 kB
"""Quick test: read/write API keys to GitHub Gist.
Usage: GITHUB_TOKEN=xxx GIST_ID=yyy python test_gist.py
"""
import httpx
import json
import os
import time
TOKEN = os.environ.get("GITHUB_TOKEN", "")
GIST_ID = os.environ.get("GIST_ID", "")
if not TOKEN or not GIST_ID:
print("❌ Set GITHUB_TOKEN and GIST_ID environment variables first.")
print(" GITHUB_TOKEN=xxx GIST_ID=yyy python test_gist.py")
exit(1)
print(f"πŸ”‘ Token: {TOKEN[:8]}...{TOKEN[-4:]}")
print(f"πŸ“ Gist ID: {GIST_ID}")
print()
# 1. Read current keys
print("── Reading Gist... ──")
resp = httpx.get(
f"https://api.github.com/gists/{GIST_ID}",
headers={"Authorization": f"Bearer {TOKEN}"},
timeout=10,
)
print(f"Status: {resp.status_code}")
if resp.status_code != 200:
print(f"❌ Failed: {resp.text}")
exit(1)
gist = resp.json()
content = gist.get("files", {}).get("api_keys.json", {}).get("content", "{}")
keys = json.loads(content)
print(f"βœ… Found {len(keys)} existing key(s)")
for k, v in keys.items():
print(f" {k} β†’ IP: {v.get('ip')}, RPM: {v.get('rpm')}")
print()
# 2. Write a test key
test_key = f"test-{int(time.time())}"
keys[test_key] = {
"ip": "127.0.0.1",
"created": int(time.time()),
"rpm": 6,
"note": "test entry β€” delete me",
}
print(f"── Writing test key: {test_key} ──")
resp = httpx.patch(
f"https://api.github.com/gists/{GIST_ID}",
headers={
"Authorization": f"Bearer {TOKEN}",
"Content-Type": "application/json",
},
json={"files": {"api_keys.json": {"content": json.dumps(keys, indent=2)}}},
timeout=10,
)
print(f"Status: {resp.status_code}")
if resp.status_code == 200:
print("βœ… Write successful!")
# Clean up test key
del keys[test_key]
httpx.patch(
f"https://api.github.com/gists/{GIST_ID}",
headers={"Authorization": f"Bearer {TOKEN}", "Content-Type": "application/json"},
json={"files": {"api_keys.json": {"content": json.dumps(keys, indent=2)}}},
timeout=10,
)
print("🧹 Test key cleaned up.")
else:
print(f"❌ Failed: {resp.text}")