-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPI.py
More file actions
88 lines (52 loc) · 1.83 KB
/
API.py
File metadata and controls
88 lines (52 loc) · 1.83 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
88
from flask import Flask, render_template
from local_simple_database import LocalDictDatabase
from os import getenv
import ast
LDD = LocalDictDatabase(
str_path_database_dir=".",
default_value=None,
)
db = LDD["dict_balances"]
transactions = LDD['dict_transactions']
def history(inp: str) -> dict:
out = []
t = str(transactions)
t = ast.literal_eval(t)
for i in t:
if t[i]['from'] == inp or t[i]['to'] == inp:
out.append({'timestamp': t[i]['timestamp'], 'from': t[i]['from'], 'to': t[i]['to'], 'amount': t[i]['amount'], 'id': t[i]['id']})
history = {'history': out}
return history
app = Flask(__name__)
@app.route('/')
def home():
return """
This is the ScratchCredit API!<br>
<b>/balance/[user]</b> returns the specified user's balance. [<a href='/balance/yousseftv'>example</a>]<br>
<b>/transaction/[id]</b> returns info about the specified transaction. [<a href='/transaction/1683315285'>example</a>]<br>
<b>/history/[user]</b> returns the specified user's transaction history. [<a href='/history/yousseftv'>example</a>]<br><br>
"""
@app.route('/home')
def main() -> str:
stylesheet = open("templates/index.css", "r").read()
return render_template('index.html', stylesheet=stylesheet)
@app.route('/balance/<user>')
def getBalance(user: str) -> dict:
try:
out = {"balance": db[user.lower()]}
except KeyError:
out = {"message": "user not found"}
return out
@app.route('/transaction/<id>')
def getByID(id: int):
try:
out = transactions[id]
except KeyError:
out = {}
return out
@app.route('/history/<user>')
def getHistory(user: str):
return history(user.lower())
if __name__ == '__main__':
debug_mode = getenv('FLASK_ENV') == 'development'
app.run(host="0.0.0.0", port=81, debug=debug_mode)