<commit_before>"""
Thin wrapper around the requests library.
"""

import json as jsonlib
import requests
import six

from datasift import USER_AGENT
from datasift.output_mapper import outputmapper
from datasift.exceptions import DataSiftApiException, DataSiftApiFailure, AuthException, RateLimitException
from datasift.requests_ssl import SslHttpAdapter


class PartialRequest(object):
    """ Internal class used to represent a yet-to-be-completed request """

    API_SCHEME = 'https'
    API_HOST = 'api.datasift.com'
    API_VERSION = 'v1.1'
    HEADERS = (
        ('User-Agent', USER_AGENT % API_VERSION),
    )

    def __init__(self, auth, prefix=None, ssl=True, headers=None, timeout=None, proxies=None, verify=True):
        self.auth = auth
        if not ssl:
            self.API_SCHEME = "http"
        self.prefix = prefix
        self.headers = headers
        self.timeout = timeout
        self.proxies = proxies
        self.verify = verify
        self.session = requests.Session()
        self.adapter = SslHttpAdapter()
        self.session.mount("https://", self.adapter)

    def get(self, path, params=None, headers=None):
        return self.build_response(self('get', path, params=params, headers=headers), path=path)

    def post(self, path, params=None, headers=None, data=None):
        return self.build_response(self('post', path, params=params, headers=headers, data=data), path=path)

    def json(self, path, data):
        """Convenience method for posting JSON content."""
        data = data if isinstance(data, six.string_types) else jsonlib.dumps(data)
        return self.post(path, headers={'Content-Type': 'application/json'}, data=data)

    def __call__(self, method, path, params=None, data=None, headers=None):
        url = u'%s://%s' % (self.API_SCHEME, self.path(self.API_HOST, self.API_VERSION, self.prefix, path))
        return self.session.request(method, url,
                                    params=params, data=data, auth=self.auth,
                                    headers=self.dicts(self.headers, headers, dict(self.HEADERS)),
                                    timeout=self.timeout,
                                    proxies=self.proxies,
                                    verify=self.verify)

    ## Builders

    def with_prefix(self, path, *args):
        prefix = '/'.join((path,) + args)
        return PartialRequest(self.auth, prefix, self.headers, self.timeout, self.proxies, self.verify)

    def build_response(self, response, path=None, parser=jsonlib.loads):
        """ Builds a List or Dict response object.

            Wrapper for a response from the DataSift REST API, can be accessed as a list.

            :param response: HTTP response to wrap
            :type response: :class:`~datasift.requests.DictResponse`
            :param parser: optional parser to overload how the data is loaded
            :type parser: func
            :raises: :class:`~datasift.exceptions.DataSiftApiException`, :class:`~datasift.exceptions.DataSiftApiFailure`, :class:`~datasift.exceptions.AuthException`, :class:`requests.exceptions.HTTPError`, :class:`~datasift.exceptions.RateLimitException`
        """
        if response.status_code != 204:
            try:
                data = parser(response.text)
            except ValueError as e:
                raise DataSiftApiFailure("Unable to decode returned data. %s: %s" % (e, response.text))
            if "error" in data:
                if response.status_code == 401:
                    raise AuthException(data)
                if response.status_code == 403:
                    if int(response.headers.get("x-ratelimit-cost")) > int(response.headers.get("x-ratelimit-remaining")):
                        raise RateLimitException(data)
                raise DataSiftApiException(DictResponse(response, data))
            response.raise_for_status()
            if isinstance(data, dict):
                r = DictResponse(response, data)
            elif isinstance(data, (list, map)):
                r = ListResponse(response, data)
            outputmapper(r, self.prefix, path)
            return r

        else:
            # empty dict
            return DictResponse(response, {})

    ## Helpers
<commit_msg>unicoded the error string<commit_after>"""
Thin wrapper around the requests library.
"""

import json as jsonlib
import requests
import six

from datasift import USER_AGENT
from datasift.output_mapper import outputmapper
from datasift.exceptions import DataSiftApiException, DataSiftApiFailure, AuthException, RateLimitException
from datasift.requests_ssl import SslHttpAdapter


class PartialRequest(object):
    """ Internal class used to represent a yet-to-be-completed request """

    API_SCHEME = 'https'
    API_HOST = 'api.datasift.com'
    API_VERSION = 'v1.1'
    HEADERS = (
        ('User-Agent', USER_AGENT % API_VERSION),
    )

    def __init__(self, auth, prefix=None, ssl=True, headers=None, timeout=None, proxies=None, verify=True):
        self.auth = auth
        if not ssl:
            self.API_SCHEME = "http"
        self.prefix = prefix
        self.headers = headers
        self.timeout = timeout
        self.proxies = proxies
        self.verify = verify
        self.session = requests.Session()
        self.adapter = SslHttpAdapter()
        self.session.mount("https://", self.adapter)

    def get(self, path, params=None, headers=None):
        return self.build_response(self('get', path, params=params, headers=headers), path=path)

    def post(self, path, params=None, headers=None, data=None):
        return self.build_response(self('post', path, params=params, headers=headers, data=data), path=path)

    def json(self, path, data):
        """Convenience method for posting JSON content."""
        data = data if isinstance(data, six.string_types) else jsonlib.dumps(data)
        return self.post(path, headers={'Content-Type': 'application/json'}, data=data)

    def __call__(self, method, path, params=None, data=None, headers=None):
        url = u'%s://%s' % (self.API_SCHEME, self.path(self.API_HOST, self.API_VERSION, self.prefix, path))
        return self.session.request(method, url,
                                    params=params, data=data, auth=self.auth,
                                    headers=self.dicts(self.headers, headers, dict(self.HEADERS)),
                                    timeout=self.timeout,
                                    proxies=self.proxies,
                                    verify=self.verify)

    ## Builders

    def with_prefix(self, path, *args):
        prefix = '/'.join((path,) + args)
        return PartialRequest(self.auth, prefix, self.headers, self.timeout, self.proxies, self.verify)

    def build_response(self, response, path=None, parser=jsonlib.loads):
        """ Builds a List or Dict response object.

            Wrapper for a response from the DataSift REST API, can be accessed as a list.

            :param response: HTTP response to wrap
            :type response: :class:`~datasift.requests.DictResponse`
            :param parser: optional parser to overload how the data is loaded
            :type parser: func
            :raises: :class:`~datasift.exceptions.DataSiftApiException`, :class:`~datasift.exceptions.DataSiftApiFailure`, :class:`~datasift.exceptions.AuthException`, :class:`requests.exceptions.HTTPError`, :class:`~datasift.exceptions.RateLimitException`
        """
        if response.status_code != 204:
            try:
                data = parser(response.text)
            except ValueError as e:
                raise DataSiftApiFailure(u"Unable to decode returned data. %s: %s" % (e, response.text))
            if "error" in data:
                if response.status_code == 401:
                    raise AuthException(data)
                if response.status_code == 403:
                    if int(response.headers.get("x-ratelimit-cost")) > int(response.headers.get("x-ratelimit-remaining")):
                        raise RateLimitException(data)
                raise DataSiftApiException(DictResponse(response, data))
            response.raise_for_status()
            if isinstance(data, dict):
                r = DictResponse(response, data)
            elif isinstance(data, (list, map)):
                r = ListResponse(response, data)
            outputmapper(r, self.prefix, path)
            return r

        else:
            # empty dict
            return DictResponse(response, {})

    ## Helpers

{'fraction_non_alphanumeric': 0.10739828819322313, 'fraction_numerical': 0.002579434869269551, 'mean_word_length': 2.4092725819344523, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6231034', 'n_tokens_mistral': 2417, 'n_tokens_neox': 2317, 'n_words': 591}