コード例 #1
0
    def test_post(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('POST',
                                'http://localhost',
                                json={'test': 'value'},
                                status_code=201)

            request_handler = RequestHandler()
            data, status = request_handler.post(
                'http://localhost',
                json_payload={'hello': 'world'},
                api_key='8900',
                username='******',
                event='myevent',
                signature='mysignature')
            self.assertEqual(status, client.CREATED)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'samjones')
            self.assertEqual(request_handler.headers['api-key'], '8900')
            self.assertEqual(request_handler.headers['event'], 'myevent')
            self.assertEqual(request_handler.headers['pywebhooks-signature'],
                             'mysignature')
            self.assertEqual(request_handler.headers['Content-Type'],
                             'application/json')
            self.assertEqual(request_handler.headers['Accept'],
                             'application/json')
コード例 #2
0
def client_echo_valid(endpoint):
    """
    This will validate if the user's endpoint is valid and returning the echo
    data sent to it
    """
    try:
        request_handler = RequestHandler(verify_ssl=False,
                                         request_timeout=REQUEST_TIMEOUT)
        validation_key = common.generate_key()

        try:
            returned_json, status_code = request_handler.get(
                endpoint, params={'echo': validation_key})
        # pylint: disable=W0703
        except:
            return False

        if status_code != client.OK:
            return False
        if returned_json['echo'] != validation_key:
            return False
    # pylint: disable=W0703
    except Exception:
        return False

    return True
コード例 #3
0
def client_echo_valid(endpoint):
    """
    This will validate if the user's endpoint is valid and returning the echo
    data sent to it
    """
    try:
        request_handler = RequestHandler(
            verify_ssl=False, request_timeout=REQUEST_TIMEOUT)
        validation_key = common.generate_key()

        try:
            returned_json, status_code = request_handler.get(
                endpoint, params={'echo': validation_key})
        # pylint: disable=W0703
        except:
            return False

        if status_code != client.OK:
            return False
        if returned_json['echo'] != validation_key:
            return False
    # pylint: disable=W0703
    except Exception:
        return False

    return True
コード例 #4
0
    def test_post(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('POST', 'http://localhost',
                                json={'test': 'value'},
                                status_code=201)

            request_handler = RequestHandler()
            data, status = request_handler.post(
                'http://localhost',
                json_payload={'hello': 'world'},
                api_key='8900',
                username='******',
                event='myevent',
                signature='mysignature'
            )
            self.assertEqual(status, client.CREATED)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'samjones')
            self.assertEqual(request_handler.headers['api-key'], '8900')
            self.assertEqual(request_handler.headers['event'], 'myevent')
            self.assertEqual(
                request_handler.headers['pywebhooks-signature'], 'mysignature')
            self.assertEqual(
                request_handler.headers['Content-Type'], 'application/json')
            self.assertEqual(
                request_handler.headers['Accept'], 'application/json')
コード例 #5
0
def notify_subscribed_accounts(self, event=None, event_data=None,
                               secret_key=None, endpoint=None,
                               account_id=None):
    """
    Send Webhook requests to all subscribed accounts
    """
    signature = create_signature(secret_key, event_data)

    request_handler = RequestHandler(
        verify_ssl=False, request_timeout=REQUEST_TIMEOUT)

    try:
        _, status_code = request_handler.post(
            url=endpoint,
            json_payload=event_data, event=event,
            signature=signature)

        # We don't care about anything but the return status code
        if client.OK != status_code:
            raise Exception('Endpoint returning non HTTP 200 status. '
                            'Actual code returned: {0}'.format(status_code))

        if client.OK == status_code:
            # Failed count will reset on a good contact
            update_failed_count(account_id, increment_failed_count=False)

    except Exception as exc:
        update_failed_count(account_id, increment_failed_count=True)

        if self.request.retries == 3:
            raise self.retry(exc=exc, countdown=DEFAULT_FINAL_RETRY)
        else:
            raise self.retry(exc=exc, countdown=DEFAULT_RETRY)
コード例 #6
0
    def test_delete(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('DELETE',
                                'http://localhost/45678',
                                json={'test': 'value'},
                                status_code=200)

            request_handler = RequestHandler()
            data, status = request_handler.delete('http://localhost/45678',
                                                  api_key='765434',
                                                  username='******')
            self.assertEqual(status, client.OK)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'birk')
            self.assertEqual(request_handler.headers['api-key'], '765434')
            self.assertEqual(request_handler.headers['Content-Type'],
                             'application/json')
            self.assertEqual(request_handler.headers['Accept'],
                             'application/json')
コード例 #7
0
    def test_put(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('PUT',
                                'http://localhost',
                                json={'test': 'value'},
                                status_code=200)

            request_handler = RequestHandler()
            data, status = request_handler.put('http://localhost',
                                               json_payload={'hello': 'world'},
                                               api_key='555',
                                               username='******')
            self.assertEqual(status, client.OK)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'janedoe')
            self.assertEqual(request_handler.headers['api-key'], '555')
            self.assertEqual(request_handler.headers['Content-Type'],
                             'application/json')
            self.assertEqual(request_handler.headers['Accept'],
                             'application/json')
コード例 #8
0
    def test_get(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('GET',
                                'http://localhost?test=123',
                                json={'test': 'value'},
                                status_code=200)

            request_handler = RequestHandler()
            data, status = request_handler.get('http://localhost',
                                               params={'test': 123},
                                               api_key='12345',
                                               username='******')
            self.assertEqual(status, client.OK)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'johndoe')
            self.assertEqual(request_handler.headers['api-key'], '12345')
            self.assertEqual(request_handler.headers['Content-Type'],
                             'application/json')
            self.assertEqual(request_handler.headers['Accept'],
                             'application/json')
コード例 #9
0
    def test_delete(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('DELETE', 'http://localhost/45678',
                                json={'test': 'value'},
                                status_code=200)

            request_handler = RequestHandler()
            data, status = request_handler.delete(
                'http://localhost/45678',
                api_key='765434',
                username='******'
            )
            self.assertEqual(status, client.OK)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'birk')
            self.assertEqual(request_handler.headers['api-key'], '765434')
            self.assertEqual(
                request_handler.headers['Content-Type'], 'application/json')
            self.assertEqual(
                request_handler.headers['Accept'], 'application/json')
コード例 #10
0
    def test_patch(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('PATCH', 'http://localhost',
                                json={'test': 'value'},
                                status_code=200)

            request_handler = RequestHandler()
            data, status = request_handler.patch(
                'http://localhost',
                json_payload={'hello': 'world'},
                api_key='01245',
                username='******'
            )
            self.assertEqual(status, client.OK)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'natml')
            self.assertEqual(request_handler.headers['api-key'], '01245')
            self.assertEqual(
                request_handler.headers['Content-Type'], 'application/json')
            self.assertEqual(
                request_handler.headers['Accept'], 'application/json')
コード例 #11
0
    def test_get(self):

        with requests_mock.Mocker() as mocker:
            mocker.register_uri('GET', 'http://localhost?test=123',
                                json={'test': 'value'},
                                status_code=200)

            request_handler = RequestHandler()
            data, status = request_handler.get(
                'http://localhost',
                params={'test': 123},
                api_key='12345',
                username='******'
            )
            self.assertEqual(status, client.OK)
            self.assertEqual({'test': 'value'}, data)
            self.assertEqual(request_handler.headers['username'], 'johndoe')
            self.assertEqual(request_handler.headers['api-key'], '12345')
            self.assertEqual(
                request_handler.headers['Content-Type'], 'application/json')
            self.assertEqual(
                request_handler.headers['Accept'], 'application/json')
コード例 #12
0
def client_reset_key(endpoint, key_type, key_value):
    """
    This will send an api_key or secret_key to the configured endpoint
    (assists with resets of an api_key or secret_key)
    """
    try:
        request_handler = RequestHandler(verify_ssl=False,
                                         request_timeout=REQUEST_TIMEOUT)

        try:
            returned_json, status_code = request_handler.get(
                endpoint, params={key_type: key_value})
        # pylint: disable=W0703
        except:
            return False

        if status_code != client.OK:
            return False
    # pylint: disable=W0703
    except Exception:
        return False

    return True
コード例 #13
0
def client_reset_key(endpoint, key_type, key_value):
    """
    This will send an api_key or secret_key to the configured endpoint
    (assists with resets of an api_key or secret_key)
    """
    try:
        request_handler = RequestHandler(
            verify_ssl=False, request_timeout=REQUEST_TIMEOUT)

        try:
            returned_json, status_code = request_handler.get(
                endpoint, params={key_type: key_value})
        # pylint: disable=W0703
        except:
            return False

        if status_code != client.OK:
            return False
    # pylint: disable=W0703
    except Exception:
        return False

    return True
コード例 #14
0
def notify_subscribed_accounts(self,
                               event=None,
                               event_data=None,
                               secret_key=None,
                               endpoint=None,
                               account_id=None):
    """
    Send Webhook requests to all subscribed accounts
    """
    signature = create_signature(secret_key, event_data)

    request_handler = RequestHandler(verify_ssl=False,
                                     request_timeout=REQUEST_TIMEOUT)

    try:
        _, status_code = request_handler.post(url=endpoint,
                                              json_payload=event_data,
                                              event=event,
                                              signature=signature)

        # We don't care about anything but the return status code
        if client.OK != status_code:
            raise Exception('Endpoint returning non HTTP 200 status. '
                            'Actual code returned: {0}'.format(status_code))

        if client.OK == status_code:
            # Failed count will reset on a good contact
            update_failed_count(account_id, increment_failed_count=False)

    except Exception as exc:
        update_failed_count(account_id, increment_failed_count=True)

        if self.request.retries == 3:
            raise self.retry(exc=exc, countdown=DEFAULT_FINAL_RETRY)
        else:
            raise self.retry(exc=exc, countdown=DEFAULT_RETRY)