-
Notifications
You must be signed in to change notification settings - Fork 0
Sourcery refactored master branch #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,88 +6,83 @@ | |
app = Flask(__name__) | ||
|
||
base_url = "/api/v1" | ||
data_path = os.getcwd() + "/data.json" | ||
data_path = f"{os.getcwd()}/data.json" | ||
|
||
#print(os.environ['LOG_LEVEL']) | ||
# GET | ||
@app.route(base_url + "/") | ||
@app.route(f"{base_url}/") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
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>") | ||
@app.route(f"{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]) | ||
return jsonify( | ||
error=None, | ||
message=f"Object with ID {t} is displayed", | ||
data=user_data['data'][t - 1], | ||
) | ||
Comment on lines
-19
to
+27
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
|
||
# POST | ||
@app.route(base_url + "/people" , methods = ["POST","GET"]) | ||
@app.route(f"{base_url}/people", methods = ["POST","GET"]) | ||
def test(): | ||
if request.method == "POST": | ||
if 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']}) | ||
elif 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") | ||
return jsonify(error = "present", message = "empty json obj") | ||
if a in user_data['data']: | ||
return jsonify(error = "present", message = "duplication") | ||
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 | ||
|
||
|
||
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']}) | ||
Comment on lines
-26
to
-54
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
# return (user_data['data']) | ||
# PUT | ||
@app.route(base_url + "/people/data/<int:i>" , methods = ["PUT" , "GET","DELETE"]) | ||
@app.route(f"{base_url}/people/data/<int:i>", methods = ["PUT" , "GET","DELETE"]) | ||
def user(i): | ||
if request.method == "PUT": | ||
request_data = request.json | ||
|
||
if request.method == "DELETE": | ||
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]) | ||
|
||
|
||
return jsonify(error = None, message=f"Obj with ID {i} is DELETED") | ||
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']}) | ||
Comment on lines
-57
to
88
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
This removes the following comments ( why? ):
|
||
elif 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") | ||
|
||
@app.route(base_url + "/people/count") | ||
@app.route(f"{base_url}/people/count") | ||
Comment on lines
-90
to
+85
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
def count(): | ||
with open(data_path, 'r') as f: | ||
user_data = json.load(f) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lines
9-9
refactored with the following changes:use-fstring-for-concatenation
)