Skip to content

Commit 820d978

Browse files
COmanage API Request library changes for PR
1 parent 2e9bd59 commit 820d978

File tree

2 files changed

+31
-20
lines changed

2 files changed

+31
-20
lines changed

comanage_utils.py

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import os
44
import re
5-
import sys
65
import json
76
import time
7+
import exceptions
88
import urllib.error
99
import urllib.request
1010
from ldap3 import Server, Connection, ALL, ALL_ATTRIBUTES, SAFE_SYNC
@@ -27,9 +27,8 @@
2727
TEST_UNIX_CLUSTER_ID = 10
2828
TEST_LDAP_TARGET_ID = 9
2929

30-
31-
TIMEOUT_MIN = 5
32-
TIMEOUT_MULTIPLE = 5
30+
# Value for the base of the exponential backoff
31+
TIMEOUT_BASE = 5
3332
MAX_RETRIES = 5
3433

3534

@@ -83,28 +82,29 @@ def call_api2(method, target, endpoint, authstr, **kw):
8382
def call_api3(method, target, data, endpoint, authstr, **kw):
8483
req = mkrequest(method, target, data, endpoint, authstr, **kw)
8584
retries = 0
86-
currentTimeout = TIMEOUT_MIN
87-
requestingStart = time.time()
85+
current_timeout = TIMEOUT_BASE
86+
total_timeout = 0
8887
payload = None
89-
while payload == None:
88+
while retries <= MAX_RETRIES:
9089
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.")
90+
resp = urllib.request.urlopen(req, timeout=current_timeout)
9491
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}. "
92+
break
93+
# exception catching, mainly for request timeouts and "Service Temporarily Unavailable" (Rate limiting).
94+
except urllib.error.HTTPError as exception:
95+
if retries >= MAX_RETRIES:
96+
raise exceptions.URLRequestError(
97+
"Exception raised after maximum number of retries reached after total backoff of " +
98+
f"{total_timeout} seconds. Retries: {retries}. "
10599
+ f"Exception reason: {exception}.\n Request: {req.full_url}"
106100
)
107101

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

110110

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)