2018-05-08 14:45:09 +00:00
|
|
|
import sys
|
2018-05-07 15:17:19 +00:00
|
|
|
from flask import Blueprint, request, g
|
|
|
|
from flask_restful import Api, Resource, abort
|
|
|
|
from functools import wraps
|
2018-05-07 11:57:53 +00:00
|
|
|
from marshmallow import ValidationError
|
2018-05-07 15:17:19 +00:00
|
|
|
from app.accounts import validate_token
|
2018-05-06 19:42:21 +00:00
|
|
|
|
2018-05-07 14:13:07 +00:00
|
|
|
|
2018-05-06 19:42:21 +00:00
|
|
|
api_bp = Blueprint('api', __name__)
|
|
|
|
api = Api(api_bp)
|
|
|
|
|
2018-05-07 15:17:19 +00:00
|
|
|
|
|
|
|
def protected(func):
|
|
|
|
@wraps(func)
|
|
|
|
def protected_function(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
token = request.headers['Authorization'] or None
|
|
|
|
|
|
|
|
if not token:
|
|
|
|
abort(401, message='Unauthorized', status='error')
|
|
|
|
|
|
|
|
g.current_account = validate_token(token.replace("Bearer ", ""))
|
|
|
|
if not g.current_account:
|
|
|
|
abort(401, message='Unauthorized', status='error')
|
|
|
|
except Exception:
|
2018-05-08 14:45:09 +00:00
|
|
|
error_type, error_instance, traceback = sys.exc_info()
|
|
|
|
print(str(error_type))
|
|
|
|
print(str(error_instance))
|
2018-05-07 15:17:19 +00:00
|
|
|
abort(401, message='Unauthorized', status='error')
|
|
|
|
|
2018-05-08 14:45:09 +00:00
|
|
|
return func(*args, **kwargs)
|
|
|
|
|
2018-05-07 15:17:19 +00:00
|
|
|
return protected_function
|
|
|
|
|
|
|
|
|
|
|
|
class ProtectedResource(Resource):
|
|
|
|
method_decorators = [protected]
|
|
|
|
|
|
|
|
|
|
|
|
def add_resources():
|
2018-05-08 08:52:36 +00:00
|
|
|
from .resources.account import AccountResource, AccountListResource
|
2018-05-22 14:20:14 +00:00
|
|
|
from .resources.token import TokenResource, ValidateTokenResource
|
2018-05-08 14:45:09 +00:00
|
|
|
from .resources.device import (DeviceResource,
|
|
|
|
DeviceRecordingResource,
|
2018-09-17 22:51:26 +00:00
|
|
|
DeviceListResource,
|
|
|
|
DeviceTypeResource,
|
|
|
|
DeviceTypeListResource)
|
2018-05-07 15:17:19 +00:00
|
|
|
|
2018-05-08 08:52:36 +00:00
|
|
|
api.add_resource(AccountResource, '/v1/accounts/<int:account_id>')
|
|
|
|
api.add_resource(AccountListResource, '/v1/accounts')
|
2018-05-07 15:17:19 +00:00
|
|
|
api.add_resource(TokenResource, '/v1/token')
|
2018-05-22 14:20:14 +00:00
|
|
|
api.add_resource(ValidateTokenResource, '/v1/token/validate')
|
2018-05-08 14:45:09 +00:00
|
|
|
api.add_resource(DeviceResource, '/v1/devices/<int:device_id>')
|
|
|
|
api.add_resource(DeviceRecordingResource,
|
|
|
|
'/v1/devices/<int:device_id>/recordings')
|
|
|
|
api.add_resource(DeviceListResource, '/v1/devices')
|
2018-09-17 22:51:26 +00:00
|
|
|
api.add_resource(DeviceTypeResource,
|
|
|
|
'/v1/devices/types/<int:device_type_id>')
|
|
|
|
api.add_resource(DeviceTypeListResource, '/v1/devices/types')
|
2018-05-07 15:17:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
add_resources()
|
2018-05-07 11:57:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
@api_bp.errorhandler(ValidationError)
|
2018-05-07 12:10:20 +00:00
|
|
|
@api_bp.errorhandler(422)
|
2018-05-07 11:57:53 +00:00
|
|
|
def handle_validation_error(e):
|
|
|
|
return {'status': 'error', 'message': str(e)}, 422
|
|
|
|
|
|
|
|
|
|
|
|
@api_bp.errorhandler(Exception)
|
|
|
|
def handle_unknown_errors(e):
|
|
|
|
return ({
|
|
|
|
'status': 'failed',
|
|
|
|
'message': 'Unknown error has occurred! ({0})'.format(str(e))
|
|
|
|
}, 500)
|