mirror of
https://github.com/supleed2/ELEC60013-ES-CW1.git
synced 2024-11-10 01:35:50 +00:00
Minor bug fix regarding JSON responses
This commit is contained in:
parent
c5007b774f
commit
c661fe3595
|
@ -10,7 +10,8 @@ authentication = Blueprint('authentication', __name__)
|
||||||
def register():
|
def register():
|
||||||
body = request.json
|
body = request.json
|
||||||
if body is None:
|
if body is None:
|
||||||
return Response("{'error':'Invalid request - please provide a body'}", status=400, mimetype='application/json')
|
resp = {'error': 'Invalid request - please provide a body'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
email = body['email']
|
email = body['email']
|
||||||
password = body['password']
|
password = body['password']
|
||||||
|
@ -19,7 +20,8 @@ def register():
|
||||||
|
|
||||||
# Some fields are not present
|
# Some fields are not present
|
||||||
if email is None or password is None or name is None or deviceId is None:
|
if email is None or password is None or name is None or deviceId is None:
|
||||||
return Response("{'error':'Entries missing'}", status=400, mimetype='application/json')
|
resp = {'error': 'Entries missing'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
# Register user with Firebase authentication
|
# Register user with Firebase authentication
|
||||||
try:
|
try:
|
||||||
|
@ -30,8 +32,8 @@ def register():
|
||||||
display_name=name,
|
display_name=name,
|
||||||
disabled=False)
|
disabled=False)
|
||||||
except EmailAlreadyExistsError:
|
except EmailAlreadyExistsError:
|
||||||
return Response("{'error':'User with given email address already exists'}", status=409, mimetype='application/json')
|
resp = {'error': 'User with given email address already exists'}
|
||||||
|
return Response(json.dumps(resp), status=409, mimetype='application/json')
|
||||||
# Prompt the user to get verified
|
# Prompt the user to get verified
|
||||||
code = lib.utils.saveVerificationCode(user.uid)
|
code = lib.utils.saveVerificationCode(user.uid)
|
||||||
lib.utils.sendVerificationMail(name, email, code)
|
lib.utils.sendVerificationMail(name, email, code)
|
||||||
|
@ -50,7 +52,8 @@ def register():
|
||||||
def verify():
|
def verify():
|
||||||
body = request.json
|
body = request.json
|
||||||
if body is None:
|
if body is None:
|
||||||
return Response("{'error':'Invalid request - please provide a body'}", status=400, mimetype='application/json')
|
resp = {'error': 'Invalid request - please provide a body'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
uid = body['uid']
|
uid = body['uid']
|
||||||
code = body['code']
|
code = body['code']
|
||||||
|
@ -60,12 +63,14 @@ def verify():
|
||||||
if doc.to_dict()['code'] == code:
|
if doc.to_dict()['code'] == code:
|
||||||
auth.update_user(uid, email_verified=True)
|
auth.update_user(uid, email_verified=True)
|
||||||
firestore.client().collection(u'verification').document(uid).delete()
|
firestore.client().collection(u'verification').document(uid).delete()
|
||||||
return Response("{'error':'User verified'}", status=200, mimetype='application/json')
|
resp = {'success': 'User verified'}
|
||||||
|
return Response(json.dumps(resp), status=200, mimetype='application/json')
|
||||||
else:
|
else:
|
||||||
return Response("{'error':'Invalid code'}", status=400, mimetype='application/json')
|
resp = {'error': 'Invalid code'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
else:
|
else:
|
||||||
user = auth.get_user(uid)
|
user = auth.get_user(uid)
|
||||||
code = lib.utils.saveVerificationCode(user.uid)
|
code = lib.utils.saveVerificationCode(user.uid)
|
||||||
lib.utils.sendVerificationMail(user.display_name, user.email, code)
|
lib.utils.sendVerificationMail(user.display_name, user.email, code)
|
||||||
return Response("{'error':'Server couldn't find code, creating new one and sending email'}", status=500, mimetype='application/json')
|
resp = {'error': 'Server could not find code, creating new one and sending email'}
|
||||||
|
return Response(json.dumps(resp), status=500, mimetype='application/json')
|
||||||
|
|
13
api/data.py
13
api/data.py
|
@ -3,18 +3,19 @@ import json
|
||||||
from flask import Response, Blueprint, request
|
from flask import Response, Blueprint, request
|
||||||
from firebase_admin import firestore
|
from firebase_admin import firestore
|
||||||
|
|
||||||
|
|
||||||
data = Blueprint('data', __name__)
|
data = Blueprint('data', __name__)
|
||||||
|
|
||||||
@data.route('/readings/save', methods=['POST'])
|
@data.route('/readings/save', methods=['POST'])
|
||||||
def uploadReadings():
|
def uploadReadings():
|
||||||
deviceId = request.headers.get('deviceid')
|
deviceId = request.headers.get('deviceid')
|
||||||
if deviceId is None:
|
if deviceId is None:
|
||||||
return Response("{'error':'Device not specified'}", status=400, mimetype='application/json')
|
resp = {'error': 'Device not specified'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
body = request.json
|
body = request.json
|
||||||
if body is None:
|
if body is None:
|
||||||
return Response("{'error':'Invalid request - please provide a body'}", status=400, mimetype='application/json')
|
resp = {'error': 'Invalid request - please provide a body'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
body['timestamp'] = time.time()
|
body['timestamp'] = time.time()
|
||||||
|
|
||||||
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
|
@ -27,13 +28,15 @@ def uploadReadings():
|
||||||
|
|
||||||
upload = {'data': data}
|
upload = {'data': data}
|
||||||
firestore.client().collection(u'readings').document(deviceId).set(upload)
|
firestore.client().collection(u'readings').document(deviceId).set(upload)
|
||||||
return Response("{'success':'Data saved'}", status=200, mimetype='application/json')
|
resp = {'success': 'Data saved'}
|
||||||
|
return Response(json.dumps(resp), status=200, mimetype='application/json')
|
||||||
|
|
||||||
@data.route('/readings/getall', methods=['GET'])
|
@data.route('/readings/getall', methods=['GET'])
|
||||||
def getAllReadings():
|
def getAllReadings():
|
||||||
deviceId = request.headers.get('deviceid')
|
deviceId = request.headers.get('deviceid')
|
||||||
if deviceId is None:
|
if deviceId is None:
|
||||||
return Response("{'error':'Device not specified'}", status=400, mimetype='application/json')
|
resp = {'error': 'Device not specified'}
|
||||||
|
return Response(json.dumps(resp), status=400, mimetype='application/json')
|
||||||
|
|
||||||
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
doc = firestore.client().collection(u'readings').document(deviceId).get()
|
||||||
if doc.exists:
|
if doc.exists:
|
||||||
|
|
Loading…
Reference in a new issue