|
6 | 6 | app = Flask(__name__) |
7 | 7 |
|
8 | 8 | base_url = "/api/v1" |
9 | | -data_path = os.getcwd() + "/data.json" |
| 9 | +data_path = f"{os.getcwd()}/data.json" |
10 | 10 |
|
11 | 11 | #print(os.environ['LOG_LEVEL']) |
12 | 12 | # GET |
13 | | -@app.route(base_url + "/") |
| 13 | +@app.route(f"{base_url}/") |
14 | 14 | def all(): |
15 | 15 | with open (data_path, 'r') as f: |
16 | 16 | user_data= json.load(f) |
17 | 17 | return jsonify(error = None, message = "All Objects Are Displayed", data = user_data['data']) |
18 | 18 |
|
19 | | -@app.route(base_url + "/people/<int:t>") |
| 19 | +@app.route(f"{base_url}/people/<int:t>") |
20 | 20 | def specific_id(t): |
21 | 21 | with open (data_path, 'r') as f: |
22 | 22 | user_data= json.load(f) |
23 | | - return jsonify(error = None, message = "Object with ID %s is displayed" % t , data = user_data['data'][t-1]) |
| 23 | + return jsonify( |
| 24 | + error=None, |
| 25 | + message=f"Object with ID {t} is displayed", |
| 26 | + data=user_data['data'][t - 1], |
| 27 | + ) |
24 | 28 |
|
25 | 29 | # POST |
26 | | -@app.route(base_url + "/people" , methods = ["POST","GET"]) |
| 30 | +@app.route(f"{base_url}/people", methods = ["POST","GET"]) |
27 | 31 | def test(): |
28 | | - if request.method == "POST": |
| 32 | + if request.method == "GET": |
| 33 | + with open(data_path, 'r') as f: |
| 34 | + user_data = json.load(f) |
| 35 | + #print (user_data) |
| 36 | + return jsonify({"error":None, "message":"Here is the required data", "data":user_data['data']}) |
| 37 | + elif request.method == "POST": |
29 | 38 | a = request.json |
30 | | - |
| 39 | + |
31 | 40 | with open(data_path, 'r') as f: #'r' - shows that the file should be read |
32 | 41 | user_data = json.load(f) #json.load - used to deserialize/decode(convert json to py) |
33 | | - |
| 42 | + |
34 | 43 | if len(a) == 0: |
35 | | - return jsonify(error = "present", message = "empty json obj") |
| 44 | + return jsonify(error = "present", message = "empty json obj") |
| 45 | + if a in user_data['data']: |
| 46 | + return jsonify(error = "present", message = "duplication") |
36 | 47 | else: |
37 | | - if a in user_data['data']: |
38 | | - return jsonify(error = "present", message = "duplication") |
39 | | - else: |
40 | | - user_data['data'].append(a) #append - used to add an info in the existing dict |
41 | | - |
42 | | - |
| 48 | + user_data['data'].append(a) #append - used to add an info in the existing dict |
| 49 | + |
| 50 | + |
43 | 51 | with open(data_path,'w') as f: |
44 | 52 | json.dump(user_data,f) #json.dump- used to serialize/encode(converting py to json) |
45 | 53 | #json.dump is used to write data to a file.| json.dumps() is used to write to a python string |
46 | 54 | # dump-converts dict of python to obj in json file.| dumps-converts dict obj of py to json string format |
47 | 55 | return jsonify(error=None, message = "received sucesully") |
48 | | - |
49 | | - |
50 | | - elif request.method == "GET": |
51 | | - with open(data_path, 'r') as f: |
52 | | - user_data = json.load(f) |
53 | | - #print (user_data) |
54 | | - return jsonify({"error":None, "message":"Here is the required data", "data":user_data['data']}) |
55 | 56 | # return (user_data['data']) |
56 | 57 | # PUT |
57 | | -@app.route(base_url + "/people/data/<int:i>" , methods = ["PUT" , "GET","DELETE"]) |
| 58 | +@app.route(f"{base_url}/people/data/<int:i>", methods = ["PUT" , "GET","DELETE"]) |
58 | 59 | def user(i): |
59 | | - if request.method == "PUT": |
60 | | - request_data = request.json |
61 | | - |
| 60 | + if request.method == "DELETE": |
62 | 61 | with open(data_path , 'r') as f: |
63 | 62 | user_data = json.load(f) |
64 | | - |
65 | | - user_data['data'][i-1].update(request_data) |
66 | | - |
67 | | - with open(data_path,'w') as f: |
68 | | - json.dump(user_data,f) |
69 | | - return jsonify(error = None, message = "Latest Obj Updated") |
70 | | - #return jsonify(info = user_data['data'][i-1]) |
71 | 63 |
|
72 | | - elif request.method == "DELETE": |
73 | | - |
74 | | - with open(data_path , 'r') as f: |
75 | | - user_data = json.load(f) |
76 | | - |
77 | 64 | user_data['data'].pop(i-1) |
78 | 65 |
|
79 | 66 | with open(data_path,'w') as f: |
80 | 67 | json.dump(user_data,f) |
81 | | - return jsonify(error = None, message = "Obj with ID %s is DELETED" % i ) |
82 | | - #return jsonify(info = user_data['data'][i-1]) |
83 | | - |
84 | | - |
| 68 | + return jsonify(error = None, message=f"Obj with ID {i} is DELETED") |
85 | 69 | elif request.method == "GET": |
86 | 70 | with open(data_path , 'r') as f: |
87 | 71 | user_data = json.load(f) |
88 | 72 | return jsonify({"error":None, "message":"Here is the required data", "data":user_data['data']}) |
| 73 | + elif request.method == "PUT": |
| 74 | + request_data = request.json |
| 75 | + |
| 76 | + with open(data_path , 'r') as f: |
| 77 | + user_data = json.load(f) |
| 78 | + |
| 79 | + user_data['data'][i-1].update(request_data) |
| 80 | + |
| 81 | + with open(data_path,'w') as f: |
| 82 | + json.dump(user_data,f) |
| 83 | + return jsonify(error = None, message = "Latest Obj Updated") |
89 | 84 |
|
90 | | -@app.route(base_url + "/people/count") |
| 85 | +@app.route(f"{base_url}/people/count") |
91 | 86 | def count(): |
92 | 87 | with open(data_path, 'r') as f: |
93 | 88 | user_data = json.load(f) |
|
0 commit comments