Spaces:
Sleeping
Sleeping
File size: 1,271 Bytes
61d4d9d 8cee326 61d4d9d 8cee326 018266e 61d4d9d 8cee326 61d4d9d 018266e 61d4d9d 018266e 61d4d9d 8cee326 61d4d9d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
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")
@app.get("/health")
async def health():
return {"status": "ok"}
@app.get("/rows")
async def get_rows(limit: int = 3, authorization: str | None = Header(default=None)):
await auth(authorization)
data = load_dataset()
return data[:limit]
@app.get("/row")
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")
|