-
Notifications
You must be signed in to change notification settings - Fork 39
add patch support #264
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
Open
bitner
wants to merge
2
commits into
main
Choose a base branch
from
support_patch
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add patch support #264
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
@@ -1,6 +1,6 @@ | ||||
services: | ||||
app: | ||||
container_name: stac-fastapi-pgstac | ||||
#container_name: stac-fastapi-pgstac | ||||
image: stac-utils/stac-fastapi-pgstac | ||||
build: . | ||||
environment: | ||||
|
@@ -30,7 +30,7 @@ services: | |||
command: bash -c "./scripts/wait-for-it.sh database:5432 && python -m stac_fastapi.pgstac.app" | ||||
|
||||
tests: | ||||
container_name: stac-fastapi-pgstac-test | ||||
#container_name: stac-fastapi-pgstac-test | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
image: stac-utils/stac-fastapi-pgstac-test | ||||
build: | ||||
context: . | ||||
|
@@ -45,7 +45,7 @@ services: | |||
command: bash -c "python -m pytest -s -vv" | ||||
|
||||
database: | ||||
container_name: stac-db | ||||
#container_name: stac-db | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
image: ghcr.io/stac-utils/pgstac:v0.9.2 | ||||
environment: | ||||
- POSTGRES_USER=username | ||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,8 +5,10 @@ | |||||
from typing import List, Optional, Union | ||||||
|
||||||
import attr | ||||||
import jsonpatch | ||||||
from buildpg import render | ||||||
from fastapi import HTTPException, Request | ||||||
from json_merge_patch import merge | ||||||
from stac_fastapi.extensions.core.transaction import AsyncBaseTransactionsClient | ||||||
from stac_fastapi.extensions.core.transaction.request import ( | ||||||
PartialCollection, | ||||||
|
@@ -19,6 +21,7 @@ | |||||
Items, | ||||||
) | ||||||
from stac_fastapi.types import stac as stac_types | ||||||
from stac_fastapi.types.errors import NotFoundError | ||||||
from stac_pydantic import Collection, Item, ItemCollection | ||||||
from starlette.responses import JSONResponse, Response | ||||||
|
||||||
|
@@ -219,19 +222,95 @@ async def patch_item( | |||||
collection_id: str, | ||||||
item_id: str, | ||||||
patch: Union[PartialItem, List[PatchOperation]], | ||||||
request: Request, | ||||||
**kwargs, | ||||||
) -> Optional[Union[stac_types.Item, Response]]: | ||||||
"""Patch Item.""" | ||||||
raise NotImplementedError | ||||||
|
||||||
# Get Existing Item to Patch | ||||||
async with request.app.state.get_connection(request, "r") as conn: | ||||||
q, p = render( | ||||||
""" | ||||||
SELECT * FROM get_item(:item_id::text, :collection_id::text); | ||||||
""", | ||||||
item_id=item_id, | ||||||
collection_id=collection_id, | ||||||
) | ||||||
existing = await conn.fetchval(q, *p) | ||||||
if existing is None: | ||||||
raise NotFoundError( | ||||||
f"Item {item_id} does not exist in collection {collection_id}." | ||||||
) | ||||||
|
||||||
# Merge Patch with Existing Item | ||||||
if isinstance(patch, list): | ||||||
patchjson = [op.model_dump(mode="json") for op in patch] | ||||||
p = jsonpatch.JsonPatch(patchjson) | ||||||
item = p.apply(existing) | ||||||
elif isinstance(patch, PartialItem): | ||||||
partial = patch.model_dump(mode="json") | ||||||
item = merge(existing, partial) | ||||||
else: | ||||||
raise Exception( | ||||||
"Patch must be a list of PatchOperations or a PartialCollection." | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) | ||||||
|
||||||
self._validate_item(request, item, collection_id, item_id) | ||||||
item["collection"] = collection_id | ||||||
|
||||||
async with request.app.state.get_connection(request, "w") as conn: | ||||||
await dbfunc(conn, "update_item", item) | ||||||
|
||||||
item["links"] = await ItemLinks( | ||||||
collection_id=collection_id, | ||||||
item_id=item["id"], | ||||||
request=request, | ||||||
).get_links(extra_links=item.get("links")) | ||||||
|
||||||
return stac_types.Item(**item) | ||||||
|
||||||
async def patch_collection( | ||||||
self, | ||||||
collection_id: str, | ||||||
patch: Union[PartialCollection, List[PatchOperation]], | ||||||
request: Request, | ||||||
**kwargs, | ||||||
) -> Optional[Union[stac_types.Collection, Response]]: | ||||||
"""Patch Collection.""" | ||||||
raise NotImplementedError | ||||||
|
||||||
# Get Existing Collection to Patch | ||||||
async with request.app.state.get_connection(request, "r") as conn: | ||||||
q, p = render( | ||||||
""" | ||||||
SELECT * FROM get_collection(:id::text); | ||||||
""", | ||||||
id=collection_id, | ||||||
) | ||||||
existing = await conn.fetchval(q, *p) | ||||||
if existing is None: | ||||||
raise NotFoundError(f"Collection {collection_id} does not exist.") | ||||||
|
||||||
# Merge Patch with Existing Collection | ||||||
if isinstance(patch, list): | ||||||
patchjson = [op.model_dump(mode="json") for op in patch] | ||||||
p = jsonpatch.JsonPatch(patchjson) | ||||||
col = p.apply(existing) | ||||||
elif isinstance(patch, PartialCollection): | ||||||
partial = patch.model_dump(mode="json") | ||||||
col = merge(existing, partial) | ||||||
else: | ||||||
raise Exception( | ||||||
"Patch must be a list of PatchOperations or a PartialCollection." | ||||||
) | ||||||
|
||||||
async with request.app.state.get_connection(request, "w") as conn: | ||||||
await dbfunc(conn, "update_collection", col) | ||||||
|
||||||
col["links"] = await CollectionLinks( | ||||||
collection_id=col["id"], request=request | ||||||
).get_links(extra_links=col.get("links")) | ||||||
|
||||||
return stac_types.Collection(**col) | ||||||
|
||||||
|
||||||
@attr.s | ||||||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we're not using, can we delete the line instead of commenting?