<commit_before>import logging

from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from upload_util import encode_multipart_formdata

logger = logging.getLogger(__name__)


class ContactClient(object):
    '''
    Async HTTP client to make facilitate request sending to newebe contacts.
    '''

    def __init__(self, activity=None):
        '''
        Register activity in which errors will be stored if a request to a
        contact failed.
        '''
        
        self.contacts = dict()
        self.client = AsyncHTTPClient()
        self.activity = activity
        self.extra = ""


    def get(self, contact, path):
        '''
        Perform a GET request to given contact.
        '''

        url = contact.url + path
        request = HTTPRequest(url)
        return self.client.fetch(self, request)


    def post(self, contact, path, body):
        '''
        Perform a POST request to given contact.
        '''

        url = contact.url + path
        request = HTTPRequest(url, method="POST", body=body)
        self.contacts[request] = contact
        return self.client.fetch(request, self.on_contact_response)


    def post_files(self, contact, path, fields={}, files={}):
        '''
        Post file and fields to givent contact.
        '''

        (contentType, body) = encode_multipart_formdata(fields=fields, 
                                                        files=files)
        headers = { 'Content-Type': contentType } 

        url = contact.url + path
        request = HTTPRequest(url=url, method="POST", 
                              body=body, headers=headers)
        self.contacts[request] = contact
        return self.client.fetch(request, self.on_contact_response)


    def delete(self, contact, path, body, extra=None):
        '''
        Perform a DELETE request to given contact (PUT is send because tornado
        does not handle DELETE request with body).
        '''

        url = contact.url + path
        request = HTTPRequest(url, method="PUT", body=body)
        self.contacts[request] = contact
        self.extra = extra
        return self.client.fetch(request, self.on_contact_response)


    def on_contact_response(self, response, **kwargs):
        '''
        Callback for requests sent to contacts. If error occurs it 
        marks it inside the activity for which error occurs. Else 
        it logs that micropost posting succeeds.
        '''
        
        contact = self.contacts[response.request]

        if response.error: 
            logger.error(""" Request to a contact failed, error infos 
                             are stored inside activity.""")            

            self.activity.add_error(contact, extra=self.extra)
            self.activity.save()

        else: 
            logger.info("Request successfully sent to {}.".format(contact.name))
        
        del self.contacts[response.request]

<commit_msg>debug logging to avoid exceptions<commit_after>import logging

from tornado.httpclient import AsyncHTTPClient, HTTPRequest
from upload_util import encode_multipart_formdata

logger = logging.getLogger(__name__)


class ContactClient(object):
    '''
    Async HTTP client to make facilitate request sending to newebe contacts.
    '''

    def __init__(self, activity=None):
        '''
        Register activity in which errors will be stored if a request to a
        contact failed.
        '''
        
        self.contacts = dict()
        self.client = AsyncHTTPClient()
        self.activity = activity
        self.extra = ""


    def get(self, contact, path):
        '''
        Perform a GET request to given contact.
        '''

        url = contact.url + path
        request = HTTPRequest(url)
        return self.client.fetch(self, request)


    def post(self, contact, path, body):
        '''
        Perform a POST request to given contact.
        '''

        url = contact.url + path
        request = HTTPRequest(url, method="POST", body=body)
        self.contacts[request] = contact
        return self.client.fetch(request, self.on_contact_response)


    def post_files(self, contact, path, fields={}, files={}):
        '''
        Post file and fields to givent contact.
        '''

        (contentType, body) = encode_multipart_formdata(fields=fields, 
                                                        files=files)
        headers = { 'Content-Type': contentType } 

        url = contact.url + path
        request = HTTPRequest(url=url, method="POST", 
                              body=body, headers=headers)
        self.contacts[request] = contact
        return self.client.fetch(request, self.on_contact_response)


    def delete(self, contact, path, body, extra=None):
        '''
        Perform a DELETE request to given contact (PUT is send because tornado
        does not handle DELETE request with body).
        '''

        url = contact.url + path
        request = HTTPRequest(url, method="PUT", body=body)
        self.contacts[request] = contact
        self.extra = extra
        return self.client.fetch(request, self.on_contact_response)


    def on_contact_response(self, response, **kwargs):
        '''
        Callback for requests sent to contacts. If error occurs it 
        marks it inside the activity for which error occurs. Else 
        it logs that micropost posting succeeds.
        '''
        
        contact = self.contacts[response.request]

        if response.error: 
            logger.error(""" Request to a contact failed, error infos 
                             are stored inside activity.""")            

            self.activity.add_error(contact, extra=self.extra)
            self.activity.save()

        elif contact and contact.name: 
            logger.info("Request successfully sent to {}.".format(contact.name))
        
        del self.contacts[response.request]


{'fraction_non_alphanumeric': 0.09057050956492298, 'fraction_numerical': 0.0, 'mean_word_length': 2.034411915767848, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5784174', 'n_tokens_mistral': 1526, 'n_tokens_neox': 1452, 'n_words': 453}