54 lines
3.4 KiB
Python
54 lines
3.4 KiB
Python
"""Local development fixture. Run: python tools/mock_proxy.py; adb reverse tcp:8765 tcp:8765."""
|
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
from urllib.parse import urlparse, parse_qs
|
|
import json
|
|
|
|
|
|
class Handler(BaseHTTPRequestHandler):
|
|
def do_POST(self):
|
|
if self.headers.get("Authorization") != "Bearer sk-demo":
|
|
return self.reply(401, {"error": {"message": "Invalid API key"}})
|
|
body = json.loads(self.rfile.read(int(self.headers.get("Content-Length", "0"))))
|
|
path = urlparse(self.path).path
|
|
if path == "/key/generate":
|
|
return self.reply(200, {"key": "sk-new-demo-key", "key_alias": body.get("key_alias")})
|
|
if path == "/team/new":
|
|
return self.reply(200, {"team_id": "new-team", "team_alias": body.get("team_alias")})
|
|
if path == "/model/new":
|
|
return self.reply(200, {"model_name": body.get("model_name")})
|
|
self.reply(404, {"error": {"message": "Not found"}})
|
|
|
|
def do_GET(self):
|
|
if self.headers.get("Authorization") != "Bearer sk-demo":
|
|
return self.reply(401, {"error": {"message": "Invalid API key"}})
|
|
url = urlparse(self.path)
|
|
page = int(parse_qs(url.query).get("page", ["1"])[0])
|
|
routes = {
|
|
"/model/info": {"data": [
|
|
{"model_name": "gpt-4o", "litellm_params": {"model": "openai/gpt-4o"}, "model_info": {"id": "model-1", "litellm_provider": "openai", "mode": "chat", "max_tokens": 128000}},
|
|
{"model_name": "claude-sonnet", "litellm_params": {"model": "anthropic/claude-sonnet"}, "model_info": {"id": "model-2", "litellm_provider": "anthropic", "mode": "chat"}},
|
|
]},
|
|
"/key/list": {"keys": [{"key_alias": "Production", "token": "hashed-token", "user_id": "alvin", "team_id": "core", "spend": 42.12, "max_budget": 100, "blocked": False}]},
|
|
"/team/list": [{"team_alias": "Core", "team_id": "core", "spend": 98.45, "max_budget": 250, "members": ["alvin", "sam"]}],
|
|
"/global/spend": {"spend": 234.56},
|
|
"/global/activity": {"sum_api_requests": 1234, "daily_data": [{"date": "2026-11-17", "api_requests": 12}, {"date": "2026-11-18", "api_requests": 18}]},
|
|
"/gateway/daily/activity": {"total_successful_requests": 1221, "total_failed_requests": 13, "by_date": [{"date": "2026-11-17", "successful_requests": 11, "failed_requests": 1}, {"date": "2026-11-18", "successful_requests": 18, "failed_requests": 0}]},
|
|
"/global/spend/report": [{"group_by_day": "2026-11-17T00:00:00+00:00", "total_spend": 4.5}, {"group_by_day": "2026-11-18T00:00:00+00:00", "total_spend": 7.9}],
|
|
"/spend/logs/v2": {"data": [{"request_id": f"req-{page}", "model": "gpt-4o", "startTime": "2026-11-19T08:00:00", "status": "success", "spend": 0.024, "total_tokens": 945, "team_id": "core"}], "total": 31, "page": page, "page_size": 30},
|
|
}
|
|
if url.path not in routes:
|
|
return self.reply(404, {"error": {"message": "Not found"}})
|
|
self.reply(200, routes[url.path])
|
|
|
|
def reply(self, status, value):
|
|
payload = json.dumps(value).encode()
|
|
self.send_response(status)
|
|
self.send_header("Content-Type", "application/json")
|
|
self.send_header("Content-Length", str(len(payload)))
|
|
self.end_headers()
|
|
self.wfile.write(payload)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
HTTPServer(("0.0.0.0", 8765), Handler).serve_forever()
|