Skip to content

Commit fcf4935

Browse files
Update for stac-fastapi v2.5.x (#101)
* first run * fix f-strings * lint * update for v2.5.3 * update changelog * update, uncomment test * fix test * update changelog * update * update test * update * remove comment --------- Co-authored-by: vincentsarago <[email protected]>
1 parent a25b6ac commit fcf4935

File tree

9 files changed

+57
-16
lines changed

9 files changed

+57
-16
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Changed
6+
7+
- Updated stac-fastapi libraries to v2.5.4 ([#101](https://github.com/stac-utils/stac-fastapi-pgstac/pull/101))
8+
-
59
### Added
610

711
- Ability to configure the database runtime parameters ([#92](https://github.com/stac-utils/stac-fastapi-pgstac/pull/92))

scripts/ingest_joplin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def post_or_put(url: str, data: dict):
1919
"""Post or put data to url."""
2020
r = requests.post(url, json=data)
2121
if r.status_code == 409:
22-
new_url = url if data["type"] == "Collection" else url + f"/{data['id']}"
22+
new_url = url + f"/{data['id']}"
2323
# Exists, so update
2424
r = requests.put(new_url, json=data)
2525
# Unchanged may throw a 404

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
"orjson",
1111
"pydantic[dotenv]>=1.10.8", # https://github.com/pydantic/pydantic/issues/5821
1212
"stac_pydantic==2.0.*",
13-
"stac-fastapi.types~=2.4.9",
14-
"stac-fastapi.api~=2.4.9",
15-
"stac-fastapi.extensions~=2.4.9",
13+
"stac-fastapi.types~=2.5.5.post1",
14+
"stac-fastapi.api~=2.5.5.post1",
15+
"stac-fastapi.extensions~=2.5.5.post1",
1616
"asyncpg",
1717
"buildpg",
1818
"brotli_asgi",

stac_fastapi/pgstac/core.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Item crud client."""
22
import re
3-
from datetime import datetime
43
from typing import Any, Dict, List, Optional, Union
54
from urllib.parse import unquote_plus, urljoin
65

@@ -16,9 +15,10 @@
1615
from stac_fastapi.types.core import AsyncBaseCoreClient
1716
from stac_fastapi.types.errors import InvalidQueryParameter, NotFoundError
1817
from stac_fastapi.types.requests import get_base_url
18+
from stac_fastapi.types.rfc3339 import DateTimeType
1919
from stac_fastapi.types.stac import Collection, Collections, Item, ItemCollection
2020
from stac_pydantic.links import Relations
21-
from stac_pydantic.shared import MimeTypes
21+
from stac_pydantic.shared import BBox, MimeTypes
2222

2323
from stac_fastapi.pgstac.config import Settings
2424
from stac_fastapi.pgstac.models.links import (
@@ -28,7 +28,7 @@
2828
PagingLinks,
2929
)
3030
from stac_fastapi.pgstac.types.search import PgstacSearch
31-
from stac_fastapi.pgstac.utils import filter_fields
31+
from stac_fastapi.pgstac.utils import filter_fields, format_datetime_range
3232

3333
NumType = Union[float, int]
3434

@@ -155,8 +155,12 @@ async def _search_base(
155155

156156
settings: Settings = request.app.state.settings
157157

158+
if search_request.datetime:
159+
search_request.datetime = format_datetime_range(search_request.datetime)
160+
158161
search_request.conf = search_request.conf or {}
159162
search_request.conf["nohydrate"] = settings.use_api_hydrate
163+
160164
search_request_json = search_request.json(exclude_none=True, by_alias=True)
161165

162166
try:
@@ -248,8 +252,8 @@ async def item_collection(
248252
self,
249253
collection_id: str,
250254
request: Request,
251-
bbox: Optional[List[NumType]] = None,
252-
datetime: Optional[Union[str, datetime]] = None,
255+
bbox: Optional[BBox] = None,
256+
datetime: Optional[DateTimeType] = None,
253257
limit: Optional[int] = None,
254258
token: str = None,
255259
**kwargs,
@@ -341,8 +345,8 @@ async def get_search(
341345
request: Request,
342346
collections: Optional[List[str]] = None,
343347
ids: Optional[List[str]] = None,
344-
bbox: Optional[List[NumType]] = None,
345-
datetime: Optional[Union[str, datetime]] = None,
348+
bbox: Optional[BBox] = None,
349+
datetime: Optional[DateTimeType] = None,
346350
limit: Optional[int] = None,
347351
query: Optional[str] = None,
348352
token: Optional[str] = None,

stac_fastapi/pgstac/utils.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""stac-fastapi utility methods."""
2+
from datetime import datetime
23
from typing import Any, Dict, Optional, Set, Union
34

5+
from stac_fastapi.types.rfc3339 import DateTimeType
46
from stac_fastapi.types.stac import Item
57

68

@@ -113,3 +115,34 @@ def dict_deep_update(merge_to: Dict[str, Any], merge_from: Dict[str, Any]) -> No
113115
dict_deep_update(merge_to[k], merge_from[k])
114116
else:
115117
merge_to[k] = v
118+
119+
120+
def format_datetime_range(dt_range: DateTimeType) -> Union[str, Any]:
121+
"""
122+
Convert a datetime object or a tuple of datetime objects to a formatted string for datetime ranges.
123+
124+
Args:
125+
dt_range (DateTimeType): The date interval,
126+
which might be a single datetime or a tuple with one or two datetimes.
127+
128+
Returns:
129+
str: A formatted string like 'YYYY-MM-DDTHH:MM:SSZ/..', 'YYYY-MM-DDTHH:MM:SSZ', or the original string input.
130+
"""
131+
# Handle a single datetime object
132+
if isinstance(dt_range, datetime):
133+
return dt_range.isoformat().replace("+00:00", "Z")
134+
135+
# Handle a tuple containing datetime objects or None
136+
if isinstance(dt_range, tuple):
137+
start, end = dt_range
138+
139+
# Convert start datetime to string if not None, otherwise use ".."
140+
start_str = start.isoformat().replace("+00:00", "Z") if start else ".."
141+
142+
# Convert end datetime to string if not None, otherwise use ".."
143+
end_str = end.isoformat().replace("+00:00", "Z") if end else ".."
144+
145+
return f"{start_str}/{end_str}"
146+
147+
# Return input as-is if it's not any expected type (fallback)
148+
return dt_range

tests/api/test_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
"DELETE /collections/{collection_id}/items/{item_id}",
3434
"POST /collections",
3535
"POST /collections/{collection_id}/items",
36-
"PUT /collections",
36+
"PUT /collections/{collection_id}",
3737
"PUT /collections/{collection_id}/items/{item_id}",
3838
]
3939

tests/clients/test_postgres.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ async def test_update_collection(app_client, load_test_collection):
3434
in_coll = load_test_collection
3535
in_coll.keywords.append("newkeyword")
3636

37-
resp = await app_client.put("/collections", json=in_coll.dict())
37+
resp = await app_client.put(f"/collections/{in_coll.id}", json=in_coll.dict())
3838
assert resp.status_code == 200
3939

4040
resp = await app_client.get(f"/collections/{in_coll.id}")

tests/resources/test_collection.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ async def test_update_collection(app_client, load_test_data, load_test_collectio
3232
in_coll = load_test_collection
3333
in_coll.keywords.append("newkeyword")
3434

35-
resp = await app_client.put("/collections", json=in_coll.dict())
35+
resp = await app_client.put(f"/collections/{in_coll.id}", json=in_coll.dict())
3636
assert resp.status_code == 200
3737
put_coll = Collection.parse_obj(resp.json())
3838

@@ -88,7 +88,7 @@ async def test_update_new_collection(app_client, load_test_collection):
8888
in_coll = load_test_collection
8989
in_coll.id = "test-updatenew"
9090

91-
resp = await app_client.put("/collections", json=in_coll.dict())
91+
resp = await app_client.put(f"/collections/{in_coll.id}", json=in_coll.dict())
9292
assert resp.status_code == 404
9393

9494

tests/resources/test_item.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ async def test_update_collection(app_client, load_test_data, load_test_collectio
3939
in_coll = load_test_collection
4040
in_coll.keywords.append("newkeyword")
4141

42-
resp = await app_client.put("/collections", json=in_coll.dict())
42+
resp = await app_client.put(f"/collections/{in_coll.id}", json=in_coll.dict())
4343
assert resp.status_code == 200
4444

4545
resp = await app_client.get(f"/collections/{in_coll.id}")

0 commit comments

Comments
 (0)