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 for stac-fastapi 4.0 #189

Merged
merged 7 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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: 0 additions & 1 deletion .github/workflows/cicd.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ jobs:
- {python: '3.11', pypgstac: '0.8.*'}
- {python: '3.10', pypgstac: '0.8.*'}
- {python: '3.9', pypgstac: '0.8.*'}
- {python: '3.8', pypgstac: '0.8.*'}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot have python 3.13 yet because we don't have pypgstac wheels for it I think


timeout-minutes: 20

Expand Down
15 changes: 13 additions & 2 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@

## [Unreleased]

- Fix Docker compose file, so example data can be loaded into database (author @zstatmanweil, <https://github.com/stac-utils/stac-fastapi-pgstac/pull/142>)
### Changed

- Handle `next` and `dev` tokens now returned as links from pgstac>=0.9.0 (author @zstatmanweil, https://github.com/stac-utils/stac-fastapi-pgstac/pull/140)
- Add collection search extension ([#139](https://github.com/stac-utils/stac-fastapi-pgstac/pull/139))
- keep `/search` and `/collections` extensions separate ([#158](https://github.com/stac-utils/stac-fastapi-pgstac/pull/158))
- Fix `filter` extension implementation in `CoreCrudClient`
- update `pypgstac` requirement to `>=0.8,<0.10`
- set `pypgstac==0.9.*` for test requirements
- update `stac-fastapi-*` requirement to `~=4.0`
- remove `python 3.8` support
- renamed `post_request_model` attribute to `pgstac_search_model` in `CoreCrudClient` class
- changed `datetime` input type to `sting` in GET endpoint methods
- renamed `filter` to `filter_expr` input attributes in GET endpoint methods

### Fixed

- Fix Docker compose file, so example data can be loaded into database (author @zstatmanweil, <https://github.com/stac-utils/stac-fastapi-pgstac/pull/142>)
- Fix `filter` extension implementation in `CoreCrudClient`

## [3.0.1] - 2024-11-14

- Enable runtime `CORS` configuration using environment variables (`CORS_ORIGIN="https://...,https://..."`, `CORS_METHODS="PUT,OPTIONS"`) (https://github.com/stac-utils/stac-fastapi-pgstac/pull/168)

## [3.0.0] - 2024-08-02
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG PYTHON_VERSION=3.11
ARG PYTHON_VERSION=3.12

FROM python:${PYTHON_VERSION}-slim as base

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.docs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM python:3.8-slim
FROM python:3.10-slim

# build-essential is required to build a wheel for ciso8601
RUN apt update && apt install -y build-essential
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile.tests
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG PYTHON_VERSION=3.11
ARG PYTHON_VERSION=3.12

FROM python:${PYTHON_VERSION}-slim as base

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ PgSTAC stores all collection and item records as jsonb fields exactly as they co
| --| --|
| 2.5 | >=0.7,<0.8 |
| 3.0 | >=0.8,<0.9 |
| 4.0 | >=0.8,<0.10 |

## Usage

Expand Down
13 changes: 8 additions & 5 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.3",
"stac-fastapi.extensions~=3.0.3",
"stac-fastapi.types~=3.0.3",
"stac-fastapi.api~=4.0",
"stac-fastapi.extensions~=4.0",
"stac-fastapi.types~=4.0",
"asyncpg",
"buildpg",
"brotli_asgi",
Expand Down Expand Up @@ -46,12 +46,15 @@
description="An implementation of STAC API based on the FastAPI framework and using the pgstac backend.",
long_description=desc,
long_description_content_type="text/markdown",
python_requires=">=3.8",
python_requires=">=3.9",
classifiers=[
"Intended Audience :: Developers",
"Intended Audience :: Information Technology",
"Intended Audience :: Science/Research",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"License :: OSI Approved :: MIT License",
],
keywords="STAC FastAPI COG",
Expand Down
4 changes: 2 additions & 2 deletions stac_fastapi/pgstac/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ async def lifespan(app: FastAPI):
openapi_url=settings.openapi_url,
docs_url=settings.docs_url,
redoc_url=None,
root_path=getattr(settings, "root_path", None),
root_path=settings.root_path,
lifespan=lifespan,
)

Expand All @@ -128,7 +128,7 @@ async def lifespan(app: FastAPI):
extensions=extensions + [collection_search_extension]
if collection_search_extension
else extensions,
client=CoreCrudClient(post_request_model=post_request_model), # type: ignore
client=CoreCrudClient(pgstac_search_model=post_request_model),
response_class=ORJSONResponse,
items_get_request_model=items_get_request_model,
search_get_request_model=get_request_model,
Expand Down
40 changes: 18 additions & 22 deletions stac_fastapi/pgstac/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import json
import re
from typing import Any, Dict, List, Optional, Set, Union
from typing import Any, Dict, List, Optional, Set, Type, Union
from urllib.parse import unquote_plus, urljoin

import attr
Expand All @@ -18,7 +18,6 @@
from stac_fastapi.types.core import AsyncBaseCoreClient, Relations
from stac_fastapi.types.errors import InvalidQueryParameter, NotFoundError
from stac_fastapi.types.requests import get_base_url
from stac_fastapi.types.rfc3339 import DateTimeType
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
from stac_pydantic.shared import BBox, MimeTypes

Expand All @@ -31,7 +30,7 @@
PagingLinks,
)
from stac_fastapi.pgstac.types.search import PgstacSearch
from stac_fastapi.pgstac.utils import filter_fields, format_datetime_range
from stac_fastapi.pgstac.utils import filter_fields

NumType = Union[float, int]

Expand All @@ -40,18 +39,20 @@
class CoreCrudClient(AsyncBaseCoreClient):
"""Client for core endpoints defined by stac."""

pgstac_search_model: Type[PgstacSearch] = attr.ib(default=PgstacSearch)

async def all_collections( # noqa: C901
self,
request: Request,
# Extensions
bbox: Optional[BBox] = None,
datetime: Optional[DateTimeType] = None,
datetime: Optional[str] = None,
limit: Optional[int] = None,
offset: Optional[int] = None,
query: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
filter: Optional[str] = None,
filter_expr: Optional[str] = None,
filter_lang: Optional[str] = None,
**kwargs,
) -> Collections:
Expand Down Expand Up @@ -82,7 +83,7 @@ async def all_collections( # noqa: C901
datetime=datetime,
fields=fields,
sortby=sortby,
filter_query=filter,
filter_query=filter_expr,
filter_lang=filter_lang,
)

Expand Down Expand Up @@ -234,9 +235,6 @@ async def _search_base( # noqa: C901

settings: Settings = request.app.state.settings

if search_request.datetime:
search_request.datetime = format_datetime_range(search_request.datetime)

search_request.conf = search_request.conf or {}
search_request.conf["nohydrate"] = settings.use_api_hydrate

Expand Down Expand Up @@ -340,7 +338,7 @@ async def item_collection(
collection_id: str,
request: Request,
bbox: Optional[BBox] = None,
datetime: Optional[DateTimeType] = None,
datetime: Optional[str] = None,
limit: Optional[int] = None,
token: Optional[str] = None,
**kwargs,
Expand All @@ -360,9 +358,6 @@ async def item_collection(
# If collection does not exist, NotFoundError wil be raised
await self.get_collection(collection_id, request=request)

if datetime:
datetime = format_datetime_range(datetime)

base_args = {
"collections": [collection_id],
"bbox": bbox,
Expand All @@ -373,7 +368,7 @@ async def item_collection(

if self.extension_is_enabled("FilterExtension"):
filter_lang = kwargs.get("filter_lang", None)
filter_query = kwargs.get("filter", None)
filter_query = kwargs.get("filter_expr", None)
if filter_query:
if filter_lang == "cql2-text":
filter_query = to_cql2(parse_cql2_text(filter_query))
Expand All @@ -387,7 +382,7 @@ async def item_collection(
if v is not None and v != []:
clean[k] = v

search_request = self.post_request_model(**clean)
search_request = self.pgstac_search_model(**clean)
item_collection = await self._search_base(search_request, request=request)

links = await ItemCollectionLinks(
Expand All @@ -414,7 +409,7 @@ async def get_item(
# If collection does not exist, NotFoundError wil be raised
await self.get_collection(collection_id, request=request)

search_request = self.post_request_model(
search_request = self.pgstac_search_model(
ids=[item_id], collections=[collection_id], limit=1
)
item_collection = await self._search_base(search_request, request=request)
Expand Down Expand Up @@ -456,14 +451,14 @@ async def get_search(
ids: Optional[List[str]] = None,
bbox: Optional[BBox] = None,
intersects: Optional[str] = None,
datetime: Optional[DateTimeType] = None,
datetime: Optional[str] = None,
limit: Optional[int] = None,
# Extensions
query: Optional[str] = None,
token: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
filter: Optional[str] = None,
filter_expr: Optional[str] = None,
filter_lang: Optional[str] = None,
**kwargs,
) -> ItemCollection:
Expand All @@ -490,13 +485,13 @@ async def get_search(
datetime=datetime,
fields=fields,
sortby=sortby,
filter_query=filter,
filter_query=filter_expr,
filter_lang=filter_lang,
)

# Do the request
try:
search_request = self.post_request_model(**clean)
search_request = self.pgstac_search_model(**clean)
except ValidationError as e:
raise HTTPException(
status_code=400, detail=f"Invalid parameters provided {e}"
Expand All @@ -508,7 +503,7 @@ def _clean_search_args( # noqa: C901
self,
base_args: Dict[str, Any],
intersects: Optional[str] = None,
datetime: Optional[DateTimeType] = None,
datetime: Optional[str] = None,
fields: Optional[List[str]] = None,
sortby: Optional[str] = None,
filter_query: Optional[str] = None,
Expand All @@ -524,7 +519,7 @@ def _clean_search_args( # noqa: C901
base_args["filter_lang"] = filter_lang

if datetime:
base_args["datetime"] = format_datetime_range(datetime)
base_args["datetime"] = datetime

if intersects:
base_args["intersects"] = orjson.loads(unquote_plus(intersects))
Expand Down Expand Up @@ -553,6 +548,7 @@ def _clean_search_args( # noqa: C901
includes.add(field[1:])
else:
includes.add(field)

base_args["fields"] = {"include": includes, "exclude": excludes}

# Remove None values from dict
Expand Down
4 changes: 2 additions & 2 deletions tests/api/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ async def get_collection(
)

api = StacApi(
client=Client(post_request_model=post_request_model),
client=Client(pgstac_search_model=post_request_model),
settings=settings,
extensions=extensions,
search_post_request_model=post_request_model,
Expand Down Expand Up @@ -794,7 +794,7 @@ async def test_no_extension(
extensions = []
post_request_model = create_post_request_model(extensions, base_model=PgstacSearch)
api = StacApi(
client=CoreCrudClient(post_request_model=post_request_model),
client=CoreCrudClient(pgstac_search_model=post_request_model),
settings=settings,
extensions=extensions,
search_post_request_model=post_request_model,
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def api_client(request, database):
api = StacApi(
settings=api_settings,
extensions=extensions + [collection_search_extension],
client=CoreCrudClient(post_request_model=search_post_request_model),
client=CoreCrudClient(pgstac_search_model=search_post_request_model),
items_get_request_model=items_get_request_model,
search_get_request_model=search_get_request_model,
search_post_request_model=search_post_request_model,
Expand Down
Loading