diff --git a/stac_fastapi/core/setup.py b/stac_fastapi/core/setup.py index 4046a1c2..27b17be5 100644 --- a/stac_fastapi/core/setup.py +++ b/stac_fastapi/core/setup.py @@ -1,4 +1,4 @@ -"""stac_fastapi: elasticsearch module.""" +"""stac_fastapi: core elasticsearch/ opensearch module.""" from setuptools import find_namespace_packages, setup @@ -13,31 +13,13 @@ "stac-fastapi.types==2.4.9", "stac-fastapi.api==2.4.9", "stac-fastapi.extensions==2.4.9", - # "elasticsearch[async]==8.11.0", - # "elasticsearch-dsl==8.11.0", "pystac[validation]", - # "uvicorn", "orjson", "overrides", - # "starlette", "geojson-pydantic", "pygeofilter==0.2.1", ] -# extra_reqs = { -# "dev": [ -# "pytest", -# "pytest-cov", -# "pytest-asyncio", -# "pre-commit", -# "requests", -# "ciso8601", -# "httpx", -# ], -# "docs": ["mkdocs", "mkdocs-material", "pdocs"], -# "server": ["uvicorn[standard]==0.19.0"], -# } - setup( name="stac-fastapi.core", description="Core library for the Elasticsearch and Opensearch stac-fastapi backends.", diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/datetime_utils.py b/stac_fastapi/core/stac_fastapi/core/datetime_utils.py similarity index 100% rename from stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/datetime_utils.py rename to stac_fastapi/core/stac_fastapi/core/datetime_utils.py diff --git a/stac_fastapi/core/stac_fastapi/core/serializers.py b/stac_fastapi/core/stac_fastapi/core/serializers.py index 725e8f65..db7353c2 100644 --- a/stac_fastapi/core/stac_fastapi/core/serializers.py +++ b/stac_fastapi/core/stac_fastapi/core/serializers.py @@ -4,7 +4,7 @@ import attr -from stac_fastapi.elasticsearch.datetime_utils import now_to_rfc3339_str +from stac_fastapi.core.datetime_utils import now_to_rfc3339_str from stac_fastapi.types import stac as stac_types from stac_fastapi.types.links import CollectionLinks, ItemLinks, resolve_links diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py index 0dc30d00..c1bc2d3d 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/app.py @@ -7,12 +7,12 @@ EsAsyncBaseFiltersClient, TransactionsClient, ) +from stac_fastapi.core.extensions import QueryExtension from stac_fastapi.elasticsearch.config import ElasticsearchSettings from stac_fastapi.elasticsearch.database_logic import ( DatabaseLogic, create_collection_index, ) -from stac_fastapi.elasticsearch.extensions import QueryExtension from stac_fastapi.elasticsearch.session import Session from stac_fastapi.extensions.core import ( ContextExtension, diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py index ed812c46..933312f2 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py @@ -10,11 +10,11 @@ from elasticsearch import exceptions, helpers # type: ignore from stac_fastapi.core.extensions import filter +from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer from stac_fastapi.elasticsearch.config import AsyncElasticsearchSettings from stac_fastapi.elasticsearch.config import ( ElasticsearchSettings as SyncElasticsearchSettings, ) -from stac_fastapi.elasticsearch.serializers import CollectionSerializer, ItemSerializer from stac_fastapi.types.errors import ConflictError, NotFoundError from stac_fastapi.types.stac import Collection, Item diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py deleted file mode 100644 index 725e8f65..00000000 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/serializers.py +++ /dev/null @@ -1,162 +0,0 @@ -"""Serializers.""" -import abc -from typing import Any - -import attr - -from stac_fastapi.elasticsearch.datetime_utils import now_to_rfc3339_str -from stac_fastapi.types import stac as stac_types -from stac_fastapi.types.links import CollectionLinks, ItemLinks, resolve_links - - -@attr.s -class Serializer(abc.ABC): - """Defines serialization methods between the API and the data model. - - This class is meant to be subclassed and implemented by specific serializers for different STAC objects (e.g. Item, Collection). - """ - - @classmethod - @abc.abstractmethod - def db_to_stac(cls, item: dict, base_url: str) -> Any: - """Transform database model to STAC object. - - Arguments: - item (dict): A dictionary representing the database model. - base_url (str): The base URL of the STAC API. - - Returns: - Any: A STAC object, e.g. an `Item` or `Collection`, representing the input `item`. - """ - ... - - @classmethod - @abc.abstractmethod - def stac_to_db(cls, stac_object: Any, base_url: str) -> dict: - """Transform STAC object to database model. - - Arguments: - stac_object (Any): A STAC object, e.g. an `Item` or `Collection`. - base_url (str): The base URL of the STAC API. - - Returns: - dict: A dictionary representing the database model. - """ - ... - - -class ItemSerializer(Serializer): - """Serialization methods for STAC items.""" - - @classmethod - def stac_to_db(cls, stac_data: stac_types.Item, base_url: str) -> stac_types.Item: - """Transform STAC item to database-ready STAC item. - - Args: - stac_data (stac_types.Item): The STAC item object to be transformed. - base_url (str): The base URL for the STAC API. - - Returns: - stac_types.Item: The database-ready STAC item object. - """ - item_links = ItemLinks( - collection_id=stac_data["collection"], - item_id=stac_data["id"], - base_url=base_url, - ).create_links() - stac_data["links"] = item_links - - now = now_to_rfc3339_str() - if "created" not in stac_data["properties"]: - stac_data["properties"]["created"] = now - stac_data["properties"]["updated"] = now - return stac_data - - @classmethod - def db_to_stac(cls, item: dict, base_url: str) -> stac_types.Item: - """Transform database-ready STAC item to STAC item. - - Args: - item (dict): The database-ready STAC item to be transformed. - base_url (str): The base URL for the STAC API. - - Returns: - stac_types.Item: The STAC item object. - """ - item_id = item["id"] - collection_id = item["collection"] - item_links = ItemLinks( - collection_id=collection_id, item_id=item_id, base_url=base_url - ).create_links() - - original_links = item.get("links", []) - if original_links: - item_links += resolve_links(original_links, base_url) - - return stac_types.Item( - type="Feature", - stac_version=item.get("stac_version", ""), - stac_extensions=item.get("stac_extensions", []), - id=item_id, - collection=item.get("collection", ""), - geometry=item.get("geometry", {}), - bbox=item.get("bbox", []), - properties=item.get("properties", {}), - links=item_links, - assets=item.get("assets", {}), - ) - - -class CollectionSerializer(Serializer): - """Serialization methods for STAC collections.""" - - @classmethod - def db_to_stac(cls, collection: dict, base_url: str) -> stac_types.Collection: - """Transform database model to STAC collection. - - Args: - collection (dict): The collection data in dictionary form, extracted from the database. - base_url (str): The base URL for the collection. - - Returns: - stac_types.Collection: The STAC collection object. - """ - # Use dictionary unpacking to extract values from the collection dictionary - collection_id = collection.get("id") - stac_extensions = collection.get("stac_extensions", []) - stac_version = collection.get("stac_version", "") - title = collection.get("title", "") - description = collection.get("description", "") - keywords = collection.get("keywords", []) - license = collection.get("license", "") - providers = collection.get("providers", {}) - summaries = collection.get("summaries", {}) - extent = collection.get("extent", {}) - collection_assets = collection.get("assets", {}) - - # Create the collection links using CollectionLinks - collection_links = CollectionLinks( - collection_id=collection_id, base_url=base_url - ).create_links() - - # Add any additional links from the collection dictionary - original_links = collection.get("links") - if original_links: - collection_links += resolve_links(original_links, base_url) - - # Return the stac_types.Collection object - return stac_types.Collection( - type="Collection", - id=collection_id, - stac_extensions=stac_extensions, - stac_version=stac_version, - title=title, - description=description, - keywords=keywords, - license=license, - providers=providers, - summaries=summaries, - extent=extent, - links=collection_links, - assets=collection_assets, - ) diff --git a/stac_fastapi/elasticsearch/tests/resources/test_item.py b/stac_fastapi/elasticsearch/tests/resources/test_item.py index c63be048..e62da8b8 100644 --- a/stac_fastapi/elasticsearch/tests/resources/test_item.py +++ b/stac_fastapi/elasticsearch/tests/resources/test_item.py @@ -13,8 +13,8 @@ from pystac.utils import datetime_to_str from stac_fastapi.core.core import CoreClient +from stac_fastapi.core.datetime_utils import now_to_rfc3339_str from stac_fastapi.elasticsearch.database_logic import DatabaseLogic -from stac_fastapi.elasticsearch.datetime_utils import now_to_rfc3339_str from stac_fastapi.types.core import LandingPageMixin from ..conftest import create_item, refresh_indices