-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
58 lines (45 loc) · 1.83 KB
/
api.py
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
import asyncpg
from fastapi import FastAPI, Request
from fastapi.responses import JSONResponse
app = FastAPI()
class Database():
async def create_db_pool(self):
try:
self.pool = await asyncpg.create_pool(
host="DEPRECATED",
database="RoPerks",
user="postgres",
password="e0Hu7AgF*24o7Hf$H&3o",
)
except asyncpg.exceptions.InvalidAuthorizationSpecificationError:
self.pool = await asyncpg.create_pool(
host="localhost",
database="RoPerks",
user="postgres",
password="e0Hu7AgF*24o7Hf$H&3o",
)
async def get_nitro_status(self, api_key, roblox_id):
guild_id = await self.pool.fetchval(f'SELECT guild_id FROM guild_dump WHERE api_key=$1', api_key)
if not guild_id:
return JSONResponse(status_code=403, content={'detail': 'api-key was invalid.'})
discord_id = await self.pool.fetchval(f'SELECT discord_id FROM user_data WHERE guild_id=$1 AND roblox_id=$2', guild_id, roblox_id)
if discord_id:
return {"has_nitro": True}
return {"has_nitro": False}
def create_app():
app = FastAPI()
db = Database()
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
request.state.pool = db.pool
response = await call_next(request)
return response
@app.on_event("startup")
async def startup():
await db.create_db_pool()
@app.get("/nitro_status/{roblox_id}")
async def nitro_status(request: Request, roblox_id: int, api_key: str):
nitro_status = await db.get_nitro_status(api_key, roblox_id)
return nitro_status
return app
app = create_app()