Uh oh!
There was an error while loading. Please reload this page.
This repository was archived by the owner on Dec 10, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
Latest commit
72 lines (62 loc) · 1.92 KB
/
Copy pathapi.py
File metadata and controls
72 lines (62 loc) · 1.92 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
importos
importdatetime, time
fromflaskimportFlask, request, render_template
fromutilsimportdb
importhashlib
fromdotenvimportload_dotenv
load_dotenv()
app=Flask(__name__)
@app.errorhandler(404)
defpage_not_found(e):
return"<h1>404</h1><p>The resource could not be found.</p>", 404
@app.route('/favicon.ico')
deffavicon():
returnapp.send_static_file('favicon.ico')
@app.route("/")
defhome():
returnrender_template("home.html")
@app.route("/api")
defapi():
returnrender_template("api.html")
@app.route("/api/ping")
defping():
try:
return {"ping": db["ping"]}
exceptExceptionase:
return {"ping": -1}
@app.route("/api/balance")
defbalance():
query_parameters=request.args
userid=query_parameters.get("userid")
ifuserid:
return {"balance": db["balance"][str(userid)]}
else:
returndb["balance"]
@app.route("/api/bank")
defbank():
query_parameters=request.args
userid=query_parameters.get("userid")
ifuserid:
return {"bank": db["bank"][str(userid)]} ifstr(userid) indb["bank"] else ({"message": "The user could not be found.", "status": 404}, 404)
else:
returndb["bank"]
@app.route("/api/inventory")
definventory():
query_parameters=request.args
userid=query_parameters.get("userid")
ifuserid:
returndb["inventory"][str(userid)] ifstr(userid) indb["inventory"] else ({"message": "The user could not be found.", "status": 404}, 404)
else:
returndb["inventory"]
@app.route("/api/testkeys")
deftest():
query_parameters=request.args
apikey=query_parameters.get("apikey")
ifnotapikey:
return {"error": "Missing API key"}, 400
hashed_apikey=hashlib.sha256(apikey.encode()).hexdigest()
ifhashed_apikeynotindb["apikeys"]:
return {"error": "Invalid API key"}, 404
ifnotdb["apikeys"][hashed_apikey]["isvalid"]:
return {"error": "API key is invalid"}, 401
return {"message": db["apikeys"][hashed_apikey]}