Spaces:
Sleeping
Sleeping
| from fastapi import FastAPI, Header, HTTPException | |
| import requests | |
| import os | |
| app = FastAPI() | |
| AUTH_TOKEN = os.getenv("AUTH_TOKEN") # poprawiona zmienna | |
| URL = os.getenv("URL") # URL do datasetu JSON | |
| def load_dataset(): | |
| if not URL: | |
| raise HTTPException(status_code=500, detail="Dataset URL not set") | |
| r = requests.get(URL) | |
| if r.status_code != 200: | |
| raise HTTPException(status_code=500, detail="Dataset fetch failed") | |
| return r.json() | |
| async def auth(authorization: str | None = Header(default=None)): | |
| if AUTH_TOKEN: | |
| token = authorization.replace("Bearer ", "") if authorization else None | |
| if token != AUTH_TOKEN: | |
| raise HTTPException(status_code=401, detail="Unauthorized") | |
| async def health(): | |
| return {"status": "ok"} | |
| async def get_rows(limit: int = 3, authorization: str | None = Header(default=None)): | |
| await auth(authorization) | |
| data = load_dataset() | |
| return data[:limit] | |
| async def get_row(id: str, authorization: str | None = Header(default=None)): | |
| await auth(authorization) | |
| data = load_dataset() | |
| for r in data: | |
| if r.get("id") == id: | |
| return r | |
| raise HTTPException(status_code=404, detail="Not found") | |