<commit_before>from .exceptions import ServerResponseError, InternalServerError, NonXMLResponseError
from functools import wraps
from xml.etree.ElementTree import ParseError
from ..query import QuerySet
import logging

try:
    from distutils2.version import NormalizedVersion as Version
except ImportError:
    from distutils.version import LooseVersion as Version

logger = logging.getLogger('tableau.endpoint')

Success_codes = [200, 201, 202, 204]


class Endpoint(object):
    def __init__(self, parent_srv):
        self.parent_srv = parent_srv

    @staticmethod
    def _make_common_headers(auth_token, content_type):
        headers = {}
        if auth_token is not None:
            headers['x-tableau-auth'] = auth_token
        if content_type is not None:
            headers['content-type'] = content_type

        return headers

    @staticmethod
    def _safe_to_log(server_response):
        """Checks if the server_response content is not xml (eg binary image or zip)
        and replaces it with a constant
        """
        ALLOWED_CONTENT_TYPES = ('application/xml', 'application/xml;charset=utf-8')
        if server_response.headers.get('Content-Type', None) not in ALLOWED_CONTENT_TYPES:
            return '[Truncated File Contents]'
        else:
            return server_response.content

    def _make_request(self, method, url, content=None, request_object=None,
                      auth_token=None, content_type=None, parameters=None):
        parameters = parameters or {}
        if request_object is not None:
            parameters["params"] = request_object.get_query_params()
        parameters.update(self.parent_srv.http_options)
        parameters['headers'] = Endpoint._make_common_headers(auth_token, content_type)

        if content is not None:
            parameters['data'] = content

        logger.debug(u'request {}, url: {}'.format(method.__name__, url))
        if content:
            logger.debug(u'request content: {}'.format(content))

        server_response = method(url, **parameters)
        self.parent_srv._namespace.detect(server_response.content)
        self._check_status(server_response)

        # This check is to determine if the response is a text response (xml or otherwise)
        # so that we do not attempt to log bytes and other binary data.
        if server_response.encoding:
            logger.debug(u'Server response from {0}:\n\t{1}'.format(
                url, server_response.content.decode(server_response.encoding)))
        return server_response

    def _check_status(self, server_response):
        if len(server_response.content) > 0:
            logger.debug(self._safe_to_log(server_response))
        if server_response.status_code >= 500:
            raise InternalServerError(server_response)
        elif server_response.status_code not in Success_codes:
            try:<commit_msg>Limit request content to 1000 bytes, remove redundant response logging<commit_after>from .exceptions import ServerResponseError, InternalServerError, NonXMLResponseError
from functools import wraps
from xml.etree.ElementTree import ParseError
from ..query import QuerySet
import logging

try:
    from distutils2.version import NormalizedVersion as Version
except ImportError:
    from distutils.version import LooseVersion as Version

logger = logging.getLogger('tableau.endpoint')

Success_codes = [200, 201, 202, 204]


class Endpoint(object):
    def __init__(self, parent_srv):
        self.parent_srv = parent_srv

    @staticmethod
    def _make_common_headers(auth_token, content_type):
        headers = {}
        if auth_token is not None:
            headers['x-tableau-auth'] = auth_token
        if content_type is not None:
            headers['content-type'] = content_type

        return headers

    @staticmethod
    def _safe_to_log(server_response):
        """Checks if the server_response content is not xml (eg binary image or zip)
        and replaces it with a constant
        """
        ALLOWED_CONTENT_TYPES = ('application/xml', 'application/xml;charset=utf-8')
        if server_response.headers.get('Content-Type', None) not in ALLOWED_CONTENT_TYPES:
            return '[Truncated File Contents]'
        else:
            return server_response.content

    def _make_request(self, method, url, content=None, request_object=None,
                      auth_token=None, content_type=None, parameters=None):
        parameters = parameters or {}
        if request_object is not None:
            parameters["params"] = request_object.get_query_params()
        parameters.update(self.parent_srv.http_options)
        parameters['headers'] = Endpoint._make_common_headers(auth_token, content_type)

        if content is not None:
            parameters['data'] = content

        logger.debug(u'request {}, url: {}'.format(method.__name__, url))
        if content:
            logger.debug(u'request content: {}'.format(content[:1000]))

        server_response = method(url, **parameters)
        self.parent_srv._namespace.detect(server_response.content)
        self._check_status(server_response)

        # This check is to determine if the response is a text response (xml or otherwise)
        # so that we do not attempt to log bytes and other binary data.
        if len(server_response.content) > 0 and server_response.encoding:
            logger.debug(u'Server response from {0}:\n\t{1}'.format(
                url, server_response.content.decode(server_response.encoding)))
        return server_response

    def _check_status(self, server_response):
        if server_response.status_code >= 500:
            raise InternalServerError(server_response)
        elif server_response.status_code not in Success_codes:
            try:
{'fraction_non_alphanumeric': 0.08300121296135851, 'fraction_numerical': 0.008317449315543233, 'mean_word_length': 3.0448493342676946, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '3863235', 'n_tokens_mistral': 1599, 'n_tokens_neox': 1529, 'n_words': 423}