<commit_before>
from flask.testing import FlaskClient
from flask import json
from werkzeug import urls
from werkzeug.utils import cached_property


class JsonResponseMixin(object):
    '''Mixin which adds method to jsonify response data'''
    @cached_property
    def json(self):
        return json.loads(self.data)


def make_client_response(response_class):
    class ClientResponse(response_class, JsonResponseMixin):
        pass

    return ClientResponse


class Client(FlaskClient):
    def get(self, url, params=None, **kargs):
        if params:
            url = urls.Href(url)(params)

        return super(Client, self).get(url, **kargs)

    def post(self, url, data, **kargs):
        kargs['data'] = data
        return super(Client, self).post(url, **kargs)

    def put(self, url, data, **kargs):
        kargs['data'] = data
        return super(Client, self).put(url, **kargs)

    def patch(self, url, data, **kargs):
        kargs['data'] = data
        return super(Client, self).patch(url, **kargs)


class JSONClient(Client):
    def open(self, *args, **kargs):
        # all requests will be treated like JSON unless otherwise specified
        kargs.setdefault('content_type', 'application/json')

        # convert kargs['data'] dict to json, otherwise, werkzeug will cast to ImmutableDict
        # ImmutableDicts are a problem since our API performs updates to incoming data
        # in normal operation, all dict data is sent as JSON strings which our API then converts to a dict which can be updated
        if isinstance(kargs.get('data'), dict):
            try:
                kargs['data'] = json.dumps(kargs['data'])
            except Exception:
                # some data may not be serializable (e.g. StringIO for file uploads)
                # in that case, it's probably ok to not serialize since that data dict won't be updated in the API
                pass

        return super(JSONClient, self).open(*args, **kargs)<commit_msg>Only JSON serialize if content_type is json.<commit_after>
from flask.testing import FlaskClient
from flask import json
from werkzeug import urls
from werkzeug.utils import cached_property


class JsonResponseMixin(object):
    '''Mixin which adds method to jsonify response data'''
    @cached_property
    def json(self):
        return json.loads(self.data)


def make_client_response(response_class):
    class ClientResponse(response_class, JsonResponseMixin):
        pass

    return ClientResponse


class Client(FlaskClient):
    def get(self, url, params=None, **kargs):
        if params:
            url = urls.Href(url)(params)

        return super(Client, self).get(url, **kargs)

    def post(self, url, data, **kargs):
        kargs['data'] = data
        return super(Client, self).post(url, **kargs)

    def put(self, url, data, **kargs):
        kargs['data'] = data
        return super(Client, self).put(url, **kargs)

    def patch(self, url, data, **kargs):
        kargs['data'] = data
        return super(Client, self).patch(url, **kargs)


class JSONClient(Client):
    def open(self, *args, **kargs):
        # all requests will be treated like JSON unless otherwise specified
        kargs.setdefault('content_type', 'application/json')

        if (kargs['content_type'] == 'application/json'
                and isinstance(kargs.get('data'), dict):
            # If data is a dict, then assume we want to send a JSON serialized
            # string in the request.
            try:
                kargs['data'] = json.dumps(kargs['data'])
            except Exception:
                # Ignore error if data isn't serializable and just send it.
                pass

        return super(JSONClient, self).open(*args, **kargs)
{'fraction_non_alphanumeric': 0.10454545454545454, 'fraction_numerical': 0.0, 'mean_word_length': 2.790273556231003, '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': '4463894', 'n_tokens_mistral': 1073, 'n_tokens_neox': 1040, 'n_words': 350}