Skip to content

Commit d0dd1ea

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent c0d7992 commit d0dd1ea

File tree

1 file changed

+38
-43
lines changed

1 file changed

+38
-43
lines changed

request_final.py

Lines changed: 38 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,88 +6,83 @@
66
app = Flask(__name__)
77

88
base_url = "/api/v1"
9-
data_path = os.getcwd() + "/data.json"
9+
data_path = f"{os.getcwd()}/data.json"
1010

1111
#print(os.environ['LOG_LEVEL'])
1212
# GET
13-
@app.route(base_url + "/")
13+
@app.route(f"{base_url}/")
1414
def all():
1515
with open (data_path, 'r') as f:
1616
user_data= json.load(f)
1717
return jsonify(error = None, message = "All Objects Are Displayed", data = user_data['data'])
1818

19-
@app.route(base_url + "/people/<int:t>")
19+
@app.route(f"{base_url}/people/<int:t>")
2020
def specific_id(t):
2121
with open (data_path, 'r') as f:
2222
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+
)
2428

2529
# POST
26-
@app.route(base_url + "/people" , methods = ["POST","GET"])
30+
@app.route(f"{base_url}/people", methods = ["POST","GET"])
2731
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":
2938
a = request.json
30-
39+
3140
with open(data_path, 'r') as f: #'r' - shows that the file should be read
3241
user_data = json.load(f) #json.load - used to deserialize/decode(convert json to py)
33-
42+
3443
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")
3647
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+
4351
with open(data_path,'w') as f:
4452
json.dump(user_data,f) #json.dump- used to serialize/encode(converting py to json)
4553
#json.dump is used to write data to a file.| json.dumps() is used to write to a python string
4654
# dump-converts dict of python to obj in json file.| dumps-converts dict obj of py to json string format
4755
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']})
5556
# return (user_data['data'])
5657
# 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"])
5859
def user(i):
59-
if request.method == "PUT":
60-
request_data = request.json
61-
60+
if request.method == "DELETE":
6261
with open(data_path , 'r') as f:
6362
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])
7163

72-
elif request.method == "DELETE":
73-
74-
with open(data_path , 'r') as f:
75-
user_data = json.load(f)
76-
7764
user_data['data'].pop(i-1)
7865

7966
with open(data_path,'w') as f:
8067
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")
8569
elif request.method == "GET":
8670
with open(data_path , 'r') as f:
8771
user_data = json.load(f)
8872
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")
8984

90-
@app.route(base_url + "/people/count")
85+
@app.route(f"{base_url}/people/count")
9186
def count():
9287
with open(data_path, 'r') as f:
9388
user_data = json.load(f)

0 commit comments

Comments
 (0)