python-keycloak icon indicating copy to clipboard operation
python-keycloak copied to clipboard

add support for paging in get_users

Open keith-rhodes-0723 opened this issue 4 years ago • 1 comments

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)

keith-rhodes-0723 avatar May 02 '20 13:05 keith-rhodes-0723

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.

double-a avatar Apr 18 '21 15:04 double-a

This is fixed

ryshoooo avatar Nov 13 '23 14:11 ryshoooo