<commit_before>from django.db.models.query import QuerySet
from django.http import HttpResponse
from django.core import serializers
from django.utils import simplejson as json


class HttpResponseNotImplemented(HttpResponse):
    status_code = 501


class JSONResponse(HttpResponse):
    """
    A subclass of django.http.HttpResponse which serializes its content
    and returns a response with an application/json mimetype.
    """
    def __init__(self, content=None, content_type=None, status=None, mimetype='application/json'):
        content = self.serialize(content) if content is not None else ''
        super(JSONResponse, self).__init__(content, content_type, status, mimetype)

    def serialize(self, obj):
        json_s = serializers.get_serializer('json')()
        if isinstance(obj, QuerySet):
            return json_s.serialize(obj)
        elif isinstance(obj, dict):
            return json.dumps(obj)

        serialized_list = json_s.serialize([obj])
        m = json.loads(serialized_list)[0]
        return json.dumps(m)<commit_msg>added custom HTTP header for server identification<commit_after>from django.db.models.query import QuerySet
from django.http import HttpResponse
from django.core import serializers
from django.utils import simplejson as json
from netrobe.settings import API_HTTP_HEADER_KEY


class HttpResponseNotImplemented(HttpResponse):
    status_code = 501


class JSONResponse(HttpResponse):
    """
    A subclass of django.http.HttpResponse which serializes its content
    and returns a response with an application/json mimetype.
    """
    def __init__(self, content=None, content_type=None, status=None, mimetype='application/json'):
        content = self.serialize(content) if content is not None else ''
        super(JSONResponse, self).__init__(content, content_type, status, mimetype)
        self['Netrobe-Server-Identifier'] = API_HTTP_HEADER_KEY

    def serialize(self, obj):
        json_s = serializers.get_serializer('json')()
        if isinstance(obj, QuerySet):
            return json_s.serialize(obj)
        elif isinstance(obj, dict):
            return json.dumps(obj)

        serialized_list = json_s.serialize([obj])
        m = json.loads(serialized_list)[0]
        return json.dumps(m)
{'fraction_non_alphanumeric': 0.08818342151675485, 'fraction_numerical': 0.003527336860670194, 'mean_word_length': 3.7172557172557172, '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': '484043', 'n_tokens_mistral': 630, 'n_tokens_neox': 610, 'n_words': 171}