refactor api endpoints

This commit is contained in:
Sorrel Bri 2019-10-08 17:18:40 -07:00
parent 2bafe9102c
commit 33584d6147
6 changed files with 54 additions and 48 deletions

View file

@ -1,31 +1,18 @@
from flask import Blueprint, request, jsonify, session from flask import Blueprint, request, jsonify, session
from .users.user_endpoint import UserEndpoint from .users.api_users import api_users
from .users.room_endpoint import RoomEndpoint from .rooms.api_rooms import api_rooms
api = Blueprint('api', __name__, url_prefix='/api') api = Blueprint('api', __name__, url_prefix='/api')
def register_api_endpoints(app):
app.register_blueprint(api_users)
app.register_blueprint(api_rooms)
app.register_blueprint(api)
return app
@api.route('/home', methods=['GET']) @api.route('/home', methods=['GET'])
def api_home(): def api_home():
response = {"message": "home page"} response = {"message": "home page"}
return jsonify(response) return jsonify(response)
@api.route('/users', methods=['GET'])
def api_get_users():
return jsonify(UserEndpoint.users())
@api.route('/user', methods=['GET'])
def api_get_user():
return jsonify(UserEndpoint.user())
@api.route('/rooms', methods=['GET'])
def api_get_rooms():
return RoomEndpoint.get_rooms()
@api.route('/room', methods=['GET'])
def api_get_room():
return RoomEndpoint.get_room()
# protected route
@api.route('/room', methods=['POST'])
def api_post_room():
return pass

19
api/rooms/api_rooms.py Normal file
View file

@ -0,0 +1,19 @@
from models.User import User, user_schema, users_schema
from models.GameRoom import GameRoom
from flask import Blueprint, request, jsonify, session
api_rooms = Blueprint('api_rooms', __name__, url_prefix='/api/rooms')
@api_rooms.route('/<id>', methods=['GET'])
def get_room():
pass
@api_rooms.route('/', methods=['GET'])
def get_rooms():
response = {"status" : "success"}
return jsonify(response)
# protected route
@api_rooms.route('/', methods=['POST'])
def post_room():
pass

View file

@ -1,9 +0,0 @@
from models.User import User, user_schema, users_schema
from models.Room import Room
from flask import request, jsonify, Response, json
class RoomEndpoint(object):
def get_rooms():
pass
def get_room():
pass

25
api/users/api_users.py Normal file
View file

@ -0,0 +1,25 @@
from models.User import User, user_schema, users_schema
from flask import Blueprint, request, json, session, jsonify
api_users = Blueprint('api_users', __name__, url_prefix='/api')
@api_users.route('/users/', methods=['GET'])
def api_get_users():
print('called one')
users = User.query.all()
response = users_schema.dumps(users)
return jsonify(response)
@api_users.route('/users/account', methods=['GET'])
def api_get_user():
print('called')
auth_header = request.headers.get('Authorization') or None
if auth_header:
auth_token = auth_header.split(" ")[1]
user = User.decode_auth_token(auth_token) or None
response = json.dumps(user)
else:
response = {
'status': 'failed',
'message': 'Please Log In'}
return jsonify(response)

View file

@ -1,15 +0,0 @@
from models.User import User, user_schema, users_schema
from flask import request, jsonify, Response, json
class UserEndpoint(object):
def users():
users = User.query.all()
response = users_schema.dumps(users)
return response
def user():
auth_header = request.headers.get('Authorization') or None
auth_token = auth_header.split(" ")[1]
user = User.decode_auth_token(auth_token) or None
response = json.dumps(user)
return response

View file

@ -1,7 +1,7 @@
from app import create_app, db from app import create_app, db
# Blueprints # Blueprints
from api.api import api from api.api import register_api_endpoints
from auth.auth import auth from auth.auth import auth
# Web sockets # Web sockets
@ -14,7 +14,6 @@ from flask_migrate import Migrate
if __name__ == '__main__': if __name__ == '__main__':
app = create_app() app = create_app()
app.register_blueprint(api) register_api_endpoints(app)
app.register_blueprint(auth)
migrate = Migrate(app, db) migrate = Migrate(app, db)
socketio.run(app, debug=True) socketio.run(app, debug=True)