"""Blueprint for OpenAPI spec API.""" import os from flask import Blueprint, request, send_from_directory from owsrequest import flask_request from owsresponse import response from owsresponse.adaptors.flask import flaskify from royalties.constants import error spec_api = Blueprint('spec_api', __name__, url_prefix='/spec') SPEC_DIR = os.path.join( os.path.dirname(os.path.dirname(os.path.dirname(__file__))), 'spec' ) SPEC_FILENAME = 'ows_royalties-1.0.0.yaml' @spec_api.route('') def serve_openapi_spec(): """Serve the OpenAPI specification.""" access_rule_decision = flask_request.verify_rules_access_standalone(request) if not access_rule_decision: return flaskify( response.create_error_response( code=error.ERROR_CODE_AUTHORIZATION, message='Unauthorized', status=403, ) ) try: return send_from_directory(SPEC_DIR, SPEC_FILENAME) except FileNotFoundError: return flaskify( response.create_error_response( code=error.ERROR_CODE_GENERAL, message='Specification file not found', status=404, ) )