From 113ed9a8ae130cbbbeac5c6e3120d0d576e78100 Mon Sep 17 00:00:00 2001 From: Anton Shutik Date: Fri, 15 Nov 2024 12:45:06 +0100 Subject: [PATCH] Removed hooks --- shopify_client/hooks.py | 18 -------------- tests/test_hooks.py | 52 ----------------------------------------- 2 files changed, 70 deletions(-) delete mode 100644 shopify_client/hooks.py delete mode 100644 tests/test_hooks.py diff --git a/shopify_client/hooks.py b/shopify_client/hooks.py deleted file mode 100644 index 2671690..0000000 --- a/shopify_client/hooks.py +++ /dev/null @@ -1,18 +0,0 @@ -import time - - -def rate_limit(response, *args, **kwargs): - max_retry_count = 5 - retry_count = int(response.request.headers.get("X-Retry-Count", 0)) - print(f"Response: {response}") - - if response.status_code == 429 and retry_count < max_retry_count: - retry_after = int(response.headers.get("retry-after", 4)) - # print(f"Retry after: {retry_after}") - time.sleep(retry_after) - response.request.headers["X-Retry-Count"] = retry_count + 1 - new_response = response.connection.send(response.request) - new_response.history.append(response) - return rate_limit(new_response, *args, **kwargs) - - return response \ No newline at end of file diff --git a/tests/test_hooks.py b/tests/test_hooks.py deleted file mode 100644 index d9ad769..0000000 --- a/tests/test_hooks.py +++ /dev/null @@ -1,52 +0,0 @@ -from unittest.mock import MagicMock -import requests - -from shopify_client.hooks import rate_limit -import pytest - -@pytest.fixture -def mock_connection(): - """Fixture to create a mock connection object.""" - return MagicMock() - -@pytest.fixture -def initial_response(mock_connection): - """Fixture to create an initial response object.""" - response = requests.Response() - response.status_code = 429 - response.headers = {"retry-after": "2"} - response.request = requests.Request("GET", "https://api.example.com/resource").prepare() - response.request.headers["X-Retry-Count"] = 0 - response.connection = mock_connection - return response - -def test_rate_limit_retries_on_429(monkeypatch, initial_response, mock_connection): - """Test that the rate_limit function retries on a 429 response.""" - new_response = requests.Response() - new_response.status_code = 200 # Successful response to stop the retry loop - new_response.request = initial_response.request - mock_connection.send.return_value = new_response - - monkeypatch.setattr("time.sleep", lambda x: None) - - final_response = rate_limit(initial_response) - - assert final_response.status_code == 200 - assert mock_connection.send.call_count == 1 - assert final_response.request.headers["X-Retry-Count"] == 1 - -def test_rate_limit_stops_after_max_retries(monkeypatch, initial_response, mock_connection): - failed_response = requests.Response() - failed_response.status_code = 429 - failed_response.request = initial_response.request - failed_response.connection = mock_connection - - mock_connection.send.return_value = failed_response - - monkeypatch.setattr("time.sleep", lambda x: None) - - final_response = rate_limit(initial_response) - - assert final_response.status_code == 429 - assert mock_connection.send.call_count == 5 - assert final_response.request.headers["X-Retry-Count"] == 5 \ No newline at end of file