-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from groveco/rename-query
Rename query
- Loading branch information
Showing
9 changed files
with
118 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,30 @@ | ||
from .test_endpoint import mock_client | ||
from copy import deepcopy | ||
from unittest.mock import Mock | ||
|
||
from shopify_client import ShopifyClient | ||
from shopify_client.endpoint import Endpoint | ||
import pytest | ||
|
||
|
||
@pytest.fixture | ||
def mock_client(mocker): | ||
client = mocker.Mock() | ||
client.parse_response.side_effect = lambda x: x # Just return the response data as-is | ||
return client | ||
|
||
@pytest.fixture | ||
def endpoint(mock_client): | ||
return Endpoint(client=mock_client, endpoint="test_endpoint") | ||
|
||
|
||
@pytest.fixture | ||
def shopify_client(mocker): | ||
return ShopifyClient(api_url="https://test-shop.myshopify.com", api_token="test-token") | ||
|
||
# Create a new mock that will deepcopy the arguments passed to it | ||
# https://docs.python.org/3.7/library/unittest.mock-examples.html#coping-with-mutable-arguments | ||
class CopyingMock(Mock): | ||
def __call__(self, *args, **kwargs): | ||
args = deepcopy(args) | ||
kwargs = deepcopy(kwargs) | ||
return super().__call__(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
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 |