From f775924e5378e0a23e7ad8b305b5dc2c826fb17e Mon Sep 17 00:00:00 2001 From: Spencer Churchill Date: Sun, 28 Apr 2024 19:28:34 -0700 Subject: [PATCH] non-test-passing retry logic --- pennylane_ionq/api_client.py | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/pennylane_ionq/api_client.py b/pennylane_ionq/api_client.py index 0162528..ecd6ff0 100644 --- a/pennylane_ionq/api_client.py +++ b/pennylane_ionq/api_client.py @@ -116,7 +116,7 @@ class APIClient: HOSTNAME = "api.ionq.co/v0.3" BASE_URL = "https://{}".format(HOSTNAME) - def __init__(self, timeout_seconds=600, **kwargs): + def __init__(self, timeout_seconds=200, **kwargs): self.AUTHENTICATION_TOKEN = ( kwargs.get("api_key", None) or os.getenv("PENNYLANE_IONQ_API_KEY") @@ -136,6 +136,9 @@ def __init__(self, timeout_seconds=600, **kwargs): # sets the default timeout for API requests self.TIMEOUT_SECONDS = timeout_seconds + # sets the default number of retries for API requests + self.RETRIES = 3 + if self.AUTHENTICATION_TOKEN: self.set_authorization_header(self.AUTHENTICATION_TOKEN) else: @@ -169,9 +172,10 @@ def join_path(self, path): def request(self, method, **params): """ - Calls ``method`` with ``params`` after applying headers. Records the request type and - parameters to ``self.errors`` if the request is not successful, and the response to - ``self.responses`` if a response is returned from the server. + Calls ``method`` with ``params`` after applying headers. Implements retry logic for + network-related errors. Records the request type and parameters to ``self.errors`` if + the request is not successful, and the response to ``self.responses`` if a response is + returned from the server. Args: method: one of ``requests.get`` or ``requests.post`` @@ -185,18 +189,24 @@ def request(self, method, **params): raise TypeError("Unexpected or unsupported method provided") params["headers"] = self.HEADERS - params["timeout"] = self.TIMEOUT_SECONDS + attempt = 0 + + while attempt < self.RETRIES: + try: + response = method(**params) + if response.status_code < 500: # Consider 5xx errors as potential retries, adjust as needed + return response + except (requests.exceptions.ConnectionError, requests.exceptions.Timeout) as e: + if attempt == self.RETRIES - 1: + # Log or handle the final failed attempt as needed + if self.DEBUG: + self.errors.append((method.__name__, params, str(e))) + raise + attempt += 1 - try: - response = method(**params) - except Exception as e: if self.DEBUG: - self.errors.append((method, params, e)) - raise - - if self.DEBUG: - self.responses.append(response) + self.responses.append(response) return response