Skip to content

Commit e6b353a

Browse files
Return added Item/Collection in Transactions response (#424)
* Add collection_id path parameter and check against Item collection property * Fix unformatted f-strings * Fix Item PUT endpoint per #385 * Update API tests to use new PUT paths * Make equivalent changes to sqlalchemy backend * Add CHANGELOG entry for #425 * Fix failing tests from previous merge * Return 400 for Item id or collection conflicts * Add failing tests * Return added Item/Collection from Transactions endpoints * Format changes * Add CHANGES entry for #424 * Get Transactions Item response collection id from path parameter * remove duplicate test cases Co-authored-by: Jeff Albrecht <[email protected]>
1 parent d2fcf13 commit e6b353a

File tree

4 files changed

+49
-4
lines changed

4 files changed

+49
-4
lines changed

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* Transactions Extension update Item endpoint validates that the `{collection_id}` path parameter matches the Item `"collection"` property
4343
from the request body, if present, and falls back to using the path parameter if no `"collection"` property is found in the body
4444
([#425](https://github.com/stac-utils/stac-fastapi/pull/425))
45+
* PGStac Backend Transactions endpoints return added Item/Collection instead of Item/Collection from request ([#424](https://github.com/stac-utils/stac-fastapi/pull/424))
4546

4647
## [2.3.0]
4748

stac_fastapi/pgstac/stac_fastapi/pgstac/transactions.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
Items,
1313
)
1414
from stac_fastapi.pgstac.db import dbfunc
15+
from stac_fastapi.pgstac.models.links import CollectionLinks, ItemLinks
1516
from stac_fastapi.types import stac as stac_types
1617
from stac_fastapi.types.core import AsyncBaseTransactionsClient
1718

@@ -37,7 +38,12 @@ async def create_item(
3738
request = kwargs["request"]
3839
pool = request.app.state.writepool
3940
await dbfunc(pool, "create_item", item)
40-
return item
41+
item["links"] = await ItemLinks(
42+
collection_id=collection_id,
43+
item_id=item["id"],
44+
request=request,
45+
).get_links(extra_links=item.get("links"))
46+
return stac_types.Item(**item)
4147

4248
async def update_item(
4349
self, collection_id: str, item_id: str, item: stac_types.Item, **kwargs
@@ -59,7 +65,12 @@ async def update_item(
5965
request = kwargs["request"]
6066
pool = request.app.state.writepool
6167
await dbfunc(pool, "update_item", item)
62-
return item
68+
item["links"] = await ItemLinks(
69+
collection_id=collection_id,
70+
item_id=item["id"],
71+
request=request,
72+
).get_links(extra_links=item.get("links"))
73+
return stac_types.Item(**item)
6374

6475
async def create_collection(
6576
self, collection: stac_types.Collection, **kwargs
@@ -68,7 +79,11 @@ async def create_collection(
6879
request = kwargs["request"]
6980
pool = request.app.state.writepool
7081
await dbfunc(pool, "create_collection", collection)
71-
return collection
82+
collection["links"] = await CollectionLinks(
83+
collection_id=collection["id"], request=request
84+
).get_links(extra_links=collection.get("links"))
85+
86+
return stac_types.Collection(**collection)
7287

7388
async def update_collection(
7489
self, collection: stac_types.Collection, **kwargs
@@ -77,7 +92,10 @@ async def update_collection(
7792
request = kwargs["request"]
7893
pool = request.app.state.writepool
7994
await dbfunc(pool, "update_collection", collection)
80-
return collection
95+
collection["links"] = await CollectionLinks(
96+
collection_id=collection["id"], request=request
97+
).get_links(extra_links=collection.get("links"))
98+
return stac_types.Collection(**collection)
8199

82100
async def delete_item(
83101
self, item_id: str, **kwargs

stac_fastapi/pgstac/tests/resources/test_collection.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,21 @@ async def test_create_collection(app_client, load_test_data: Callable):
2020
get_coll = Collection.parse_obj(resp.json())
2121
assert post_coll.dict(exclude={"links"}) == get_coll.dict(exclude={"links"})
2222

23+
post_self_link = next(
24+
(link for link in post_coll.links if link.rel == "self"), None
25+
)
26+
get_self_link = next((link for link in get_coll.links if link.rel == "self"), None)
27+
assert post_self_link is not None and get_self_link is not None
28+
assert post_self_link.href == get_self_link.href
29+
2330

2431
async def test_update_collection(app_client, load_test_data, load_test_collection):
2532
in_coll = load_test_collection
2633
in_coll.keywords.append("newkeyword")
2734

2835
resp = await app_client.put("/collections", json=in_coll.dict())
2936
assert resp.status_code == 200
37+
put_coll = Collection.parse_obj(resp.json())
3038

3139
resp = await app_client.get(f"/collections/{in_coll.id}")
3240
assert resp.status_code == 200
@@ -35,6 +43,11 @@ async def test_update_collection(app_client, load_test_data, load_test_collectio
3543
assert in_coll.dict(exclude={"links"}) == get_coll.dict(exclude={"links"})
3644
assert "newkeyword" in get_coll.keywords
3745

46+
put_self_link = next((link for link in put_coll.links if link.rel == "self"), None)
47+
get_self_link = next((link for link in get_coll.links if link.rel == "self"), None)
48+
assert put_self_link is not None and get_self_link is not None
49+
assert put_self_link.href == get_self_link.href
50+
3851

3952
async def test_delete_collection(
4053
app_client, load_test_data: Callable, load_test_collection

stac_fastapi/pgstac/tests/resources/test_item.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,13 @@ async def test_create_item(app_client, load_test_data: Callable, load_test_colle
8282
get_item = Item.parse_obj(resp.json())
8383
assert in_item.dict(exclude={"links"}) == get_item.dict(exclude={"links"})
8484

85+
post_self_link = next(
86+
(link for link in post_item.links if link.rel == "self"), None
87+
)
88+
get_self_link = next((link for link in get_item.links if link.rel == "self"), None)
89+
assert post_self_link is not None and get_self_link is not None
90+
assert post_self_link.href == get_self_link.href
91+
8592

8693
async def test_create_item_mismatched_collection_id(
8794
app_client, load_test_data: Callable, load_test_collection
@@ -141,6 +148,7 @@ async def test_update_item(
141148
f"/collections/{coll.id}/items/{item.id}", content=item.json()
142149
)
143150
assert resp.status_code == 200
151+
put_item = Item.parse_obj(resp.json())
144152

145153
resp = await app_client.get(f"/collections/{coll.id}/items/{item.id}")
146154
assert resp.status_code == 200
@@ -149,6 +157,11 @@ async def test_update_item(
149157
assert item.dict(exclude={"links"}) == get_item.dict(exclude={"links"})
150158
assert get_item.properties.description == "Update Test"
151159

160+
post_self_link = next((link for link in put_item.links if link.rel == "self"), None)
161+
get_self_link = next((link for link in get_item.links if link.rel == "self"), None)
162+
assert post_self_link is not None and get_self_link is not None
163+
assert post_self_link.href == get_self_link.href
164+
152165

153166
async def test_update_item_mismatched_collection_id(
154167
app_client, load_test_data: Callable, load_test_collection, load_test_item

0 commit comments

Comments
 (0)