<commit_before>from functools import wraps
from json import loads as load_json_string

from django.db.models import Model
from django.http import JsonResponse
from yunity.api.ids import ids_uri_pattern_delim

from yunity.utils.status import HTTP_400_BAD_REQUEST, HTTP_200_OK, HTTP_403_FORBIDDEN, HTTP_201_CREATED


class ApiBase(object):
    @classmethod
    def validation_failure(cls, message, status=HTTP_400_BAD_REQUEST):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'message': message}, status=status)

    @classmethod
    def success(cls, data=None, status=HTTP_200_OK):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data or {}, status=status)

    @classmethod
    def created(cls, data=None, status=HTTP_201_CREATED):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data or {}, status=status)

    @classmethod
    def forbidden(cls, message, status=HTTP_403_FORBIDDEN):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'message': message}, status=status)

    @classmethod
    def error(cls, message, status=HTTP_400_BAD_REQUEST):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse({'message': message}, status=status)


class JsonRequest(object):
    def __init__(self, http_request, json_body):
        self._http_request = http_request
        self._json_body = json_body

    @classmethod
    def from_http_request(cls, http_request, expected_keys):
        """
        :type http_request: HttpRequest
        :type expected_keys: list
        :rtype: JsonRequest
        :raises ValueError: if the request body is not valid JSON or one of the expected keys is missing

        """
        try:
            json_data = load_json_string(http_request.body.decode("utf-8"))
        except ValueError:
            raise ValueError('incorrect json request')

        for expected_key in expected_keys:
            value = json_data.get(expected_key)
            if not value:
                raise ValueError('missing key: {}'.format(expected_key))

        return cls(http_request, json_data)
<commit_msg>Removed implicit 'message' key of error messages. Should be explicit and same as success messages<commit_after>from functools import wraps
from json import loads as load_json_string

from django.db.models import Model
from django.http import JsonResponse
from yunity.api.ids import ids_uri_pattern_delim

from yunity.utils.status import HTTP_400_BAD_REQUEST, HTTP_200_OK, HTTP_403_FORBIDDEN, HTTP_201_CREATED


class ApiBase(object):
    @classmethod
    def validation_failure(cls, message, status=HTTP_400_BAD_REQUEST):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data or {}, status=status)

    @classmethod
    def success(cls, data=None, status=HTTP_200_OK):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data or {}, status=status)

    @classmethod
    def created(cls, data=None, status=HTTP_201_CREATED):
        """
        :type data: dict
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data or {}, status=status)

    @classmethod
    def forbidden(cls, message, status=HTTP_403_FORBIDDEN):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data or {}, status=status)

    @classmethod
    def error(cls, message, status=HTTP_400_BAD_REQUEST):
        """
        :type message: str
        :type status: int
        :rtype JsonResponse

        """
        return JsonResponse(data or {}, status=status)


class JsonRequest(object):
    def __init__(self, http_request, json_body):
        self._http_request = http_request
        self._json_body = json_body

    @classmethod
    def from_http_request(cls, http_request, expected_keys):
        """
        :type http_request: HttpRequest
        :type expected_keys: list
        :rtype: JsonRequest
        :raises ValueError: if the request body is not valid JSON or one of the expected keys is missing

        """
        try:
            json_data = load_json_string(http_request.body.decode("utf-8"))
        except ValueError:
            raise ValueError('incorrect json request')

        for expected_key in expected_keys:
            value = json_data.get(expected_key)
            if not value:
                raise ValueError('missing key: {}'.format(expected_key))

        return cls(http_request, json_data)

{'fraction_non_alphanumeric': 0.08539053014973695, 'fraction_numerical': 0.0113314447592068, 'mean_word_length': 2.558675305975522, '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': '3667858', 'n_tokens_mistral': 1425, 'n_tokens_neox': 1358, 'n_words': 378}