Exemple #1
0
    def test_request_raises_error_after_5_retries(self, mock_sleep,
                                                  mock_request):

        service_unavailable_response = Response({})
        service_unavailable_response.status = 403
        content = google_error_response(403, "userRateLimitExceeded")
        logging.info(content)

        mock_request.return_value = (service_unavailable_response, content)

        response = HttpWithBackoff().request('http://fake.com',
                                             method="PUT",
                                             body="bar",
                                             headers="foo",
                                             redirections=10,
                                             connection_type="wibble")

        assert_equal(response, (service_unavailable_response, content))

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

        mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo",
                                        10, "wibble")
Exemple #2
0
    def test_request_with_only_uri_calls_Http_with_default_params(
            self, mock_request):

        response = Response({})
        response.status = 200
        mock_request.return_value = (response, 'content')
        HttpWithBackoff().request('http://fake.com')

        mock_request.assert_called_with('http://fake.com', "GET", None, None,
                                        5, None)
Exemple #3
0
 def test_request_sleeps_correctly_on_403_quotaExceeded(self):
     _request_and_assert(
         lambda: HttpWithBackoff().request('http://fake.com',
                                           method="PUT",
                                           body="bar",
                                           headers="foo",
                                           redirections=10,
                                           connection_type="wibble"),
         call('http://fake.com', "PUT", "bar", "foo", 10, "wibble"), 403,
         "quotaExceeded")
Exemple #4
0
    def test_request_with_full_args_calls_Http_with_args(self, mock_request):

        response = Response({})
        response.status = 200
        mock_request.return_value = (response, 'content')
        HttpWithBackoff().request('http://fake.com',
                                  method="PUT",
                                  body="bar",
                                  headers="foo",
                                  redirections=10,
                                  connection_type="wibble")

        mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo",
                                        10, "wibble")
def create_client(credentials):
    if "CLIENT_SECRETS" in credentials and "STORAGE_PATH" in credentials:
        return from_secrets_file(
            credentials['CLIENT_SECRETS'],
            storage_path=credentials['STORAGE_PATH'],
            http_client=HttpWithBackoff(),
            ga_hook=track_ga_api_usage,
        )
    elif "ACCOUNT_NAME" in credentials:
        return from_private_key(
            credentials['ACCOUNT_NAME'],
            private_key_path=credentials['PRIVATE_KEY'],
            storage_path=credentials['STORAGE_PATH'],
            http_client=HttpWithBackoff(),
            ga_hook=track_ga_api_usage,
        )
    else:
        return from_credentials_db(
            credentials['CLIENT_SECRETS']['installed'],
            credentials['OAUTH2_CREDENTIALS'],
            http_client=HttpWithBackoff(),
            ga_hook=track_ga_api_usage,
        )
Exemple #6
0
    def test_request_does_not_sleep_on_503(self, mock_sleep, mock_request):
        backendError = Response({})
        backendError.status = 503
        content = google_error_response(503, "backendError")

        mock_request.return_value = (backendError, content)

        HttpWithBackoff().request('http://fake.com',
                                  method="PUT",
                                  body="bar",
                                  headers="foo",
                                  redirections=10,
                                  connection_type="wibble")

        assert_equal([], mock_sleep.call_args_list)

        mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo",
                                        10, "wibble")
Exemple #7
0
    def test_request_does_not_sleep_on_404(self, mock_sleep, mock_request):

        not_found_response = Response({})
        not_found_response.status = 404
        content = 'content'
        mock_request.return_value = (not_found_response, content)

        HttpWithBackoff().request('http://fake.com',
                                  method="PUT",
                                  body="bar",
                                  headers="foo",
                                  redirections=10,
                                  connection_type="wibble")

        assert_equal([], mock_sleep.call_args_list)

        mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo",
                                        10, "wibble")
Exemple #8
0
    def test_request_treats_403_dailyLimitExceeded_as_bad_error(
            self, mock_error, mock_sleep, mock_request):

        bad_response = Response({})
        bad_response.status = 403
        content = google_error_response(403, 'dailyLimitExceeded')
        mock_request.return_value = (bad_response, content)

        HttpWithBackoff().request('http://fake.com',
                                  method="PUT",
                                  body="bar",
                                  headers="foo",
                                  redirections=10,
                                  connection_type="wibble")

        assert_equal([], mock_sleep.call_args_list)

        mock_request.assert_called_with('http://fake.com', "PUT", "bar", "foo",
                                        10, "wibble")
        mock_error.assert_called()