<commit_before>from ckan.plugins import toolkit

import urlparse

from pylons import config
import requests


def call_api(endpoint, function, **kwargs):
    try:
        api_url = config['ckanext.cadasta.api_url']
    except KeyError:
        raise toolkit.ValidationError(
            toolkit._('ckanext.cadasta.api_url has not been set')
        )
    try:
        r = function(urlparse.urljoin(api_url, endpoint), **kwargs)
        # r = function(urlparse.urljoin(api_url, '/post'), **kwargs)
        result = r.json()
        error_dict = result.get('error')
        if error_dict:
            message = result.get('message', '')
            error_dict['message'] = message
            raise toolkit.ValidationError(
                error_dict,
                'Error returned from Cadasta API: {0}'.format(message)
            )
        return result
    except requests.exceptions.RequestException, e:<commit_msg>[#33] add request and response debuggin<commit_after>from ckan.plugins import toolkit

import urlparse

from pylons import config
import requests
import logging
log = logging.getLogger(__name__)
import json

def call_api(endpoint, function, **kwargs):
    try:
        api_url = config['ckanext.cadasta.api_url']
    except KeyError:
        raise toolkit.ValidationError(
            toolkit._('ckanext.cadasta.api_url has not been set')
        )
    try:
        log.debug("[ REQUEST ]:\n\tfunction={0}\n\turl={1}\n\tparams={2}".format(
            function.__name__,
            urlparse.urljoin(api_url, endpoint),
            json.dumps(kwargs,indent=4)
        ))
        r = function(urlparse.urljoin(api_url, endpoint), **kwargs)
        # r = function(urlparse.urljoin(api_url, '/post'), **kwargs)
        result = r.json()
        log.debug("[ RESPONSE ]:\n\tstatus={0}\n\terror={1}\n\tfull_result={2}".format(
            r.status_code,
            result.get('error',{}),
            json.dumps(result,indent=4)
        ))
        error_dict = result.get('error')
        if error_dict:
            message = result.get('message', '')
            error_dict['message'] = message
            raise toolkit.ValidationError(
                error_dict,
                'Error returned from Cadasta API: {0}'.format(message)
            )
        return result
    except requests.exceptions.RequestException, e:
{'fraction_non_alphanumeric': 0.11917983767620675, 'fraction_numerical': 0.0051260145237078175, 'mean_word_length': 2.355300859598854, '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': '5887984', 'n_tokens_mistral': 693, 'n_tokens_neox': 679, 'n_words': 145}