from flask import make_response
from flask_restful import Resource
import importlib.resources

class SwaggerResource(Resource):
    api_package = "xivo_dird.swagger"
    api_filename = "api.json"
    api_path = "/api/api.json"

    @classmethod
    def add_resource(cls, api):
        api.add_resource(cls, cls.api_path)

    def get(self):
        try:
            api_spec = importlib.resources.files(self.api_package).joinpath(self.api_filename).read_text()
        except IOError:
            return {'error': "API spec does not exist"}, 404
        return make_response(api_spec, 200, {'Content-Type': 'application/json'})
