From c767c21177cf4b68a98b5c7fda259772c43c4072 Mon Sep 17 00:00:00 2001 From: Akos Hencz Date: Tue, 30 Jul 2019 17:22:33 +0200 Subject: [PATCH] Add pagination wrapper --- keycloak/keycloak_admin.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/keycloak/keycloak_admin.py b/keycloak/keycloak_admin.py index 4e2b398..300554f 100644 --- a/keycloak/keycloak_admin.py +++ b/keycloak/keycloak_admin.py @@ -42,6 +42,8 @@ from .urls_patterns import URL_ADMIN_SERVER_INFO, URL_ADMIN_CLIENT_AUTHZ_RESOURC class KeycloakAdmin: + PAGE_SIZE = 100 + def __init__(self, server_url, username, password, realm_name='master', client_id='admin-cli', verify=True, client_secret_key=None): """ @@ -120,6 +122,35 @@ class KeycloakAdmin: def token(self, value): self._token = value + + def __fetch_all(self, url, query=None): + '''Wrapper function to paginate GET requests + + :param url: The url on which the query is executed + :param query: Existing query parameters (optional) + + :return: Combined results of paginated queries + ''' + results = [] + + # initalize query if it was called with None + if not query: + query = {} + page = 0 + query['max'] = self.PAGE_SIZE + + # fetch until we can + while True: + query['first'] = page*self.PAGE_SIZE + partial_results = raise_error_from_response( + self.connection.raw_get(url, **query), + KeycloakGetError) + if not partial_results: + break + results.extend(partial_results) + page += 1 + return results + def import_realm(self, payload): """ Import a new realm from a RealmRepresentation. Realm name must be unique.