Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update pagination for collection-search #155

Merged
merged 16 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jobs:
- {python: '3.12', pypgstac: '0.9.*'}
- {python: '3.12', pypgstac: '0.8.*'}
- {python: '3.11', pypgstac: '0.8.*'}
- {python: '3.10', pypgstac: '0.8.*'}
- {python: '3.9', pypgstac: '0.8.*'}
- {python: '3.8', pypgstac: '0.8.*'}

Expand Down
4 changes: 3 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ services:
build:
context: .
dockerfile: Dockerfile.tests
volumes:
- .:/app
environment:
- ENVIRONMENT=local
- DB_MIN_CONN_SIZE=1
Expand All @@ -44,7 +46,7 @@ services:

database:
container_name: stac-db
image: ghcr.io/stac-utils/pgstac:v0.9.1
image: ghcr.io/stac-utils/pgstac:v0.9.2
environment:
- POSTGRES_USER=username
- POSTGRES_PASSWORD=password
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
"orjson",
"pydantic",
"stac_pydantic==3.1.*",
"stac-fastapi.api~=3.0.2",
"stac-fastapi.extensions~=3.0.2",
"stac-fastapi.types~=3.0.2",
"stac-fastapi.api~=3.0.3",
"stac-fastapi.extensions~=3.0.3",
"stac-fastapi.types~=3.0.3",
"asyncpg",
"buildpg",
"brotli_asgi",
Expand Down
4 changes: 4 additions & 0 deletions stac_fastapi/pgstac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from stac_fastapi.extensions.core import (
FieldsExtension,
FilterExtension,
OffsetPaginationExtension,
SortExtension,
TokenPaginationExtension,
TransactionExtension,
Expand Down Expand Up @@ -58,6 +59,9 @@
"sort": SortExtension(),
"fields": FieldsExtension(),
"filter": FilterExtension(client=FiltersClient()),
# NOTE: there is no conformance class for the Pagination extension
# so `CollectionSearchExtension.from_extensions` will raise a warning
"pagination": OffsetPaginationExtension(),
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
}

enabled_extensions = (
Expand Down
28 changes: 18 additions & 10 deletions stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from stac_fastapi.pgstac.config import Settings
from stac_fastapi.pgstac.models.links import (
CollectionLinks,
CollectionSearchPagingLinks,
ItemCollectionLinks,
ItemLinks,
PagingLinks,
Expand All @@ -46,8 +47,8 @@ async def all_collections( # noqa: C901
bbox: Optional[BBox] = None,
datetime: Optional[DateTimeType] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
query: Optional[str] = None,
token: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
filter: Optional[str] = None,
Expand All @@ -68,7 +69,7 @@ async def all_collections( # noqa: C901
base_args = {
"bbox": bbox,
"limit": limit,
"token": token,
"offset": offset,
"query": orjson.loads(unquote_plus(query)) if query else query,
}

Expand All @@ -90,12 +91,16 @@ async def all_collections( # noqa: C901
)
collections_result: Collections = await conn.fetchval(q, *p)

next: Optional[str] = None
prev: Optional[str] = None

next_link: Optional[Dict[str, Any]] = None
prev_link: Optional[Dict[str, Any]] = None
if links := collections_result.get("links"):
next = collections_result["links"].pop("next")
prev = collections_result["links"].pop("prev")
next_link = None
prev_link = None
for link in links:
if link["rel"] == "next":
next_link = link
elif link["rel"] == "prev":
prev_link = link

linked_collections: List[Collection] = []
collections = collections_result["collections"]
Expand All @@ -120,10 +125,13 @@ async def all_collections( # noqa: C901

linked_collections.append(coll)

links = await PagingLinks(
if not collections:
next_link = None
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved

links = await CollectionSearchPagingLinks(
request=request,
next=next,
prev=prev,
next=next_link,
prev=prev_link,
).get_links()

return Collections(
Expand Down
65 changes: 65 additions & 0 deletions stac_fastapi/pgstac/models/links.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,71 @@ def link_prev(self) -> Optional[Dict[str, Any]]:
return None


@attr.s
class CollectionSearchPagingLinks(BaseLinks):
next: Optional[Dict[str, Any]] = attr.ib(kw_only=True, default=None)
prev: Optional[Dict[str, Any]] = attr.ib(kw_only=True, default=None)

def link_next(self) -> Optional[Dict[str, Any]]:
"""Create link for next page."""
if self.next is not None:
method = self.request.method
if method == "GET":
# if offset is equal to default value (0), drop it
if self.next["body"].get("offset", -1) == 0:
_ = self.next["body"].pop("offset")

href = merge_params(self.url, self.next["body"])

# if next link is equal to this link, skip it
if href == self.url:
return None

return {
"rel": Relations.next.value,
"type": MimeTypes.geojson.value,
"method": method,
"href": href,
}

return None

def link_prev(self):
if self.prev is not None:
method = self.request.method
if method == "GET":
u = urlparse(self.url)
params = parse_qs(u.query)
params.update(self.prev["body"])

# if offset is equal to default value (0), drop it
if params.get("offset", -1) == 0:
_ = params.pop("offset")

param_string = unquote(urlencode(params, True))
href = ParseResult(
scheme=u.scheme,
netloc=u.netloc,
path=u.path,
params=u.params,
query=param_string,
fragment=u.fragment,
).geturl()

# if prev link is equal to this link, skip it
if href == self.url:
return None

return {
"rel": Relations.previous.value,
"type": MimeTypes.geojson.value,
"method": method,
"href": href,
}

return None


@attr.s
class CollectionLinksBase(BaseLinks):
"""Create inferred links specific to collections."""
Expand Down
16 changes: 13 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from fastapi import APIRouter
from fastapi.responses import ORJSONResponse
from httpx import ASGITransport, AsyncClient
from pypgstac import __version__ as pgstac_version
from pypgstac.db import PgstacDB
from pypgstac.migrate import Migrate
from pytest_postgresql.janitor import DatabaseJanitor
Expand All @@ -26,6 +27,7 @@
CollectionSearchExtension,
FieldsExtension,
FilterExtension,
OffsetPaginationExtension,
SortExtension,
TokenPaginationExtension,
TransactionExtension,
Expand All @@ -47,6 +49,12 @@
logger = logging.getLogger(__name__)


requires_pgstac_0_9_2 = pytest.mark.skipif(
tuple(map(int, pgstac_version.split("."))) < (0, 9, 2),
reason="at least PgSTAC>0.9.2 required",
vincentsarago marked this conversation as resolved.
Show resolved Hide resolved
)


@pytest.fixture(scope="session")
def event_loop():
return asyncio.get_event_loop()
Expand Down Expand Up @@ -140,10 +148,12 @@ def api_client(request, database):
SortExtension(),
FieldsExtension(),
FilterExtension(client=FiltersClient()),
OffsetPaginationExtension(),
]
collection_search_extension = CollectionSearchExtension.from_extensions(
collection_extensions
)
with pytest.warns(UserWarning):
collection_search_extension = CollectionSearchExtension.from_extensions(
collection_extensions
)

items_get_request_model = create_request_model(
model_name="ItemCollectionUri",
Expand Down
Loading
Loading