<commit_before>from c2corg_ui.diff.differ import diff_documents
from shapely.geometry import asShape
from shapely.ops import transform
from functools import partial
from urllib.parse import urlencode
import httplib2
import pyproj
import json

from pyramid.httpexceptions import (
    HTTPBadRequest, HTTPNotFound, HTTPInternalServerError)

from c2corg_common.attributes import default_langs


class Document(object):

    # FIXME Is a "documents" route available/relevant in the API?
    _API_ROUTE = 'documents'

    # FIXME sync with API => use a CONSTANT in c2corg_common?
    _DEFAULT_FILTERS = {
        'limit': 30
    }

    def __init__(self, request):
        self.request = request
        self.settings = request.registry.settings
        self.template_input = {
            'debug': 'debug' in self.request.params,
            'api_url': self.settings['api_url']
        }

    def _call_api(self, url, method='GET', body=None, headers=None):
        settings = self.settings
        if 'api_url_internal' in settings and settings['api_url_internal']:
            api_url = settings['api_url_internal']
            if 'api_url_host' in settings and settings['api_url_host']:
                headers = {} if headers is None else headers
                headers['Host'] = settings['api_url_host']
        else:
            api_url = settings['api_url']
        url = '%s/%s' % (api_url, url)
        http = httplib2.Http()
        try:
            resp, content = http.request(
                url, method=method, body=body, headers=headers
            )
            return resp, json.loads(content.decode('utf-8'))
        except Exception:
            # TODO: return error message as the second item
            return {'status': '500'}, {}

    def _validate_id_lang(self):
        if 'id' not in self.request.matchdict:
            # eg. creating a new document
            return None, None

        id = self._validate_int('id')
        lang = self._validate_lang()

        return id, lang

    def _validate_lang(self):
        lang = str(self.request.matchdict['lang'])
        if lang not in default_langs:
            raise HTTPBadRequest("Incorrect lang")
        return lang<commit_msg>Log calls to API<commit_after>from c2corg_ui.diff.differ import diff_documents
from shapely.geometry import asShape
from shapely.ops import transform
from functools import partial
from urllib.parse import urlencode
import httplib2
import pyproj
import json
import logging

from pyramid.httpexceptions import (
    HTTPBadRequest, HTTPNotFound, HTTPInternalServerError)

from c2corg_common.attributes import default_langs

log = logging.getLogger(__name__)


class Document(object):

    # FIXME Is a "documents" route available/relevant in the API?
    _API_ROUTE = 'documents'

    # FIXME sync with API => use a CONSTANT in c2corg_common?
    _DEFAULT_FILTERS = {
        'limit': 30
    }

    def __init__(self, request):
        self.request = request
        self.settings = request.registry.settings
        self.template_input = {
            'debug': 'debug' in self.request.params,
            'api_url': self.settings['api_url']
        }

    def _call_api(self, url, method='GET', body=None, headers=None):
        settings = self.settings
        if 'api_url_internal' in settings and settings['api_url_internal']:
            api_url = settings['api_url_internal']
            if 'api_url_host' in settings and settings['api_url_host']:
                headers = {} if headers is None else headers
                headers['Host'] = settings['api_url_host']
        else:
            api_url = settings['api_url']
        url = '%s/%s' % (api_url, url)
        if log.isEnabledFor(logging.DEBUG):
            log.debug('API: %s %s', method, url)
        http = httplib2.Http()
        try:
            resp, content = http.request(
                url, method=method, body=body, headers=headers
            )
            return resp, json.loads(content.decode('utf-8'))
        except Exception:
            # TODO: return error message as the second item
            return {'status': '500'}, {}

    def _validate_id_lang(self):
        if 'id' not in self.request.matchdict:
            # eg. creating a new document
            return None, None

        id = self._validate_int('id')
        lang = self._validate_lang()

        return id, lang

    def _validate_lang(self):
        lang = str(self.request.matchdict['lang'])
        if lang not in default_langs:
            raise HTTPBadRequest("Incorrect lang")
        return lang
{'fraction_non_alphanumeric': 0.09355828220858896, 'fraction_numerical': 0.004820333041191937, 'mean_word_length': 2.4452830188679244, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '4092039', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1283, 'n_words': 363}