python-keycloak
python-keycloak copied to clipboard
add support for paging in get_users
The current implementation only supports retrieving all users, even though you have optional first and max parameters that are essentially ignored. My keycloak has > 50,000 users and I can't block as the call returns all users. My changes follow:
#
def __fetch_page(self, url, query):
''' NEW: Wrapper function for *real* paginated GET requests
'''
results = []
# initalize query if it was called with None
if not query:
return results
results = raise_error_from_response(
self.raw_get(url, **query),
KeycloakGetError)
return results
def get_users(self, query=None):
"""
Return a list of users, filtered according to query parameters
UserRepresentation
https://www.keycloak.org/docs-api/8.0/rest-api/index.html#_userrepresentation
:param query: Query parameters (optional)
:return: users list
"""
params_path = {"realm-name": self.realm_name}
if query != None and 'max' in query:
return self.__fetch_page(URL_ADMIN_USERS.format(**params_path), query)
else:
return self.__fetch_all(URL_ADMIN_USERS.format(**params_path), query)
How about using the current mechanism to fetch pages of 100 and return them using a generator. This way data is only retrieved when needed and no max needs to be given.
This is fixed