2020-07-12 17:34:25 +02:00
|
|
|
# ContentDB
|
2021-01-30 17:59:42 +01:00
|
|
|
# Copyright (C) 2018-21 rubenwardy
|
2018-06-15 23:57:43 +02:00
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
2021-01-30 17:59:42 +01:00
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
2018-06-15 23:57:43 +02:00
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2021-01-30 17:59:42 +01:00
|
|
|
# GNU Affero General Public License for more details.
|
2018-06-15 23:57:43 +02:00
|
|
|
#
|
2021-01-30 17:59:42 +01:00
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
2018-06-15 23:57:43 +02:00
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2024-06-22 14:48:10 +02:00
|
|
|
import json
|
2019-11-22 15:33:22 +01:00
|
|
|
from flask import Blueprint
|
2018-06-15 23:57:43 +02:00
|
|
|
|
2024-06-22 14:48:10 +02:00
|
|
|
from .support import error
|
|
|
|
|
2019-11-16 00:51:42 +01:00
|
|
|
bp = Blueprint("api", __name__)
|
|
|
|
|
2024-06-22 14:48:10 +02:00
|
|
|
|
2019-11-22 15:33:22 +01:00
|
|
|
from . import tokens, endpoints
|
2024-06-22 14:48:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
@bp.errorhandler(400)
|
|
|
|
@bp.errorhandler(401)
|
|
|
|
@bp.errorhandler(403)
|
|
|
|
@bp.errorhandler(404)
|
|
|
|
def handle_exception(e):
|
|
|
|
"""Return JSON instead of HTML for HTTP errors."""
|
|
|
|
# start with the correct headers and status code from the error
|
|
|
|
response = e.get_response()
|
|
|
|
# replace the body with JSON
|
|
|
|
response.data = json.dumps({
|
|
|
|
"success": False,
|
|
|
|
"code": e.code,
|
|
|
|
"name": e.name,
|
|
|
|
"description": e.description,
|
|
|
|
})
|
|
|
|
response.content_type = "application/json"
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@bp.route("/api/<path:path>")
|
|
|
|
def page_not_found(path):
|
|
|
|
error(404, "Endpoint or method not found")
|