<commit_before>"""
HTTPServer
**********

An HTTP server to communicate with, for example::

    HTTPServer('http://example.com/api').request('go')
"""

from requests import Request, Session

from jsonrpcclient.server import Server


class HTTPServer(Server):
    """
    :param endpoint: The server address.
    :param kwargs: HTTP headers and other options passed on to the requests
                   module.
    """

    # The default HTTP header
    __DEFAULT_HTTP_HEADERS__ = {
        'Content-Type': 'application/json', 'Accept': 'application/json'}

    def __init__(self, endpoint):
        super(HTTPServer, self).__init__(endpoint)
        self.session = Session()
        self.session.headers.update(self.__DEFAULT_HTTP_HEADERS__)
        self.last_request = None
        self.last_response = None

    def _send_message(
            self, request, headers=None, files=None, params=None, auth=None,
            cookies=None, **kwargs):
        """Transport the message to the server and return the response.

        :param request: The JSON-RPC request string.
        :return: The JSON-RPC response.
        :rtype: A string for requests, None for notifications.
        :raise requests.exceptions.RequestException:
            Raised by the requests module in the event of a communications
            error.
        """
        # Prepare the request
        request = Request(
            method='POST', url=self.endpoint, data=request, headers=headers,
            files=files, params=params, auth=auth, cookies=cookies)
        prepped = self.session.prepare_request(request)
        self.last_request = prepped
        # Log the request
        self._log_request(request, {'http_headers': prepped.headers})
        # Send the message
        response = self.session.send(prepped, **kwargs)
        # Log the response
        self._log_response(response.text, {'http_code': response.status_code, \
            'http_reason': response.reason, 'http_headers': response.headers})
        self.last_response = response
        return response.text
<commit_msg>Fix bug when logging requests<commit_after>"""
HTTPServer
**********

An HTTP server to communicate with, for example::

    HTTPServer('http://example.com/api').request('go')
"""

from requests import Request, Session

from jsonrpcclient.server import Server


class HTTPServer(Server):
    """
    :param endpoint: The server address.
    :param kwargs: HTTP headers and other options passed on to the requests
                   module.
    """

    # The default HTTP header
    __DEFAULT_HTTP_HEADERS__ = {
        'Content-Type': 'application/json', 'Accept': 'application/json'}

    def __init__(self, endpoint):
        super(HTTPServer, self).__init__(endpoint)
        self.session = Session()
        self.session.headers.update(self.__DEFAULT_HTTP_HEADERS__)
        self.last_request = None
        self.last_response = None

    def _send_message(
            self, request, headers=None, files=None, params=None, auth=None,
            cookies=None, **kwargs):
        """Transport the message to the server and return the response.

        :param request: The JSON-RPC request string.
        :return: The JSON-RPC response.
        :rtype: A string for requests, None for notifications.
        :raise requests.exceptions.RequestException:
            Raised by the requests module in the event of a communications
            error.
        """
        # Prepare the request
        req = Request(
            method='POST', url=self.endpoint, data=request, headers=headers,
            files=files, params=params, auth=auth, cookies=cookies)
        prepped = self.session.prepare_request(req)
        self.last_request = prepped
        # Log the request
        self._log_request(request, {'http_headers': prepped.headers})
        # Send the message
        response = self.session.send(prepped, **kwargs)
        # Log the response
        self._log_response(response.text, {'http_code': response.status_code, \
            'http_reason': response.reason, 'http_headers': response.headers})
        self.last_response = response
        return response.text

{'fraction_non_alphanumeric': 0.10149110149110149, 'fraction_numerical': 0.0, 'mean_word_length': 2.683790965456156, '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': '1435795', 'n_tokens_mistral': 1087, 'n_tokens_neox': 1080, 'n_words': 316}