2024-12-02 00:00:05 +00:00
|
|
|
from flask import Blueprint, jsonify
|
2024-11-10 15:13:29 +00:00
|
|
|
from flask.typing import ResponseReturnValue
|
|
|
|
from werkzeug.exceptions import HTTPException
|
2024-11-10 13:38:51 +00:00
|
|
|
|
2024-12-06 13:34:44 +00:00
|
|
|
from app.api.onion import api_onion
|
2024-12-02 00:00:05 +00:00
|
|
|
from app.api.web import api_web
|
2024-11-10 13:38:51 +00:00
|
|
|
|
2024-12-06 18:15:47 +00:00
|
|
|
api = Blueprint("api", __name__)
|
|
|
|
api.register_blueprint(api_onion, url_prefix="/onion")
|
|
|
|
api.register_blueprint(api_web, url_prefix="/web")
|
2024-11-10 15:13:29 +00:00
|
|
|
|
2024-11-10 13:38:51 +00:00
|
|
|
|
|
|
|
@api.errorhandler(400)
|
2024-11-10 15:13:29 +00:00
|
|
|
def bad_request(error: HTTPException) -> ResponseReturnValue:
|
2024-12-06 18:15:47 +00:00
|
|
|
response = jsonify({"error": "Bad Request", "message": error.description})
|
2024-11-10 13:38:51 +00:00
|
|
|
response.status_code = 400
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@api.errorhandler(401)
|
2024-11-10 15:13:29 +00:00
|
|
|
def unauthorized(error: HTTPException) -> ResponseReturnValue:
|
2024-12-06 18:15:47 +00:00
|
|
|
response = jsonify({"error": "Unauthorized", "message": error.description})
|
2024-11-10 13:38:51 +00:00
|
|
|
response.status_code = 401
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@api.errorhandler(404)
|
2024-11-10 15:13:29 +00:00
|
|
|
def not_found(_: HTTPException) -> ResponseReturnValue:
|
2024-12-06 18:15:47 +00:00
|
|
|
response = jsonify(
|
|
|
|
{"error": "Not found", "message": "Resource could not be found."}
|
|
|
|
)
|
2024-11-10 13:38:51 +00:00
|
|
|
response.status_code = 404
|
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
@api.errorhandler(500)
|
2024-11-10 15:13:29 +00:00
|
|
|
def internal_server_error(_: HTTPException) -> ResponseReturnValue:
|
2024-12-06 18:15:47 +00:00
|
|
|
response = jsonify(
|
|
|
|
{"error": "Internal Server Error", "message": "An unexpected error occurred."}
|
|
|
|
)
|
2024-11-10 13:38:51 +00:00
|
|
|
response.status_code = 500
|
|
|
|
return response
|