Skip to content

Commit 6747fd3

Browse files
COmanage API Request library changes for PR
1 parent 2e9bd59 commit 6747fd3

File tree

2 files changed

+32
-20
lines changed

2 files changed

+32
-20
lines changed

comanage_utils.py

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sys
66
import json
77
import time
8+
import exceptions
89
import urllib.error
910
import urllib.request
1011
from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, SAFE_SYNC
@@ -27,10 +28,9 @@
2728
TEST_UNIX_CLUSTER_ID = 10
2829
TEST_LDAP_TARGET_ID = 9
2930

30-
31-
TIMEOUT_MIN = 5
32-
TIMEOUT_MULTIPLE = 5
33-
MAX_RETRIES = 5
31+
# Value for the base of the exponential backoff
32+
TIMEOUT_BASE = 5
33+
MAX_RETRIES = 2
3434

3535

3636
GET = "GET"
@@ -83,28 +83,29 @@ def call_api2(method, target, endpoint, authstr, **kw):
8383
def call_api3(method, target, data, endpoint, authstr, **kw):
8484
req = mkrequest(method, target, data, endpoint, authstr, **kw)
8585
retries = 0
86-
currentTimeout = TIMEOUT_MIN
87-
requestingStart = time.time()
86+
current_timeout = TIMEOUT_BASE
87+
total_timeout = 0
8888
payload = None
89-
while payload == None:
89+
while retries <= MAX_RETRIES:
9090
try:
91-
resp = urllib.request.urlopen(req, timeout=currentTimeout)
92-
if retries > 0:
93-
print(f"Succeeded for request {req.full_url} after {retries} retries.")
91+
resp = urllib.request.urlopen(req, timeout=current_timeout)
9492
payload = resp.read()
95-
except urllib.error.URLError as exception:
96-
if retries < MAX_RETRIES:
97-
print(f"Error: {exception} for request {req.full_url}, sleeping for {currentTimeout} seconds and retrying.")
98-
time.sleep(currentTimeout)
99-
currentTimeout *= TIMEOUT_MULTIPLE
100-
retries += 1
101-
else:
102-
requestingStop = time.time()
103-
sys.exit(
104-
f"Exception raised after maximum number of retries reached after {requestingStop - requestingStart} seconds. Retries: {retries}. "
93+
break
94+
# exception catching, mainly for request timeouts and "Service Temporarily Unavailable" (Rate limiting).
95+
except urllib.error.HTTPError as exception:
96+
if retries >= MAX_RETRIES:
97+
raise exceptions.URLRequestError(
98+
"Exception raised after maximum number of retries reached after total backoff of " +
99+
f"{total_timeout} seconds. Retries: {retries}. "
105100
+ f"Exception reason: {exception}.\n Request: {req.full_url}"
106101
)
107102

103+
print("waiting for seconds: " + str(current_timeout))
104+
time.sleep(current_timeout)
105+
total_timeout += current_timeout
106+
current_timeout *= TIMEOUT_BASE
107+
retries += 1
108+
108109
return json.loads(payload) if payload else None
109110

110111

exceptions.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
""" Exceptions used in configuration script """
2+
3+
4+
class Error(Exception):
5+
"""Base exception class for all exceptions defined"""
6+
pass
7+
8+
9+
class URLRequestError(Error):
10+
"""Class for exceptions due to not being able to fulfill a URLRequest"""
11+
pass

0 commit comments

Comments
 (0)