OjciecTadeusz commited on
Commit
61d4d9d
·
verified ·
1 Parent(s): 7601a82

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -0
app.py ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import FastAPI, Header, HTTPException
2
+ import requests
3
+ import os
4
+
5
+ app = FastAPI()
6
+ AUTH_TOKEN = os.getenv("API_TOKEN")
7
+
8
+ def load_dataset():
9
+ url = "https://huggingface.co/datasets/OjciecTadeusz/test/raw/main/data.json"
10
+ return requests.get(url).json()
11
+
12
+ async def auth(authorization: str | None = Header(default=None)):
13
+ if not authorization or authorization.replace("Bearer ", "") != AUTH_TOKEN:
14
+ raise HTTPException(status_code=401, detail="Unauthorized")
15
+
16
+ @app.get("/rows")
17
+ async def get_rows(limit: int = 100, authorization: str | None = Header(default=None)):
18
+ await auth(authorization)
19
+ if not (1 <= limit <= 100):
20
+ raise HTTPException(status_code=400, detail="Invalid limit")
21
+
22
+ return load_dataset()[:limit]
23
+
24
+ @app.get("/row")
25
+ async def get_row(id: str, authorization: str | None = Header(default=None)):
26
+ await auth(authorization)
27
+ data = load_dataset()
28
+ for r in data:
29
+ if r["id"] == id:
30
+ return r
31
+ raise HTTPException(status_code=404, detail="Not found")