44from datetime import datetime as datetime_type
55from datetime import timezone
66from typing import Any , Dict , List , Optional , Set , Type , Union
7- from urllib . parse import urljoin
7+ from base64 import urlsafe_b64encode
88
99import attr
1010import stac_pydantic
1111from fastapi import HTTPException
1212from overrides import overrides
1313from pydantic import ValidationError
14+ from starlette .requests import Request
1415from stac_pydantic .links import Relations
1516from stac_pydantic .shared import MimeTypes
16- from starlette . requests import Request
17+ from urllib . parse import urljoin
1718
1819from stac_fastapi .elasticsearch import serializers
1920from stac_fastapi .elasticsearch .config import ElasticsearchSettings
@@ -70,7 +71,12 @@ class CoreClient(AsyncBaseCoreClient):
7071 database = DatabaseLogic ()
7172
7273 @overrides
73- async def all_collections (self , ** kwargs ) -> Collections :
74+ async def all_collections (
75+ self ,
76+ limit : Optional [int ] = 10 ,
77+ token : Optional [str ] = None ,
78+ ** kwargs
79+ ) -> Collections :
7480 """Read all collections from the database.
7581
7682 Returns:
@@ -80,30 +86,49 @@ async def all_collections(self, **kwargs) -> Collections:
8086 Raises:
8187 Exception: If any error occurs while reading the collections from the database.
8288 """
89+ request : Request = kwargs ["request" ]
8390 base_url = str (kwargs ["request" ].base_url )
8491
92+ hits = self .database .get_all_collections (limit = limit , token = token )
93+
94+ next_search_after = None
95+ next_link = None
96+ if len (hits ) == limit :
97+ last_hit = hits [- 1 ]
98+ next_search_after = last_hit ['sort' ]
99+ next_token = urlsafe_b64encode (',' .join (map (str , next_search_after )).encode ()).decode ()
100+ paging_links = PagingLinks (
101+ next = next_token , request = request
102+ )
103+ next_link = paging_links .link_next ()
104+
105+ links = [
106+ {
107+ "rel" : Relations .root .value ,
108+ "type" : MimeTypes .json ,
109+ "href" : base_url ,
110+ },
111+ {
112+ "rel" : Relations .parent .value ,
113+ "type" : MimeTypes .json ,
114+ "href" : base_url ,
115+ },
116+ {
117+ "rel" : Relations .self .value ,
118+ "type" : MimeTypes .json ,
119+ "href" : urljoin (base_url , "collections" ),
120+ }
121+ ]
122+
123+ if next_link :
124+ links .append (next_link )
125+
85126 return Collections (
86127 collections = [
87- self .collection_serializer .db_to_stac (c , base_url = base_url )
88- for c in await self .database .get_all_collections ()
89- ],
90- links = [
91- {
92- "rel" : Relations .root .value ,
93- "type" : MimeTypes .json ,
94- "href" : base_url ,
95- },
96- {
97- "rel" : Relations .parent .value ,
98- "type" : MimeTypes .json ,
99- "href" : base_url ,
100- },
101- {
102- "rel" : Relations .self .value ,
103- "type" : MimeTypes .json ,
104- "href" : urljoin (base_url , "collections" ),
105- },
128+ self .collection_serializer .db_to_stac (c ["_source" ], base_url = base_url )
129+ for c in hits
106130 ],
131+ links = links
107132 )
108133
109134 @overrides
0 commit comments