Exemple #1
0
            def error_handler(error):
                # pylint: disable=unused-variable
                """Error handler to return customized error messages from rest api"""

                if isinstance(error, RestValidationError):
                    response = jsonify({'message': str(error)})
                    response.status_code = 400

                elif isinstance(error, RestInputValidationError):
                    response = jsonify({'message': str(error)})
                    response.status_code = 404

                elif isinstance(error, RestFeatureNotAvailable):
                    response = jsonify({'message': str(error)})
                    response.status_code = 501

                elif isinstance(error, HTTPException) and error.code == 404:
                    from aiida.restapi.common.utils import list_routes

                    response = jsonify({
                        'message': 'The requested URL is not found on the server.',
                        'available_endpoints': list_routes()
                    })
                    response.status_code = 404

                # Generic server-side error (not to make the api crash if an
                # unhandled exception is raised. Caution is never enough!!)
                else:
                    response = jsonify({'message': str(error)})
                    response.status_code = 500

                return response
Exemple #2
0
    def get(self):
        """
        It returns the general info about the REST API
        :return: returns current AiiDA version defined in aiida/__init__.py
        """
        # Decode url parts
        path = unquote(request.path)
        url = unquote(request.url)
        url_root = unquote(request.url_root)

        subpath = self.utils.strip_api_prefix(path).strip('/')
        pathlist = self.utils.split_path(subpath)

        if subpath == '':
            resource_type = 'endpoints'
        elif len(pathlist) > 1:
            resource_type = pathlist.pop(1)
        else:
            resource_type = 'info'

        response = {}

        from aiida.restapi.common.config import API_CONFIG
        from aiida import __version__

        if resource_type == 'info':
            response = {}

            # Add Rest API version
            api_version = API_CONFIG['VERSION'].split('.')
            response['API_major_version'] = api_version[0]
            response['API_minor_version'] = api_version[1]
            response['API_revision_version'] = api_version[2]

            # Add Rest API prefix
            response['API_prefix'] = API_CONFIG['PREFIX']

            # Add AiiDA version
            response['AiiDA_version'] = __version__

        elif resource_type == 'endpoints':

            from aiida.restapi.common.utils import list_routes
            response['available_endpoints'] = list_routes()

        headers = self.utils.build_headers(url=request.url, total_count=1)

        ## Build response and return it
        data = dict(method=request.method,
                    url=url,
                    url_root=url_root,
                    path=path,
                    query_string=request.query_string.decode('utf-8'),
                    resource_type='Info',
                    data=response)
        return self.utils.build_response(status=200,
                                         headers=headers,
                                         data=data)
Exemple #3
0
    def get(self):
        """
        It returns the general info about the REST API
        :return: returns current AiiDA version defined in aiida/__init__.py
        """

        ## Decode url parts
        path = unquote(request.path)
        query_string = unquote(request.query_string)
        url = unquote(request.url)
        url_root = unquote(request.url_root)

        pathlist = self.utils.split_path(self.utils.strip_prefix(path))

        if len(pathlist) > 1:
            resource_type = pathlist.pop(1)
        else:
            resource_type = "info"

        response = {}

        import aiida.restapi.common.config as conf
        from aiida import __version__

        if resource_type == "info":
            response = []

            # Add Rest API version
            response.append("REST API version: " + conf.PREFIX.split("/")[-1])

            # Add Rest API prefix
            response.append("REST API Prefix: " + conf.PREFIX)

            # Add AiiDA version
            response.append("AiiDA==" + __version__)

        elif resource_type == "endpoints":

            from aiida.restapi.common.utils import list_routes
            response["available_endpoints"] = list_routes()

        headers = self.utils.build_headers(url=request.url, total_count=1)

        ## Build response and return it
        data = dict(method=request.method,
                    url=url,
                    url_root=url_root,
                    path=path,
                    query_string=query_string,
                    resource_type="Info",
                    data=response)
        return self.utils.build_response(status=200,
                                         headers=headers,
                                         data=data)
Exemple #4
0
    def handle_error(self, e):
        """
        this method handles the 404 "URL not found" exception and return custom message
        :param e: raised exception
        :return: list of available endpoints
        """

        if isinstance(e, HTTPException):
            if e.code == 404:

                from aiida.restapi.common.utils import list_routes

                response = {}

                response["status"] = "404 Not Found"
                response[
                    "message"] = "The requested URL is not found on the server."

                response["available_endpoints"] = list_routes()

                return jsonify(response)

        else:
            raise e