<commit_before>import json
from functools import partial

import requests

from plaid.errors import PlaidError
from plaid.version import __version__


ALLOWED_METHODS = {'post'}
DEFAULT_TIMEOUT = 600  # 10 minutes


def _requests_http_request(url, method, data, timeout=DEFAULT_TIMEOUT):
    normalized_method = method.lower()
    if normalized_method in ALLOWED_METHODS:
        return getattr(requests, normalized_method)(
            url,
            json=data,
            headers={
                'User-Agent': 'Plaid Python v{}'.format(__version__),
            },
            timeout=timeout,
        )
    else:
        raise Exception(
            'Invalid request method {}'.format(method)
        )


def http_request(url, method=None, data=None, timeout=DEFAULT_TIMEOUT):
    response = _requests_http_request(url, method, data or {}, timeout)
    response_body = json.loads(response.text)
    if response_body.get('error_type'):
        raise PlaidError.from_response(response_body)
    else:
        return response_body


# helpers to simplify partial function application
post_request = partial(http_request, method='POST')<commit_msg>add safe handling of invalid json responses (#108)<commit_after>import json
from functools import partial

import requests

from plaid.errors import PlaidError
from plaid.version import __version__


ALLOWED_METHODS = {'post'}
DEFAULT_TIMEOUT = 600  # 10 minutes


def _requests_http_request(url, method, data, timeout=DEFAULT_TIMEOUT):
    normalized_method = method.lower()
    if normalized_method in ALLOWED_METHODS:
        return getattr(requests, normalized_method)(
            url,
            json=data,
            headers={
                'User-Agent': 'Plaid Python v{}'.format(__version__),
            },
            timeout=timeout,
        )
    else:
        raise Exception(
            'Invalid request method {}'.format(method)
        )


def http_request(url, method=None, data=None, timeout=DEFAULT_TIMEOUT):
    response = _requests_http_request(url, method, data or {}, timeout)
    try:
        response_body = json.loads(response.text)
    except json.JSONDecodeError:
        raise PlaidError.from_response({
            'error_message': response.text,
            'error_type': 'API_ERROR',
            'error_code': 'INTERNAL_SERVER_ERROR',
            'display_message': None,
        })
    if response_body.get('error_type'):
        raise PlaidError.from_response(response_body)
    else:
        return response_body


# helpers to simplify partial function application
post_request = partial(http_request, method='POST')
{'fraction_non_alphanumeric': 0.08888888888888889, 'fraction_numerical': 0.004980842911877395, 'mean_word_length': 2.998468606431853, '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': '3903818', 'n_tokens_mistral': 733, 'n_tokens_neox': 715, 'n_words': 179}