"""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}")