コード例 #1
0
    def _send_web_request(self, url, payload, attempts=1):
        """Send out a web request and retry on failure.

        TODO: Currently this is a blocking operation. Devising a way to send
        these requests without blocking would be benificial.

        Args:
            url (unicode):
                The URL to send the request to.

            payload (unicode):
                The JSON-encoded payload to send.

            attempts (int):
                The number of retry attempts left.
        """
        request = Request(url)
        arguments = urlencode({
            'payload': payload,
        })
        # The addition of data automatically converts request to a POST.
        request.add_data(arguments)

        while attempts:
            try:
                return urlopen(request)
            except URLError:
                attempts -= 1

        logging.warning('Sending WebHook Request failed: %s ' % url)