<commit_before>"""
HTTP Client Library Adapters

"""
import base64
import logging
import requests

try:
    import simplejson as json
except ImportError:
    import json

try:
    from tornado import gen
    from tornado import httpclient
except ImportError:
    httpclient = None

    class Gen:
        def coroutine(self, func):
            pass
    gen = Gen()

from consulate import utils

LOGGER = logging.getLogger(__name__)

CONTENT_FORM = 'application/x-www-form-urlencoded; charset=UTF-8'
CONTENT_JSON = 'application/json; charset=UTF-8'


def prepare_data(fun):
    def inner(*args, **kwargs):
        if kwargs.get('data') and not utils.is_string(kwargs.get('data')):
            kwargs['data'] = json.dumps(kwargs['data'])
        elif len(args) == 3 and not utils.is_string(args[2]):
            args = args[0], args[1], json.dumps(args[2])
        return fun(*args, **kwargs)
    return inner


class Request(object):

    def __init__(self):
        self.session = requests.Session()

    def delete(self, uri):
        LOGGER.debug("DELETE %s", uri)
        response = self.session.delete(uri)
        return Response(response.status_code,
                        response.content,
                        response.headers)

    def get(self, uri):
        LOGGER.debug("GET %s", uri)
        response = self.session.get(uri)
        return Response(response.status_code,
                        response.content,
                        response.headers)

    @prepare_data
    def put(self, uri, data=None):
        LOGGER.debug("PUT %s with %r", uri, data)
        if utils.is_string(data):
            headers = {'Content-Type': CONTENT_FORM}
        else:
            headers = {'Content-Type': CONTENT_JSON}
        response = self.session.put(uri, data=data.encode('utf-8'),
                                    headers=headers)
        return Response(response.status_code,
                        response.content,
                        response.headers)


class TornadoRequest(Request):

    def __init__(self):
        if gen is None or httpclient is None:
            raise ImportError('Could not import the required Tornado modules')
        self.session = httpclient.AsyncHTTPClient()

    @gen.coroutine
    def delete(self, uri):
        response = yield self.session.fetch(uri, method="delete")
        raise gen.Return(Response(response.code,<commit_msg>Only encode in Python 2<commit_after>"""
HTTP Client Library Adapters

"""
import base64
import logging
import requests

try:
    import simplejson as json
except ImportError:
    import json

try:
    from tornado import gen
    from tornado import httpclient
except ImportError:
    httpclient = None

    class Gen:
        def coroutine(self, func):
            pass
    gen = Gen()

from consulate import utils

LOGGER = logging.getLogger(__name__)

CONTENT_FORM = 'application/x-www-form-urlencoded; charset=UTF-8'
CONTENT_JSON = 'application/json; charset=UTF-8'


def prepare_data(fun):
    def inner(*args, **kwargs):
        if kwargs.get('data') and not utils.is_string(kwargs.get('data')):
            kwargs['data'] = json.dumps(kwargs['data'])
        elif len(args) == 3 and not utils.is_string(args[2]):
            args = args[0], args[1], json.dumps(args[2])
        return fun(*args, **kwargs)
    return inner


class Request(object):

    def __init__(self):
        self.session = requests.Session()

    def delete(self, uri):
        LOGGER.debug("DELETE %s", uri)
        response = self.session.delete(uri)
        return Response(response.status_code,
                        response.content,
                        response.headers)

    def get(self, uri):
        LOGGER.debug("GET %s", uri)
        response = self.session.get(uri)
        return Response(response.status_code,
                        response.content,
                        response.headers)

    @prepare_data
    def put(self, uri, data=None):
        LOGGER.debug("PUT %s with %r", uri, data)
        if utils.is_string(data):
            headers = {'Content-Type': CONTENT_FORM}
        else:
            headers = {'Content-Type': CONTENT_JSON}
        if not utils.PYTHON3:
            data = data.encode('utf-8')
        response = self.session.put(uri, data=data, headers=headers)
        return Response(response.status_code,
                        response.content,
                        response.headers)


class TornadoRequest(Request):

    def __init__(self):
        if gen is None or httpclient is None:
            raise ImportError('Could not import the required Tornado modules')
        self.session = httpclient.AsyncHTTPClient()

    @gen.coroutine
    def delete(self, uri):
        response = yield self.session.fetch(uri, method="delete")
        raise gen.Return(Response(response.code,
{'fraction_non_alphanumeric': 0.10752911813643927, 'fraction_numerical': 0.004575707154742097, 'mean_word_length': 2.2983539094650207, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1266231', 'n_tokens_mistral': 1377, 'n_tokens_neox': 1345, 'n_words': 348}