Browse Source

feat: Add the max_retries parameter (#598)

This is passed to requests.adapters.HTTPAdapter to allow for more retries
pull/599/head v4.6.0
Hugo Rivera Calzadillas 4 weeks ago
committed by GitHub
parent
commit
05c2e7a508
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 10
      src/keycloak/connection.py
  2. 6
      src/keycloak/keycloak_admin.py
  3. 6
      src/keycloak/keycloak_openid.py
  4. 3
      src/keycloak/openid_connection.py

10
src/keycloak/connection.py

@ -53,9 +53,13 @@ class ConnectionManager(object):
Either a path to an SSL certificate file, or two-tuple of Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file). (certificate file, key file).
:type cert: Union[str,Tuple[str,str]] :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. """Init method.
:param base_url: The server URL. :param base_url: The server URL.
@ -73,6 +77,8 @@ class ConnectionManager(object):
Either a path to an SSL certificate file, or two-tuple of Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file). (certificate file, key file).
:type cert: Union[str,Tuple[str,str]] :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.base_url = base_url
self.headers = headers self.headers = headers
@ -85,7 +91,7 @@ class ConnectionManager(object):
# retry once to reset connection with Keycloak after tomcat's ConnectionTimeout # retry once to reset connection with Keycloak after tomcat's ConnectionTimeout
# see https://github.com/marcospereirampj/python-keycloak/issues/36 # see https://github.com/marcospereirampj/python-keycloak/issues/36
for protocol in ("https://", "http://"): for protocol in ("https://", "http://"):
adapter = HTTPAdapter(max_retries=1)
adapter = HTTPAdapter(max_retries=max_retries)
# adds POST to retry whitelist # adds POST to retry whitelist
allowed_methods = set(adapter.max_retries.allowed_methods) allowed_methods = set(adapter.max_retries.allowed_methods)
allowed_methods.add("POST") allowed_methods.add("POST")

6
src/keycloak/keycloak_admin.py

@ -77,6 +77,8 @@ class KeycloakAdmin:
Either a path to an SSL certificate file, or two-tuple of Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file). (certificate file, key file).
:type cert: Union[str,Tuple[str,str]] :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. :param connection: A KeycloakOpenIDConnection as an alternative to individual params.
:type connection: KeycloakOpenIDConnection :type connection: KeycloakOpenIDConnection
""" """
@ -99,6 +101,7 @@ class KeycloakAdmin:
user_realm_name=None, user_realm_name=None,
timeout=60, timeout=60,
cert=None, cert=None,
max_retries=1,
connection: Optional[KeycloakOpenIDConnection] = None, connection: Optional[KeycloakOpenIDConnection] = None,
): ):
"""Init method. """Init method.
@ -134,6 +137,8 @@ class KeycloakAdmin:
:param cert: An SSL certificate used by the requested host to authenticate the client. :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). Either a path to an SSL certificate file, or two-tuple of (certificate file, key file).
:type cert: Union[str,Tuple[str,str]] :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. :param connection: An OpenID Connection as an alternative to individual params.
:type connection: KeycloakOpenIDConnection :type connection: KeycloakOpenIDConnection
""" """
@ -152,6 +157,7 @@ class KeycloakAdmin:
custom_headers=custom_headers, custom_headers=custom_headers,
timeout=timeout, timeout=timeout,
cert=cert, cert=cert,
max_retries=max_retries,
) )
@property @property

6
src/keycloak/keycloak_openid.py

@ -77,6 +77,8 @@ class KeycloakOpenID:
:param cert: An SSL certificate used by the requested host to authenticate the client. :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 Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file). (certificate file, key file).
:param max_retries: The total number of times to retry HTTP requests.
:type max_retries: int
""" """
def __init__( def __init__(
@ -90,6 +92,7 @@ class KeycloakOpenID:
proxies=None, proxies=None,
timeout=60, timeout=60,
cert=None, cert=None,
max_retries=1,
): ):
"""Init method. """Init method.
@ -114,6 +117,8 @@ class KeycloakOpenID:
Either a path to an SSL certificate file, or two-tuple of Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file). (certificate file, key file).
:type cert: Union[str,Tuple[str,str]] :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_id = client_id
self.client_secret_key = client_secret_key self.client_secret_key = client_secret_key
@ -126,6 +131,7 @@ class KeycloakOpenID:
verify=verify, verify=verify,
proxies=proxies, proxies=proxies,
cert=cert, cert=cert,
max_retries=max_retries,
) )
self.authorization = Authorization() self.authorization = Authorization()

3
src/keycloak/openid_connection.py

@ -73,6 +73,7 @@ class KeycloakOpenIDConnection(ConnectionManager):
user_realm_name=None, user_realm_name=None,
timeout=60, timeout=60,
cert=None, cert=None,
max_retries=1,
): ):
"""Init method. """Init method.
@ -108,6 +109,8 @@ class KeycloakOpenIDConnection(ConnectionManager):
Either a path to an SSL certificate file, or two-tuple of Either a path to an SSL certificate file, or two-tuple of
(certificate file, key file). (certificate file, key file).
:type cert: Union[str,Tuple[str,str]] :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 # token is renewed when it hits 90% of its lifetime. This is to account for any possible
# clock skew. # clock skew.

Loading…
Cancel
Save