-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
87 lines (70 loc) · 2.3 KB
/
main.py
File metadata and controls
87 lines (70 loc) · 2.3 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import os
import uvicorn
import traceback
import math
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.middleware.gzip import GZipMiddleware
from dotenv import load_dotenv
from supabase import create_client, Client
load_dotenv()
app = FastAPI()
# Adicionando middleware para habilitar o CORS
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.add_middleware(GZipMiddleware)
valid_apikeys = eval(os.getenv("valid_apikeys"))
keys = eval(os.getenv("keys"))
def stream_data(url, apk, tbl_name, page):
supabase: Client = create_client(url, apk)
if page == 0:
tbl = supabase.table(tbl_name).select("*").execute()
records = tbl.data
else:
tbl = supabase.table(tbl_name).select("*").eq("page", page).execute()
records = tbl.data
if page == 0:
return records
else:
return {
"page": page,
"records_on_this_page": len(records),
"data": records,
}
@app.get("/")
def default():
result = "The API is UP!"
return result
@app.get("/retrieve_page/{tbl_name}")
def read_data(tbl_name: str, apikey: str, page: int = 1):
try:
if apikey in valid_apikeys:
url = os.getenv(f"{keys[apikey]}_SUPABASE_URL")
apk = os.getenv(f"{keys[apikey]}_SUPABASE_KEY")
dados = stream_data(url, apk, tbl_name, page)
return dados
else:
raise HTTPException(status_code=500, detail="Invalid APIKEY.")
except:
print(traceback.format_exc())
raise HTTPException(status_code=404, detail="No data found.")
@app.get("/retrieve/{tbl_name}")
def read_data(tbl_name: str, apikey: str):
try:
if apikey in valid_apikeys:
url = os.getenv(f"{keys[apikey]}_SUPABASE_URL")
apk = os.getenv(f"{keys[apikey]}_SUPABASE_KEY")
dados = stream_data(url, apk, tbl_name, 0)
return dados
else:
raise HTTPException(status_code=500, detail="Invalid APIKEY.")
except:
print(traceback.format_exc())
raise HTTPException(status_code=404, detail="No data found.")
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=8000)