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