futures: paramètre limit sur /v1/futures/{root}/contracts (+ meta.total)
1 changed file +4 −1
modified
hfmarketdata/api/futures/routes.py
+4 −1
@@ -105,16 +105,19 @@ def list_contracts(request: Request, | ||
| 105 | 105 | from_: str | None = Query(None, alias="from", description="Expiration date ≥ (YYYY-MM-DD)"), |
| 106 | 106 | to: str | None = Query(None, description="Expiration date ≤ (YYYY-MM-DD)"), |
| 107 | 107 | sort: str = Query("expiration_date", description="expiration_date | symbol | first_data_date | last_data_date | volume_avg_daily | open_interest_last (prefix `-` = desc)"), |
| 108 | + limit: int | None = Query(None, ge=1, description="Maximum number of contracts returned (default 1 000, max 20 000)"), | |
| 108 | 109 | format: str = Query("json", description=_FORMAT_DOC)): |
| 109 | 110 | fmt = parse_format(format) |
| 110 | 111 | rows = service.contracts(root, status, service.parse_date(from_, "from"), service.parse_date(to, "to"), sort) |
| 112 | + total = len(rows) | |
| 113 | + rows = rows[: clamp_limit(limit, 1_000, 20_000, request)] | |
| 111 | 114 | for r in rows: |
| 112 | 115 | r.pop("updated_at", None) |
| 113 | 116 | if fmt != "json": |
| 114 | 117 | flat = [{**r, "timeframes": ",".join(r["timeframes"] or {}) if isinstance(r.get("timeframes"), dict) else r.get("timeframes"), |
| 115 | 118 | "files": None} for r in rows] |
| 116 | 119 | return frame_response(pd.DataFrame(flat), fmt, request=request, filename=f"{service.normalize_root(root)}_contracts") |
| 117 | − return json_response(service.jsonable(rows), meta={"root": service.normalize_root(root), "status": status, "sort": sort}) | |
| 120 | + return json_response(service.jsonable(rows), meta={"root": service.normalize_root(root), "status": status, "sort": sort, "total": total}) | |
| 118 | 121 | |
| 119 | 122 | |
| 120 | 123 | @router.get( |
| 121 | 124 | |