-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_final.py
More file actions
98 lines (75 loc) · 3.36 KB
/
request_final.py
File metadata and controls
98 lines (75 loc) · 3.36 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
89
90
91
92
93
94
95
96
97
98
from flask import Flask, jsonify, request
import json
import os
import requests
app = Flask(__name__)
base_url = "/api/v1"
data_path = os.getcwd() + "/data.json"
#print(os.environ['LOG_LEVEL'])
# GET
@app.route(base_url + "/")
def all():
with open (data_path, 'r') as f:
user_data= json.load(f)
return jsonify(error = None, message = "All Objects Are Displayed", data = user_data['data'])
@app.route(base_url + "/people/<int:t>")
def specific_id(t):
with open (data_path, 'r') as f:
user_data= json.load(f)
return jsonify(error = None, message = "Object with ID %s is displayed" % t , data = user_data['data'][t-1])
# POST
@app.route(base_url + "/people" , methods = ["POST","GET"])
def test():
if request.method == "POST":
a = request.json
with open(data_path, 'r') as f: #'r' - shows that the file should be read
user_data = json.load(f) #json.load - used to deserialize/decode(convert json to py)
if len(a) == 0:
return jsonify(error = "present", message = "empty json obj")
else:
if a in user_data['data']:
return jsonify(error = "present", message = "duplication")
else:
user_data['data'].append(a) #append - used to add an info in the existing dict
with open(data_path,'w') as f:
json.dump(user_data,f) #json.dump- used to serialize/encode(converting py to json)
#json.dump is used to write data to a file.| json.dumps() is used to write to a python string
# dump-converts dict of python to obj in json file.| dumps-converts dict obj of py to json string format
return jsonify(error=None, message = "received sucesully")
elif request.method == "GET":
with open(data_path, 'r') as f:
user_data = json.load(f)
#print (user_data)
return jsonify({"error":None, "message":"Here is the required data", "data":user_data['data']})
# return (user_data['data'])
# PUT
@app.route(base_url + "/people/data/<int:i>" , methods = ["PUT" , "GET","DELETE"])
def user(i):
if request.method == "PUT":
request_data = request.json
with open(data_path , 'r') as f:
user_data = json.load(f)
user_data['data'][i-1].update(request_data)
with open(data_path,'w') as f:
json.dump(user_data,f)
return jsonify(error = None, message = "Latest Obj Updated")
#return jsonify(info = user_data['data'][i-1])
elif request.method == "DELETE":
with open(data_path , 'r') as f:
user_data = json.load(f)
user_data['data'].pop(i-1)
with open(data_path,'w') as f:
json.dump(user_data,f)
return jsonify(error = None, message = "Obj with ID %s is DELETED" % i )
#return jsonify(info = user_data['data'][i-1])
elif request.method == "GET":
with open(data_path , 'r') as f:
user_data = json.load(f)
return jsonify({"error":None, "message":"Here is the required data", "data":user_data['data']})
@app.route(base_url + "/people/count")
def count():
with open(data_path, 'r') as f:
user_data = json.load(f)
return jsonify(error = None, message = "The count of objects is %d" % len(user_data['data']))
if __name__ == "__main__":
app.run(host = "0.0.0.0", port = 3000)