SPB Git forge

spb/hfmarketdata

Public

Open high-frequency market data platform — FirstRate full-history downloader, DuckDB/Parquet lake, open REST API and React docs platform (www.hfmarketdata.io)

127commits 1branches 0releases
24.7 MBsize
maindefault branch
11 days agolast push
JavaScript 53.7% Python 38.3% CSS 4.6% TypeScript 3.1%
1.9 KB · 44 lines python
Raw Blame History
1"""Probe endpoints mounted ONLY in the test app (under /v1/_test/) to exercise the rate-limit contract:2row accounting (json/csv/parquet), quota_exempt, request_cost, requires_key, key-required tags, clamp_limit.3"""4from __future__ import annotations56import numpy as np7import pandas as pd8from fastapi import FastAPI, Query, Request910from core.responses import clamp_limit, frame_response, json_response, parse_format111213def install(app: FastAPI) -> None:14    def frame(request: Request, n: int = Query(10), format: str = Query("json"), limit: int | None = Query(None)):15        lim = clamp_limit(limit, 10_000, 1_000_000, request) if limit is not None else n16        rows = min(n, lim)17        df = pd.DataFrame({"i": np.arange(rows), "x": np.linspace(0, 1, rows) if rows else []})18        return frame_response(df, parse_format(format), request=request)1920    def exempt(request: Request, n: int = Query(1000)):21        request.state.quota_exempt = True22        return json_response([{"i": i} for i in range(n)])2324    def expensive(request: Request):25        request.state.request_cost = 226        return json_response({"ok": True})2728    def needkey(request: Request):29        request.state.requires_key = True30        return json_response({"secret": True})3132    def tagged():33        return json_response({"stream": True})3435    def boom():36        raise RuntimeError("kaboom")3738    app.router.add_api_route("/v1/_test/frame", frame, methods=["GET"], include_in_schema=False)39    app.router.add_api_route("/v1/_test/exempt", exempt, methods=["GET"], include_in_schema=False)40    app.router.add_api_route("/v1/_test/expensive", expensive, methods=["GET"], include_in_schema=False)41    app.router.add_api_route("/v1/_test/needkey", needkey, methods=["GET"], include_in_schema=False)42    app.router.add_api_route("/v1/_test/tagged", tagged, methods=["GET"], tags=["stream"], include_in_schema=False)43    app.router.add_api_route("/v1/_test/boom", boom, methods=["GET"], include_in_schema=False)44