Skip to content

Commit 9bc4863

Browse files
committed
Update and add tests.
1 parent c2c5832 commit 9bc4863

File tree

2 files changed

+168
-174
lines changed

2 files changed

+168
-174
lines changed

stac_fastapi/core/stac_fastapi/core/core.py

Lines changed: 37 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
logger = logging.getLogger(__name__)
4343

4444
NumType = Union[float, int]
45+
partialItemValidator = TypeAdapter(stac_types.PartialItem)
46+
partialCollectionValidator = TypeAdapter(stac_types.PartialCollection)
4547

4648

4749
@attr.s
@@ -682,84 +684,35 @@ async def patch_item(
682684
patch: Union[stac_types.PartialItem, List[stac_types.PatchOperation]],
683685
**kwargs,
684686
):
687+
base_url = str(kwargs["request"].base_url)
688+
685689
content_type = kwargs["request"].headers.get("content-type")
690+
691+
item = None
686692
if isinstance(patch, list) and content_type == "application/json-patch+json":
687-
return await self.json_patch_item(
688-
collection_id,
689-
item_id,
690-
patch,
691-
**kwargs,
693+
item = await self.database.json_patch_item(
694+
collection_id=collection_id,
695+
item_id=item_id,
696+
operations=patch,
697+
base_url=base_url,
692698
)
693699

694700
if isinstance(patch, dict) and content_type in [
695701
"application/merge-patch+json",
696702
"application/json",
697703
]:
698-
partialItemValidator = TypeAdapter(stac_types.PartialItem)
699-
700704
patch = partialItemValidator.validate_python(patch)
701-
return await self.merge_patch_item(
702-
collection_id,
703-
item_id,
704-
patch,
705-
**kwargs,
705+
item = await self.database.merge_patch_item(
706+
collection_id=collection_id,
707+
item_id=item_id,
708+
item=patch,
709+
base_url=base_url,
706710
)
707711

708-
raise NotImplementedError("Content-Type and body combination not implemented")
709-
710-
async def merge_patch_item(
711-
self, collection_id: str, item_id: str, item: stac_types.PartialItem, **kwargs
712-
) -> Optional[stac_types.Item]:
713-
"""Patch an item in the collection following RF7396..
712+
if item:
713+
return ItemSerializer.db_to_stac(item, base_url=base_url)
714714

715-
Args:
716-
collection_id (str): The ID of the collection the item belongs to.
717-
item_id (str): The ID of the item to be updated.
718-
item (stac_types.PartialItem): The partial item data.
719-
kwargs: Other optional arguments, including the request object.
720-
721-
Returns:
722-
stac_types.Item: The patched item object.
723-
724-
"""
725-
base_url = str(kwargs["request"].base_url)
726-
727-
item = await self.database.merge_patch_item(
728-
collection_id=collection_id,
729-
item_id=item_id,
730-
item=item,
731-
base_url=base_url,
732-
)
733-
return ItemSerializer.db_to_stac(item, base_url=base_url)
734-
735-
async def json_patch_item(
736-
self,
737-
collection_id: str,
738-
item_id: str,
739-
operations: List[stac_types.PatchOperation],
740-
**kwargs,
741-
) -> Optional[stac_types.Item]:
742-
"""Patch an item in the collection following RF6902.
743-
744-
Args:
745-
collection_id (str): The ID of the collection the item belongs to.
746-
item_id (str): The ID of the item to be updated.
747-
operations (List): List of operations to run on item.
748-
kwargs: Other optional arguments, including the request object.
749-
750-
Returns:
751-
stac_types.Item: The patched item object.
752-
753-
"""
754-
base_url = str(kwargs["request"].base_url)
755-
756-
item = await self.database.json_patch_item(
757-
collection_id=collection_id,
758-
item_id=item_id,
759-
base_url=base_url,
760-
operations=operations,
761-
)
762-
return ItemSerializer.db_to_stac(item, base_url=base_url)
715+
raise NotImplementedError("Content-Type and body combination not implemented")
763716

764717
@overrides
765718
async def delete_item(self, item_id: str, collection_id: str, **kwargs) -> Optional[stac_types.Item]:
@@ -851,76 +804,35 @@ def patch_collection(
851804
The patched collection.
852805
"""
853806
content_type = kwargs["request"].headers.get("content-type")
807+
base_url = str(kwargs["request"].base_url)
854808

809+
collection = None
855810
if isinstance(patch, list) and content_type == "application/json-patch+json":
856-
return self.json_patch_collection(collection_id, patch, **kwargs)
811+
collection = self.database.json_patch_collection(
812+
collection_id=collection_id,
813+
operations=patch,
814+
base_url=base_url,
815+
)
857816

858817
if isinstance(patch, dict) and content_type in [
859818
"application/merge-patch+json",
860819
"application/json",
861820
]:
862-
partialCollectionValidator = TypeAdapter(stac_types.PartialCollection)
863-
864821
patch = partialCollectionValidator.validate_python(patch)
865-
return self.merge_patch_collection(
866-
collection_id,
867-
patch,
868-
**kwargs,
822+
collection = self.database.merge_patch_collection(
823+
collection_id=collection_id,
824+
collection=patch,
825+
base_url=base_url,
869826
)
870827

871-
raise NotImplementedError("Content-Type and body combination not implemented")
872-
873-
async def merge_patch_collection(
874-
self, collection_id: str, collection: stac_types.PartialCollection, **kwargs
875-
) -> Optional[stac_types.Collection]:
876-
"""Patch a collection following RF7396..
877-
878-
Args:
879-
collection_id (str): The ID of the collection to patch.
880-
collection (stac_types.Collection): The partial collection data.
881-
kwargs: Other optional arguments, including the request object.
882-
883-
Returns:
884-
stac_types.Collection: The patched collection object.
885-
886-
"""
887-
collection = await self.database.merge_patch_collection(
888-
collection_id=collection_id,
889-
base_url=str(kwargs["request"].base_url),
890-
collection=collection,
891-
)
892-
893-
return CollectionSerializer.db_to_stac(
894-
collection,
895-
kwargs["request"],
896-
extensions=[type(ext).__name__ for ext in self.database.extensions],
897-
)
898-
899-
async def json_patch_collection(
900-
self, collection_id: str, operations: List[stac_types.PatchOperation], **kwargs
901-
) -> Optional[stac_types.Collection]:
902-
"""Patch a collection following RF6902.
903-
904-
Args:
905-
collection_id (str): The ID of the collection to patch.
906-
operations (List): List of operations to run on collection.
907-
kwargs: Other optional arguments, including the request object.
908-
909-
Returns:
910-
stac_types.Collection: The patched collection object.
911-
912-
"""
913-
collection = await self.database.json_patch_collection(
914-
collection_id=collection_id,
915-
operations=operations,
916-
base_url=str(kwargs["request"].base_url),
917-
)
828+
if collection:
829+
return CollectionSerializer.db_to_stac(
830+
collection,
831+
kwargs["request"],
832+
extensions=[type(ext).__name__ for ext in self.database.extensions],
833+
)
918834

919-
return CollectionSerializer.db_to_stac(
920-
collection,
921-
kwargs["request"],
922-
extensions=[type(ext).__name__ for ext in self.database.extensions],
923-
)
835+
raise NotImplementedError("Content-Type and body combination not implemented")
924836

925837
@overrides
926838
async def delete_collection(self, collection_id: str, **kwargs) -> Optional[stac_types.Collection]:

0 commit comments

Comments
 (0)