Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 38 additions & 43 deletions request_final.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,83 @@
app = Flask(__name__)

base_url = "/api/v1"
data_path = os.getcwd() + "/data.json"
data_path = f"{os.getcwd()}/data.json"
Copy link
Author

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:


#print(os.environ['LOG_LEVEL'])
# GET
@app.route(base_url + "/")
@app.route(f"{base_url}/")
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function all refactored with the following changes:

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function specific_id refactored with the following changes:


# 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function test refactored with the following changes:

# 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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function user refactored with the following changes:

This removes the following comments ( why? ):

#return jsonify(info = user_data['data'][i-1])

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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function count refactored with the following changes:

def count():
with open(data_path, 'r') as f:
user_data = json.load(f)
Expand Down