*********************************** Iterate over the Pages of a Listing *********************************** A repository may hold thousands of submodels, and a registry thousands of descriptors. If the server answered a listing, such as "all the submodels", in a single response, that response could grow very large: the server would have to put it together in memory, the network would have to carry it, and your program would wait for all of it before it could look at the first item. Hence, the specification splits the listings into *pages*, as a search engine shows you the results ten at a time instead of all at once. Each request gives you one page with a limited number of items. To get further, you ask for the next page, until there are no more pages. The specification marks the place where the next page starts with a *cursor*, a string which the server gives you together with the page. You do not need to understand the cursor, and should not build it yourself; you just hand it back to the server to get the next page. In the client, each page carries the cursor to the next one in its ``paging_metadata``, and the last page has no cursor. Set Up ====== To see the paging at work, we need a listing which spans more than one page. Hence, before the actual examples, we connect to the Submodel Repository and store five submodels in it. Later, we ask for two submodels per page, so the five submodels come in three pages. The repository might already hold other submodels, for example, from your own experiments, which would mix into the listing. To list only ours, we give all five the same semantic ID, and filter the listing by it. This chapter does not depend on the previous ones, so you can run its code on its own: .. code-block:: python import requests import aas_core3_2.types as aas_types from aas_client3_2 import ( http as aas_client_http, pagination as aas_client_pagination, requests_transport as aas_client_requests_transport, submodel_repository as aas_client_submodel_repository, ) server = aas_client_http.Server( transport=aas_client_requests_transport.RequestsTransport( requests.Session() ), base_url="http://localhost:5005", ) semantic_id = aas_types.Reference( type=aas_types.ReferenceTypes.EXTERNAL_REFERENCE, keys=[ aas_types.Key( type=aas_types.KeyTypes.GLOBAL_REFERENCE, value="https://example.com/semantics/getting-started/pagination", ) ], ) for i in range(5): aas_client_submodel_repository.post_submodel( server, aas_types.Submodel( id=f"https://example.com/ids/sm/getting-started/pagination/{i}", semantic_id=semantic_id, ), ) Page by Page ============ Pass ``limit`` to set the size of a page, and the cursor of the previous page to get the next one: .. code-block:: python cursor = None page_number = 1 while True: page = aas_client_submodel_repository.get_all_submodels( server, semantic_id=semantic_id, limit=2, cursor=cursor ) print(f"Page {page_number}:") for submodel in page.over_result_or_empty(): print(f" {submodel.id}") cursor = page.paging_metadata.cursor if cursor is None: break page_number += 1 # Prints: # Page 1: # https://example.com/ids/sm/getting-started/pagination/0 # https://example.com/ids/sm/getting-started/pagination/1 # Page 2: # https://example.com/ids/sm/getting-started/pagination/2 # https://example.com/ids/sm/getting-started/pagination/3 # Page 3: # https://example.com/ids/sm/getting-started/pagination/4 Iterate over the Items ====================== :py:func:`aas_client3_2.pagination.iterate` follows the cursors for you, and yields the items of all the pages. Pass it a function which fetches the page at the given cursor: .. code-block:: python def get_page(cursor): return aas_client_submodel_repository.get_all_submodels( server, semantic_id=semantic_id, limit=2, cursor=cursor ) for submodel in aas_client_pagination.iterate(get_page): print(submodel.id) # Prints: # https://example.com/ids/sm/getting-started/pagination/0 # https://example.com/ids/sm/getting-started/pagination/1 # https://example.com/ids/sm/getting-started/pagination/2 # https://example.com/ids/sm/getting-started/pagination/3 # https://example.com/ids/sm/getting-started/pagination/4 The pages are fetched lazily, one request each, as you iterate. If you need the pages themselves, use :py:func:`aas_client3_2.pagination.iterate_pages`. If a faulty server gives a cursor for the second time, the iteration would never end; :py:func:`~aas_client3_2.pagination.iterate` raises :py:class:`~aas_client3_2.pagination.RepeatedCursor` instead. Clean Up ======== Delete what we created in this chapter, so that you can run the code of this chapter again. The identifiers are unique, so the server would refuse to create the submodels a second time: .. code-block:: python aas_client_submodel_repository.delete_submodel_by_id( server, "https://example.com/ids/sm/getting-started/pagination/0", ) aas_client_submodel_repository.delete_submodel_by_id( server, "https://example.com/ids/sm/getting-started/pagination/1", ) aas_client_submodel_repository.delete_submodel_by_id( server, "https://example.com/ids/sm/getting-started/pagination/2", ) aas_client_submodel_repository.delete_submodel_by_id( server, "https://example.com/ids/sm/getting-started/pagination/3", ) aas_client_submodel_repository.delete_submodel_by_id( server, "https://example.com/ids/sm/getting-started/pagination/4", )