1+ # !/usr/bin/env python
2+ # -*- coding: utf-8 -*-
13"""Entry point for the server application."""
24
35import json
1012 JWTManager , jwt_required , create_jwt , get_jwt_identity , get_jwt
1113)
1214
13- from .app_utils import html_codes
15+ from .http_codes import Status
1416from .factory import create_app , create_user
1517
1618logger = logging .getLogger (__name__ )
@@ -49,9 +51,9 @@ def logout():
4951 # TODO: handle this logout properly, very weird implementation.
5052 identity = get_jwt_identity ()
5153 if not identity :
52- return jsonify ({"msg" : "Token invalid" }), 401
54+ return jsonify ({"msg" : "Token invalid" }), Status . HTTP_BAD_UNAUTHORIZED
5355 logger .info ('Logged out user !!' )
54- return 'logged out successfully' , 200
56+ return 'logged out successfully' , Status . HTTP_OK_BASIC
5557
5658
5759@app .route ('/api/login' , methods = ['POST' ])
@@ -64,13 +66,13 @@ def login():
6466 password = params .get ('password' , None )
6567
6668 if not username :
67- return jsonify ({"msg" : "Missing username parameter" }), 400
69+ return jsonify ({"msg" : "Missing username parameter" }), Status . HTTP_BAD_REQUEST
6870 if not password :
69- return jsonify ({"msg" : "Missing password parameter" }), 400
71+ return jsonify ({"msg" : "Missing password parameter" }), Status . HTTP_BAD_REQUEST
7072
7173 # TODO Check from DB here
7274 if username != 'admin' or password != 'admin' :
73- return jsonify ({"msg" : "Bad username or password" }), 401
75+ return jsonify ({"msg" : "Bad username or password" }), Status . HTTP_BAD_UNAUTHORIZED
7476
7577 # Identity can be any data that is json serializable
7678 ret = {'jwt' : create_jwt (identity = username ), 'exp' : datetime .utcnow () + current_app .config ['JWT_EXPIRES' ]}
@@ -83,16 +85,16 @@ def get_data():
8385 """Get dummy data returned from the server."""
8486 jwt_data = get_jwt ()
8587 if jwt_data ['roles' ] != 'admin' :
86- return jsonify (msg = "Permission denied" ), 403
88+ return jsonify (msg = "Permission denied" ), Status . HTTP_BAD_FORBIDDEN
8789
8890 identity = get_jwt_identity ()
8991 if not identity :
90- return jsonify ({"msg" : "Token invalid" }), 401
92+ return jsonify ({"msg" : "Token invalid" }), Status . HTTP_BAD_UNAUTHORIZED
9193
9294 data = {'Heroes' : ['Hero1' , 'Hero2' , 'Hero3' ]}
9395 json_response = json .dumps (data )
9496 return Response (json_response ,
95- status = html_codes .HTTP_OK_BASIC ,
97+ status = Status .HTTP_OK_BASIC ,
9698 mimetype = 'application/json' )
9799
98100
0 commit comments