Skip to content

Commit

Permalink
add an optional custom timeout to requests
Browse files Browse the repository at this point in the history
  • Loading branch information
splch committed Apr 29, 2024
1 parent 500e9a0 commit bc91086
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pennylane_ionq/api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class APIClient:
HOSTNAME = "api.ionq.co/v0.1"
BASE_URL = "https://{}".format(HOSTNAME)

def __init__(self, **kwargs):
def __init__(self, timeout_seconds=600, **kwargs):
self.AUTHENTICATION_TOKEN = os.getenv("IONQ_API_KEY") or kwargs.get("api_key", None)
self.DEBUG = False

Expand All @@ -130,7 +130,7 @@ def __init__(self, **kwargs):
self.HEADERS = {"User-Agent": self.USER_AGENT}

# Ten minute timeout on requests.
self.TIMEOUT_SECONDS = 60 * 10
self.TIMEOUT_SECONDS = timeout_seconds

if self.AUTHENTICATION_TOKEN:
self.set_authorization_header(self.AUTHENTICATION_TOKEN)
Expand Down
21 changes: 21 additions & 0 deletions tests/test_api_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,27 @@ def test_get(self, monkeypatch):
mock_client.get.assert_called_once()
manager.handle_response.assert_called_once_with(mock_response)

def test_timeout_setting(self, monkeypatch):
"""
Test that the APIClient uses a custom timeout setting correctly for its requests.
"""
custom_timeout = 1200 # Custom timeout in seconds
client = api_client.APIClient(api_key="test", timeout_seconds=custom_timeout)
assert (
client.TIMEOUT_SECONDS == custom_timeout
), "The custom timeout should be set correctly."

# Mock the requests method to check if the custom timeout is being used
mock_request = MagicMock()
monkeypatch.setattr(requests, "get", mock_request)

client.get("https://example.com")
mock_request.assert_called_once()
_, kwargs = mock_request.call_args
assert (
kwargs["timeout"] == custom_timeout
), "The custom timeout should be used in the request."

def test_create_unsupported(self):
"""
Test a POST (create) request with a resource that does not support that type or request.
Expand Down

0 comments on commit bc91086

Please sign in to comment.