Skip to content
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

Update routes #12

Merged
merged 3 commits into from
Dec 26, 2020
Merged
Show file tree
Hide file tree
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
45 changes: 34 additions & 11 deletions app/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def add_participants(db, userDetails):
user = {
"name": userDetails["name"],
"email": userDetails["email"],
"role": userDetails["role"],
"organisation": userDetails["organisation"],
# storing the password as hash
"password": pbkdf2_sha256.hash(userDetails["password"]),
"frames": []
Expand Down Expand Up @@ -85,7 +87,7 @@ def authorise_participants(db, userDetails):
for user_id in participants:
user = participants[user_id]
if user and (user["email"] == userDetails["email"]) and (pbkdf2_sha256.verify(userDetails["password"], user["password"])):
return user_id
return (user_id, user)
return None
except Exception as e:
print('ERROR:', e)
Expand All @@ -105,26 +107,47 @@ def save_frame(db, token, frame):
try:
user_id = decode_auth_token(token)
if user_id!= None:
db.child('participants').child(user_id).child('frames').push(frame)
frame_id = db.child('participants').child(user_id).child('frames').push(frame)
frame_obj={
"frame_data": frame,
"frame_id": frame_id["name"]
}
print('* Frame added successfully')
return True
return frame_obj
else:
print('ERROR: Token Value is None')
return False
return None
except Exception as e:
print('ERROR: ', e)
return False
return None

@staticmethod
def get_frames(db, token):
try:
user_id = decode_auth_token(token)
frames = db.child('participants').child(
user_id).child('frames').get().val()
frame_arr = []
if frames!= None:
frame_arr = [frames[fid] for fid in frames]
return frame_arr
if user_id != None:
frames = db.child('participants').child(user_id).child('frames').get().val()
frame_arr = []
if frames!= None:
frame_arr = [{"frame_id":fid, "frame_data":frames[fid]} for fid in frames]
return frame_arr
else:
print('ERROR: Token Value is None')
return None
except Exception as e:
print('ERROR: ', e)
return None

@staticmethod
def delete_frames(db, token, frame_id):
try:
user_id = decode_auth_token(token)
if user_id != None:
db.child('participants').child(user_id).child('frames').child(frame_id).remove()
return True
else:
print('ERROR: Token Value is None')
return False
except Exception as e:
print('ERROR: ', e)
return False
46 changes: 25 additions & 21 deletions app/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,36 @@ def index():
def register():
user = request.json
if not Db.check_valid_details(database, user):
return make_response(jsonify({"status": "fail", "message": "User details invalid"})), 409
return make_response(jsonify({"message": "User details invalid"})), 409
elif Db.add_participants(database, user):
return make_response(jsonify({"status": "success", "message": "User added to database"})), 201
return make_response(jsonify({"message": "User added to database"})), 201
else:
return make_response(jsonify({"status": "fail", "message": "Internal Server error"})), 500
return make_response(jsonify({"message": "Internal Server error"})), 500


@app.route(BASE_URL+'/login', methods=['POST'])
@swag_from('../docs/login.yml')
def login():
user = request.json
user_id = Db.authorise_participants(database, user)
user_token=Db.get_token(database, user_id)
user_id, user_data = Db.authorise_participants(database, user)
user_token = Db.get_token(database, user_id)
if (user_id != None):
return make_response(jsonify({"status": "success", "token": user_token})), 202
return make_response(jsonify({"token": user_token, "data": user_data})), 202
else:
return make_response(jsonify({"status": "fail", "message": "Login failed"})), 401
return make_response(jsonify({"message": "Login failed"})), 401


@app.route(BASE_URL+'/frames', methods=['POST', 'GET'])
@app.route(BASE_URL+'/frames', methods=['POST', 'GET', 'DELETE'])
@swag_from('../docs/getframes.yml', methods=['GET'])
@swag_from('../docs/postframes.yml', methods=['POST'])
@swag_from('../docs/deleteframes.yml', methods=['DELETE'])
def frames():
auth_header = request.headers.get('Authorization')
if auth_header:
try:
auth_token = auth_header.split(" ")[1]
except IndexError:
responseObject = {
'status': 'fail',
'message': 'Bearer token malformed.'
}
return make_response(jsonify(responseObject)), 401
Expand All @@ -56,41 +56,45 @@ def frames():
if request.method == 'POST':
frame = request.json['frame']
if frame:
if Db.save_frame(database, auth_token, frame):
responseObject = {
'status': 'success',
'message': 'Frame added'
}
return make_response(jsonify(responseObject)), 200
responseObject = Db.save_frame(database, auth_token, frame)
if responseObject != None:
return make_response(jsonify(responseObject)), 201
else:
responseObject = {
'status': 'fail',
'message': 'Frame cannot be added'
}
return make_response(jsonify(responseObject)), 401
else:
responseObject = {
'status': 'fail',
'message': 'Provide valid frame data'
}
return make_response(jsonify(responseObject)), 401
elif request.method == 'GET':
frames_arr = Db.get_frames(database, auth_token)
if frames_arr != None:
responseObject = {
'status': 'success',
'frames': frames_arr
}
return make_response(jsonify(responseObject)), 201
else:
responseObject = {
'status': 'fail',
'message': 'Something went wrong'
}
return make_response(jsonify(responseObject)), 401
elif request.method == 'DELETE':
frame_id = request.args.get('id')
if Db.delete_frames(database, auth_token, frame_id):
responseObject = {
'message': 'Frame was deleted successfully'
}
return make_response(jsonify(responseObject)), 201
else:
responseObject = {
'message': 'Frame was not deleted!'
}
return make_response(jsonify(responseObject)), 400
else:
responseObject = {
'status': 'fail',
'message': 'Provide a valid auth token.'
}
return make_response(jsonify(responseObject)), 401
return make_response(jsonify(responseObject)), 401
22 changes: 22 additions & 0 deletions docs/deleteframes.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
Delete a frame of a user
The frame id has to be sent as a query along with the request.
---
tags:
- user
parameters:
- in: query
name: id
schema:
type: string
description: ID of the frame to be deleted
security:
- basicAuth: []
responses:
"201":
description: Frame was deleted successfully
schema:
$ref: '#/definitions/Frames'
"400":
description: Frame cannot be deleted
"401":
description: Access token is missing or invalid
11 changes: 9 additions & 2 deletions docs/getframes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ responses:
definitions:
Frames:
type: array
description: Array of Base64 encoded frames.
description: Array of Frame object
items:
type: string
type: object
properties:
frame_id:
type: string
description: Reference Id of the frame
frame_data:
type: string
description: Base64 encoded data of the frame
19 changes: 15 additions & 4 deletions docs/register.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ parameters:
required:
- name
- email
- role
- organisation
- password
properties:
name:
Expand All @@ -19,6 +21,12 @@ parameters:
email:
type: string
description: Email address of the participant
role:
type: string
description: Role of the user
organisation:
type: string
description: Organisation the user belongs to
password:
type: string
description: Strong password (must have 7 characters or more)
Expand Down Expand Up @@ -50,11 +58,14 @@ definitions:
email:
type: string
description: Email address of the participant
role:
type: string
description: Role of the user
organisation:
type: string
description: Organisation the user belongs to
password:
type: string
description: Strong password (must have 7 characters or more)
frames:
type: array
description: Base64 encoded images of the frames
items:
type: string
$ref: '#/definitions/Frames'