Beispiel #1
0
    def test_post_does_not_sleep_on_404(self, mock_sleep, mock_request):

        not_found_response = requests.Response()
        not_found_response.status_code = 404
        mock_request.return_value = not_found_response

        with assert_raises(requests.HTTPError) as e:
            requests_with_backoff.post('http://fake.com',
                                       data={},
                                       kwarg1='a kwarg',
                                       kwarg2='another kwarg')
        assert_equal(e.exception.response.status_code, 404)

        assert_equal([], mock_sleep.call_args_list)

        mock_request.assert_called_with('POST',
                                        'http://fake.com',
                                        data={},
                                        kwarg1='a kwarg',
                                        kwarg2='another kwarg')
Beispiel #2
0
 def test_post_sleeps_correctly_on_403(self):
     _request_and_assert(
         lambda: requests_with_backoff.post('http://fake.com',
                                            data={},
                                            kwarg1='a kwarg',
                                            kwarg2='another kwarg'),
         call('POST',
              'http://fake.com',
              data={},
              kwarg1='a kwarg',
              kwarg2='another kwarg'), 403)
 def test_post_sleeps_correctly_on_403(self):
     _request_and_assert(
         lambda: requests_with_backoff.post('http://fake.com',
                                            data={},
                                            kwarg1='a kwarg',
                                            kwarg2='another kwarg'),
         call('POST',
              'http://fake.com',
              data={},
              kwarg1='a kwarg',
              kwarg2='another kwarg'),
         403)
Beispiel #4
0
    def test_post_raises_error_after_5_retries(self, mock_sleep, mock_request):

        service_unavailable_response = requests.Response()
        service_unavailable_response.status_code = 503
        mock_request.return_value = service_unavailable_response

        with assert_raises(requests.HTTPError) as e:
            requests_with_backoff.post('http://fake.com',
                                       kwarg1='a kwarg',
                                       kwarg2='another kwarg')
        assert_equal(e.exception.response.status_code, 503)

        assert_equal(
            [call(10), call(20),
             call(40), call(80),
             call(160)], mock_sleep.call_args_list)

        mock_request.assert_called_with('POST',
                                        'http://fake.com',
                                        kwarg1='a kwarg',
                                        kwarg2='another kwarg')
    def test_post_raises_error_after_5_retries(self,
                                               mock_sleep,
                                               mock_request):

        service_unavailable_response = requests.Response()
        service_unavailable_response.status_code = 503
        mock_request.return_value = service_unavailable_response

        with assert_raises(requests.HTTPError) as e:
            requests_with_backoff.post('http://fake.com',
                                       kwarg1='a kwarg',
                                       kwarg2='another kwarg')
        assert_equal(e.exception.response.status_code, 503)

        assert_equal(
            [call(10), call(20), call(40), call(80), call(160)],
            mock_sleep.call_args_list)

        mock_request.assert_called_with('POST',
                                        'http://fake.com',
                                        kwarg1='a kwarg',
                                        kwarg2='another kwarg')
    def test_post_does_not_sleep_on_404(self,
                                        mock_sleep,
                                        mock_request):

        not_found_response = requests.Response()
        not_found_response.status_code = 404
        mock_request.return_value = not_found_response

        with assert_raises(requests.HTTPError) as e:
            requests_with_backoff.post('http://fake.com',
                                       data={},
                                       kwarg1='a kwarg',
                                       kwarg2='another kwarg')
        assert_equal(e.exception.response.status_code, 404)

        assert_equal(
            [],
            mock_sleep.call_args_list)

        mock_request.assert_called_with('POST',
                                        'http://fake.com',
                                        data={},
                                        kwarg1='a kwarg',
                                        kwarg2='another kwarg')
    def test_post_proxies_requests_post(self, mock_request):
        good_response = _make_good_response()
        mock_request.return_value = good_response

        response = requests_with_backoff.post('http://fake.com',
                                              data={},
                                              kwarg1='a kwarg',
                                              kwarg2='another kwarg')

        assert_is(response, good_response)
        mock_request.assert_called_with('POST',
                                        'http://fake.com',
                                        data={},
                                        kwarg1='a kwarg',
                                        kwarg2='another kwarg')
Beispiel #8
0
    def test_post_proxies_requests_post(self, mock_request):
        good_response = _make_good_response()
        mock_request.return_value = good_response

        response = requests_with_backoff.post('http://fake.com',
                                              data={},
                                              kwarg1='a kwarg',
                                              kwarg2='another kwarg')

        assert_is(response, good_response)
        mock_request.assert_called_with('POST',
                                        'http://fake.com',
                                        data={},
                                        kwarg1='a kwarg',
                                        kwarg2='another kwarg')
    def post(self, records):
        headers = DataSet._make_headers(self.token)
        json_body = DataSet._encode_json(records)

        if self.dry_run:
            DataSet._log_request('POST', self.url, headers, json_body)
        else:
            response = requests_with_backoff.post(
                url=self.url,
                headers=headers,
                data=json_body
            )

            try:
                response.raise_for_status()
            except:
                logging.error('[PP: {}]\n{}'.format(
                    self.url,
                    response.text))
                raise

            logging.debug("[PP] " + response.text)