<commit_before>"""This module provides the HaaS service's public API.

TODO: Spec out and document what sanitization is required.
"""
from flask import Flask, request
from functools import wraps
import inspect
import importlib
import json

from haas import model
from haas.config import cfg


class APIError(Exception):
    """An exception indicating an error that should be reported to the user.

    i.e. If such an error occurs in a rest API call, it should be reported as
    part of the HTTP response.
    """


class NotFoundError(APIError):
    """An exception indicating that a given resource does not exist."""


class DuplicateError(APIError):
    """An exception indicating that a given resource already exists."""


class AllocationError(APIError):
    """An exception indicating resource exhaustion."""


class BadArgumentError(APIError):
    """An exception indicating an invalid request on the part of the user."""

class ProjectMismatchError(APIError):
    """An expection indicating that the resources given don't belong to the
    same project.
    """

class BlockedError(APIError):
    """An exception indicating that the requested action cannot happen until
    some other change.  For example, deletion is blocked until the components
    are deleted, and possibly until the dirty flag is cleared as well.
    """

app = Flask(__name__)


def handle_client_errors(f):
    """A decorator which adds some error handling.

    If the function decorated with `handle_client_errors` raises an exception
    of type `APIError`, the error will be reported to the client, whereas
    other exceptions (being indications of a bug in the HaaS) will not be.
    """
    @wraps(f)
    def wrapped(*args, **kwargs):
        try:
            resp = f(*args, **kwargs)
        except APIError as e:
            # Right now we're always returning 400 (Bad Request). This probably
            # isn't actually the right thing to do.
            #<commit_msg>Fix typo in docstring in haas/api.py<commit_after>"""This module provides the HaaS service's public API.

TODO: Spec out and document what sanitization is required.
"""
from flask import Flask, request
from functools import wraps
import inspect
import importlib
import json

from haas import model
from haas.config import cfg


class APIError(Exception):
    """An exception indicating an error that should be reported to the user.

    i.e. If such an error occurs in a rest API call, it should be reported as
    part of the HTTP response.
    """


class NotFoundError(APIError):
    """An exception indicating that a given resource does not exist."""


class DuplicateError(APIError):
    """An exception indicating that a given resource already exists."""


class AllocationError(APIError):
    """An exception indicating resource exhaustion."""


class BadArgumentError(APIError):
    """An exception indicating an invalid request on the part of the user."""

class ProjectMismatchError(APIError):
    """An exception indicating that the resources given don't belong to the
    same project.
    """

class BlockedError(APIError):
    """An exception indicating that the requested action cannot happen until
    some other change.  For example, deletion is blocked until the components
    are deleted, and possibly until the dirty flag is cleared as well.
    """

app = Flask(__name__)


def handle_client_errors(f):
    """A decorator which adds some error handling.

    If the function decorated with `handle_client_errors` raises an exception
    of type `APIError`, the error will be reported to the client, whereas
    other exceptions (being indications of a bug in the HaaS) will not be.
    """
    @wraps(f)
    def wrapped(*args, **kwargs):
        try:
            resp = f(*args, **kwargs)
        except APIError as e:
            # Right now we're always returning 400 (Bad Request). This probably
            # isn't actually the right thing to do.
            #
{'fraction_non_alphanumeric': 0.07292985565966068, 'fraction_numerical': 0.0015193719929095975, 'mean_word_length': 3.428251121076233, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2697659', 'n_tokens_mistral': 1020, 'n_tokens_neox': 979, 'n_words': 493}