Skip to content

Commit

Permalink
BaseRESTConfigMixin: retrying of API call
Browse files Browse the repository at this point in the history
Added retrying mechanism for API calls.
  • Loading branch information
enhaut authored and olichtne committed Jul 12, 2024
1 parent dd90b17 commit cf77e38
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions lnst/Recipes/ENRT/ConfigMixins/BaseRESTConfigMixin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time
import logging
import requests
from requests.exceptions import RequestException

from lnst.Common.LnstError import LnstError
from lnst.Common.Parameters import BoolParam, StrParam
Expand All @@ -11,6 +13,10 @@ class BaseRESTConfigMixin:
rest_password = StrParam()
ssl_verify = BoolParam(default=True, mandatory=False)

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.BASE_DELAY = 1

@staticmethod
def __get_request_function(method: str):
try:
Expand All @@ -28,11 +34,28 @@ def __build_request(self, endpoint: str, **kwargs):

return kwargs

def api_request(self, method: str, endpoint: str, response_code: int = 200, **kwargs) -> bytes:
def api_request(self, method: str, endpoint: str, response_code: int = 200, tries: int = 7, **kwargs) -> bytes:
request = self.__build_request(endpoint, **kwargs)
req_func = self.__get_request_function(method)

response = req_func(**request)
for i in range(1, tries+1): # i starts from 1
try:
response = req_func(**request)
except RequestException as e:
delay = self.BASE_DELAY * 2 ** i

logging.error(
f"API request ({i}/{tries}) failed. Retrying in {delay} seconds.",
exc_info=True,
)

time.sleep(delay)
continue

break # request was successful
else:
raise LnstError(f"API request failed after {tries} tries")

if response.status_code != response_code:
raise LnstError(f"Request failed with status code {response.status_code}")

Expand Down

0 comments on commit cf77e38

Please sign in to comment.