-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
38 lines (29 loc) · 914 Bytes
/
Copy pathapi.py
File metadata and controls
38 lines (29 loc) · 914 Bytes
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
import json
from flask import Flask as fla
def read_file(filename):
with open(filename, "r") as my_data:
data = my_data.read()
return data
def convert_data(json_data):
json_data = eval(json_data)
return json_data
def find_all(data):
return data
def find_user_by_id(data,id):
return data[id]
app = fla(__name__)
@app.route("/")
def Home():
return ("<h1>Data API</h1>\n<h2>Go to:</h2>\n<ul><li><strong>List all users :</strong> /users</li><li><strong>List user by id :</strong> /user/1</li></ul>")
@app.route("/users")
def Users():
return find_all(convert_data)
@app.route("/user/<id>")
def User(id):
return find_user_by_id(convert_data,int(id))
if __name__ == "__main__":
read_data = read_file("data.json")
convert_data = convert_data(read_data)
# print(find_all(convert_data))
# print(find_user_by_id(convert_data,1))
app.run(debug=True)