Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HP-2247 | feat(keycloak): add timeout to API calls #471

Merged
merged 1 commit into from
Feb 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions utils/keycloak.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def __init__(self, server_url, realm_name, client_id, client_secret):

self._session = requests.Session()
self._auth = None
self._timeout = 10

def _handle_request_common_errors(self, requester):
try:
Expand All @@ -52,7 +53,7 @@ def _well_known(self):
well_known_url = f"{self._server_url}/realms/{self._realm_name}/.well-known/openid-configuration"

result = self._handle_request_common_errors(
lambda: self._session.get(well_known_url)
lambda: self._session.get(well_known_url, timeout=self._timeout)
)

if not result.ok:
Expand All @@ -73,7 +74,9 @@ def _get_auth(self, force_renew=False):
}

result = self._handle_request_common_errors(
lambda: self._session.post(token_endpoint_url, data=credentials_request)
lambda: self._session.post(
token_endpoint_url, data=credentials_request, timeout=self._timeout
)
)

if not result.ok:
Expand Down Expand Up @@ -118,7 +121,7 @@ def get_user(self, user_id):
url = self._single_user_url(user_id)

response = self._handle_user_request(
lambda auth: self._session.get(url, auth=auth)
lambda auth: self._session.get(url, auth=auth, timeout=self._timeout)
)

return response.json()
Expand All @@ -127,20 +130,27 @@ def update_user(self, user_id, update_data: dict):
url = self._single_user_url(user_id)

self._handle_user_request(
lambda auth: self._session.put(url, auth=auth, json=update_data)
lambda auth: self._session.put(
url, auth=auth, json=update_data, timeout=self._timeout
)
)

def delete_user(self, user_id):
url = self._single_user_url(user_id)

self._handle_user_request(lambda auth: self._session.delete(url, auth=auth))
self._handle_user_request(
lambda auth: self._session.delete(url, auth=auth, timeout=self._timeout)
)

def send_verify_email(self, user_id):
url = self._single_user_url(user_id)
url += "/send-verify-email"

return self._handle_user_request(
lambda auth: self._session.put(
url, auth=auth, params={"client_id": self._client_id}
url,
auth=auth,
timeout=self._timeout,
params={"client_id": self._client_id},
)
)