university-final-iot-backend/app/api/blueprint.py

112 lines
4.5 KiB
Python
Raw Normal View History

from flask_restful import Api
from marshmallow import ValidationError
2018-11-03 17:32:35 +00:00
from app.errors import NotPresentError, BadRequestError
2018-11-03 17:11:52 +00:00
from flask import Blueprint, jsonify
api_bp = Blueprint('api', __name__)
2018-11-03 17:11:52 +00:00
api = Api(api_bp)
def add_resources():
from .resources.account import (AccountResource,
AccountListResource,
AccountRoleResource,
RoleResource,
RolesResource,
AccountEmailTokenResource,
AccountEmailTokenResendResource)
from .resources.token import TokenResource, ValidateTokenResource
from .resources.device import (DeviceResource,
DeviceRecordingResource,
DeviceRecordingQueryResource,
DeviceListResource,
DeviceTypeResource,
DeviceTypeListResource,
2018-10-29 21:47:03 +00:00
DeviceConfigurationResource,
2018-11-01 00:45:14 +00:00
DeviceSecretResource,
2018-11-03 15:00:46 +00:00
DeviceSecretResetResource,
2018-11-01 00:45:14 +00:00
DeviceShareResource,
DeviceShareActivationResource)
2018-10-15 17:51:51 +00:00
from .resources.dashboard import (DashboardResource,
2018-10-22 21:54:31 +00:00
DashboardListResource,
DashboardWidgetResource,
DashboardWidgetListResource)
api.add_resource(AccountResource, '/v1/accounts/<int:account_id>')
api.add_resource(AccountListResource, '/v1/accounts')
api.add_resource(AccountRoleResource, '/v1/accounts/<int:account_id>/role')
api.add_resource(AccountEmailTokenResource,
'/v1/email/confirm/<string:token>')
api.add_resource(AccountEmailTokenResendResource,
'/v1/email/resend')
api.add_resource(RoleResource, '/v1/roles/<int:role_id>')
api.add_resource(RolesResource, '/v1/roles')
api.add_resource(TokenResource, '/v1/token')
api.add_resource(ValidateTokenResource, '/v1/token/validate')
api.add_resource(DeviceResource, '/v1/devices/<int:device_id>')
api.add_resource(DeviceRecordingResource,
'/v1/devices/<int:device_id>/recordings')
api.add_resource(DeviceRecordingQueryResource,
'/v1/devices/<int:device_id>/recordings/jsonql')
api.add_resource(DeviceListResource, '/v1/devices')
api.add_resource(DeviceTypeResource,
'/v1/devices/types/<int:device_type_id>')
api.add_resource(DeviceTypeListResource, '/v1/devices/types')
api.add_resource(DeviceConfigurationResource,
'/v1/devices/<int:device_id>/configuration')
2018-10-29 21:47:03 +00:00
api.add_resource(DeviceSecretResource,
'/v1/devices/<int:device_id>/secret')
2018-11-03 15:00:46 +00:00
api.add_resource(DeviceSecretResetResource,
'/v1/devices/<int:device_id>/secret/reset')
2018-11-01 00:45:14 +00:00
api.add_resource(DeviceShareResource,
'/v1/devices/<int:device_id>/share')
api.add_resource(
DeviceShareActivationResource,
'/v1/devices/<int:device_id>/share/activate/<string:token>')
api.add_resource(DashboardResource,
'/v1/dashboards/<int:dashboard_id>')
2018-10-15 17:51:51 +00:00
api.add_resource(DashboardListResource, '/v1/dashboards')
2018-10-22 21:54:31 +00:00
api.add_resource(
DashboardWidgetResource,
'/v1/dashboards/<int:dashboard_id>/widgets/<int:widget_id>')
api.add_resource(DashboardWidgetListResource,
'/v1/dashboards/<int:dashboard_id>/widgets')
add_resources()
@api_bp.errorhandler(ValidationError)
@api_bp.errorhandler(422)
def handle_validation_error(e):
2018-11-03 17:11:52 +00:00
return jsonify({'status': 'error', 'message': str(e)}), 422
@api_bp.errorhandler(ValueError)
def handle_value_error(e):
return jsonify({'status': 'error', 'message': str(e)}), 422
2018-11-03 17:22:01 +00:00
@api_bp.errorhandler(NotPresentError)
2018-11-03 17:24:54 +00:00
@api_bp.errorhandler(404)
2018-11-03 17:22:01 +00:00
def handle_not_present_error(e):
return jsonify({'status': 'error', 'message': str(e)}), 404
2018-11-03 17:32:35 +00:00
@api_bp.errorhandler(BadRequestError)
@api_bp.errorhandler(400)
def handle_bad_request_error(e):
return jsonify({'status': 'error', 'message': str(e)}), 400
@api_bp.errorhandler(Exception)
2018-11-03 17:11:52 +00:00
@api_bp.errorhandler(500)
def handle_unknown_errors(e):
2018-11-03 17:11:52 +00:00
return jsonify({
'status': 'failed',
'message': 'Unknown error has occurred! ({0})'.format(str(e))
2018-11-03 17:11:52 +00:00
}), 500