Skip to content

Commit 05c2e7a

Browse files
authored
feat: Add the max_retries parameter (#598)
This is passed to requests.adapters.HTTPAdapter to allow for more retries
1 parent 2d3aa5d commit 05c2e7a

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

src/keycloak/connection.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,13 @@ class ConnectionManager(object):
5353
Either a path to an SSL certificate file, or two-tuple of
5454
(certificate file, key file).
5555
:type cert: Union[str,Tuple[str,str]]
56+
:param max_retries: The total number of times to retry HTTP requests.
57+
:type max_retries: int
5658
"""
5759

58-
def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None, cert=None):
60+
def __init__(
61+
self, base_url, headers={}, timeout=60, verify=True, proxies=None, cert=None, max_retries=1
62+
):
5963
"""Init method.
6064
6165
:param base_url: The server URL.
@@ -73,6 +77,8 @@ def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None,
7377
Either a path to an SSL certificate file, or two-tuple of
7478
(certificate file, key file).
7579
:type cert: Union[str,Tuple[str,str]]
80+
:param max_retries: The total number of times to retry HTTP requests.
81+
:type max_retries: int
7682
"""
7783
self.base_url = base_url
7884
self.headers = headers
@@ -85,7 +91,7 @@ def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None,
8591
# retry once to reset connection with Keycloak after tomcat's ConnectionTimeout
8692
# see https://github.com/marcospereirampj/python-keycloak/issues/36
8793
for protocol in ("https://", "http://"):
88-
adapter = HTTPAdapter(max_retries=1)
94+
adapter = HTTPAdapter(max_retries=max_retries)
8995
# adds POST to retry whitelist
9096
allowed_methods = set(adapter.max_retries.allowed_methods)
9197
allowed_methods.add("POST")

src/keycloak/keycloak_admin.py

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class KeycloakAdmin:
7777
Either a path to an SSL certificate file, or two-tuple of
7878
(certificate file, key file).
7979
:type cert: Union[str,Tuple[str,str]]
80+
:param max_retries: The total number of times to retry HTTP requests.
81+
:type max_retries: int
8082
:param connection: A KeycloakOpenIDConnection as an alternative to individual params.
8183
:type connection: KeycloakOpenIDConnection
8284
"""
@@ -99,6 +101,7 @@ def __init__(
99101
user_realm_name=None,
100102
timeout=60,
101103
cert=None,
104+
max_retries=1,
102105
connection: Optional[KeycloakOpenIDConnection] = None,
103106
):
104107
"""Init method.
@@ -134,6 +137,8 @@ def __init__(
134137
:param cert: An SSL certificate used by the requested host to authenticate the client.
135138
Either a path to an SSL certificate file, or two-tuple of (certificate file, key file).
136139
:type cert: Union[str,Tuple[str,str]]
140+
:param max_retries: The total number of times to retry HTTP requests.
141+
:type max_retries: int
137142
:param connection: An OpenID Connection as an alternative to individual params.
138143
:type connection: KeycloakOpenIDConnection
139144
"""
@@ -152,6 +157,7 @@ def __init__(
152157
custom_headers=custom_headers,
153158
timeout=timeout,
154159
cert=cert,
160+
max_retries=max_retries,
155161
)
156162

157163
@property

src/keycloak/keycloak_openid.py

+6
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,8 @@ class KeycloakOpenID:
7777
:param cert: An SSL certificate used by the requested host to authenticate the client.
7878
Either a path to an SSL certificate file, or two-tuple of
7979
(certificate file, key file).
80+
:param max_retries: The total number of times to retry HTTP requests.
81+
:type max_retries: int
8082
"""
8183

8284
def __init__(
@@ -90,6 +92,7 @@ def __init__(
9092
proxies=None,
9193
timeout=60,
9294
cert=None,
95+
max_retries=1,
9396
):
9497
"""Init method.
9598
@@ -114,6 +117,8 @@ def __init__(
114117
Either a path to an SSL certificate file, or two-tuple of
115118
(certificate file, key file).
116119
:type cert: Union[str,Tuple[str,str]]
120+
:param max_retries: The total number of times to retry HTTP requests.
121+
:type max_retries: int
117122
"""
118123
self.client_id = client_id
119124
self.client_secret_key = client_secret_key
@@ -126,6 +131,7 @@ def __init__(
126131
verify=verify,
127132
proxies=proxies,
128133
cert=cert,
134+
max_retries=max_retries,
129135
)
130136

131137
self.authorization = Authorization()

src/keycloak/openid_connection.py

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ def __init__(
7373
user_realm_name=None,
7474
timeout=60,
7575
cert=None,
76+
max_retries=1,
7677
):
7778
"""Init method.
7879
@@ -108,6 +109,8 @@ def __init__(
108109
Either a path to an SSL certificate file, or two-tuple of
109110
(certificate file, key file).
110111
:type cert: Union[str,Tuple[str,str]]
112+
:param max_retries: The total number of times to retry HTTP requests.
113+
:type max_retries: int
111114
"""
112115
# token is renewed when it hits 90% of its lifetime. This is to account for any possible
113116
# clock skew.

0 commit comments

Comments
 (0)