Skip to content

Commit

Permalink
feat: Add the max_retries parameter (#598)
Browse files Browse the repository at this point in the history
This is passed to requests.adapters.HTTPAdapter to allow for more retries
  • Loading branch information
roguh authored Oct 4, 2024
1 parent 2d3aa5d commit 05c2e7a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/keycloak/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ class ConnectionManager(object):
Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file).
:type cert: Union[str,Tuple[str,str]]
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
"""

def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None, cert=None):
def __init__(
self, base_url, headers={}, timeout=60, verify=True, proxies=None, cert=None, max_retries=1
):
"""Init method.
:param base_url: The server URL.
Expand All @@ -73,6 +77,8 @@ def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None,
Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file).
:type cert: Union[str,Tuple[str,str]]
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
"""
self.base_url = base_url
self.headers = headers
Expand All @@ -85,7 +91,7 @@ def __init__(self, base_url, headers={}, timeout=60, verify=True, proxies=None,
# retry once to reset connection with Keycloak after tomcat's ConnectionTimeout
# see https://github.com/marcospereirampj/python-keycloak/issues/36
for protocol in ("https://", "http://"):
adapter = HTTPAdapter(max_retries=1)
adapter = HTTPAdapter(max_retries=max_retries)
# adds POST to retry whitelist
allowed_methods = set(adapter.max_retries.allowed_methods)
allowed_methods.add("POST")
Expand Down
6 changes: 6 additions & 0 deletions src/keycloak/keycloak_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class KeycloakAdmin:
Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file).
:type cert: Union[str,Tuple[str,str]]
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
:param connection: A KeycloakOpenIDConnection as an alternative to individual params.
:type connection: KeycloakOpenIDConnection
"""
Expand All @@ -99,6 +101,7 @@ def __init__(
user_realm_name=None,
timeout=60,
cert=None,
max_retries=1,
connection: Optional[KeycloakOpenIDConnection] = None,
):
"""Init method.
Expand Down Expand Up @@ -134,6 +137,8 @@ def __init__(
:param cert: An SSL certificate used by the requested host to authenticate the client.
Either a path to an SSL certificate file, or two-tuple of (certificate file, key file).
:type cert: Union[str,Tuple[str,str]]
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
:param connection: An OpenID Connection as an alternative to individual params.
:type connection: KeycloakOpenIDConnection
"""
Expand All @@ -152,6 +157,7 @@ def __init__(
custom_headers=custom_headers,
timeout=timeout,
cert=cert,
max_retries=max_retries,
)

@property
Expand Down
6 changes: 6 additions & 0 deletions src/keycloak/keycloak_openid.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class KeycloakOpenID:
:param cert: An SSL certificate used by the requested host to authenticate the client.
Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file).
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
"""

def __init__(
Expand All @@ -90,6 +92,7 @@ def __init__(
proxies=None,
timeout=60,
cert=None,
max_retries=1,
):
"""Init method.
Expand All @@ -114,6 +117,8 @@ def __init__(
Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file).
:type cert: Union[str,Tuple[str,str]]
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
"""
self.client_id = client_id
self.client_secret_key = client_secret_key
Expand All @@ -126,6 +131,7 @@ def __init__(
verify=verify,
proxies=proxies,
cert=cert,
max_retries=max_retries,
)

self.authorization = Authorization()
Expand Down
3 changes: 3 additions & 0 deletions src/keycloak/openid_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def __init__(
user_realm_name=None,
timeout=60,
cert=None,
max_retries=1,
):
"""Init method.
Expand Down Expand Up @@ -108,6 +109,8 @@ def __init__(
Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file).
:type cert: Union[str,Tuple[str,str]]
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
"""
# token is renewed when it hits 90% of its lifetime. This is to account for any possible
# clock skew.
Expand Down

0 comments on commit 05c2e7a

Please sign in to comment.