|
| 1 | +# stac-fastapi v4.0 Migration Guide |
| 2 | + |
| 3 | +This document aims to help you update your application from **stac-fastapi** 3.0 to 4.0 |
| 4 | + |
| 5 | +## CHANGELOG |
| 6 | +### Changed |
| 7 | + |
| 8 | +* use `string` type instead of python `datetime.datetime` for datetime parameter in `BaseSearchGetRequest`, `ItemCollectionUri` and `BaseCollectionSearchGetRequest` GET models |
| 9 | +* rename `filter` to `filter_expr` for `FilterExtensionGetRequest` and `FilterExtensionPostRequest` attributes to avoid conflict with python filter method |
| 10 | +* remove `post_request_model` attribute in `BaseCoreClient` and `AsyncBaseCoreClient` |
| 11 | +* remove `python3.8` support |
| 12 | + |
| 13 | +### Fixed |
| 14 | + |
| 15 | +* Support multiple proxy servers in the `forwarded` header in `ProxyHeaderMiddleware` ([#782](https://github.com/stac-utils/stac-fastapi/pull/782)) |
| 16 | + |
| 17 | +## Datetime type in GET request models |
| 18 | + |
| 19 | +While the POST request models are created using stac-pydantic, the GET request models are python `attrs` classes (~dataclasses). |
| 20 | +In 4.0, we've decided to change how the `datetime` attribute was defined in `BaseSearchGetRequest`, `ItemCollectionUri` and `BaseCollectionSearchGetRequest` models to match |
| 21 | +the `datetime` definition/validation done by the pydantic model. This mostly mean that the datetime attribute forwarded to the GET endpoints will now be of type string (forwarded from the user input). |
| 22 | + |
| 23 | +```python |
| 24 | +from starlette.testclient import TestClient |
| 25 | +from stac_fastapi.api.app import StacApi |
| 26 | +from stac_fastapi.types.config import ApiSettings |
| 27 | +from stac_fastapi.types.core import BaseCoreClient |
| 28 | + |
| 29 | +class DummyCoreClient(BaseCoreClient): |
| 30 | + def all_collections(self, *args, **kwargs): |
| 31 | + raise NotImplementedError |
| 32 | + |
| 33 | + def get_collection(self, *args, **kwargs): |
| 34 | + raise NotImplementedError |
| 35 | + |
| 36 | + def get_item(self, *args, **kwargs): |
| 37 | + raise NotImplementedError |
| 38 | + |
| 39 | + def get_search(self, *args, datetime = None, **kwargs): |
| 40 | + # Return True if datetime is a string |
| 41 | + return isinstance(datetime, str) |
| 42 | + |
| 43 | + def post_search(self, *args, **kwargs): |
| 44 | + raise NotImplementedError |
| 45 | + |
| 46 | + def item_collection(self, *args, **kwargs): |
| 47 | + raise NotImplementedError |
| 48 | + |
| 49 | +api = StacApi( |
| 50 | + settings=ApiSettings(enable_response_models=False), |
| 51 | + client=DummyCoreClient(), |
| 52 | + extensions=[], |
| 53 | +) |
| 54 | + |
| 55 | + |
| 56 | +# before |
| 57 | +with TestClient(api.app) as client: |
| 58 | + response = client.get( |
| 59 | + "/search", |
| 60 | + params={ |
| 61 | + "datetime": "2020-01-01T00:00:00.00001Z", |
| 62 | + }, |
| 63 | + ) |
| 64 | + assert response.json() == False |
| 65 | + |
| 66 | +# now |
| 67 | +with TestClient(api.app) as client: |
| 68 | + response = client.get( |
| 69 | + "/search", |
| 70 | + params={ |
| 71 | + "datetime": "2020-01-01T00:00:00.00001Z", |
| 72 | + }, |
| 73 | + ) |
| 74 | + assert response.json() == True |
| 75 | +``` |
| 76 | + |
| 77 | +#### Start/End dates |
| 78 | + |
| 79 | +Following stac-pydantic's `Search` model, we've added class attributes to easily retrieve the `parsed` dates: |
| 80 | + |
| 81 | +```python |
| 82 | +from stac_fastapi.types.search import BaseSearchGetRequest |
| 83 | + |
| 84 | +# Interval |
| 85 | +search = BaseSearchGetRequest(datetime="2020-01-01T00:00:00.00001Z/2020-01-02T00:00:00.00001Z") |
| 86 | + |
| 87 | +search.parse_datetime() |
| 88 | +>>> (datetime.datetime(2020, 1, 1, 0, 0, 0, 10, tzinfo=datetime.timezone.utc), datetime.datetime(2020, 1, 2, 0, 0, 0, 10, tzinfo=datetime.timezone.utc)) |
| 89 | + |
| 90 | +search.start_date |
| 91 | +>>> datetime.datetime(2020, 1, 1, 0, 0, 0, 10, tzinfo=datetime.timezone.utc) |
| 92 | + |
| 93 | +search.end_date |
| 94 | +>>> datetime.datetime(2020, 1, 2, 0, 0, 0, 10, tzinfo=datetime.timezone.utc) |
| 95 | + |
| 96 | +# Single date |
| 97 | +search = BaseSearchGetRequest(datetime="2020-01-01T00:00:00.00001Z") |
| 98 | + |
| 99 | +search.parse_datetime() |
| 100 | +>>> datetime.datetime(2020, 1, 1, 0, 0, 0, 10, tzinfo=datetime.timezone.utc) |
| 101 | + |
| 102 | +search.start_date |
| 103 | +>>> datetime.datetime(2020, 1, 1, 0, 0, 0, 10, tzinfo=datetime.timezone.utc) |
| 104 | + |
| 105 | +search.end_date |
| 106 | +>>> None |
| 107 | +``` |
| 108 | + |
| 109 | +## Filter extension |
| 110 | + |
| 111 | +We've renamed the `filter` attribute to `filter_expr` in the `FilterExtensionGetRequest` and `FilterExtensionPostRequest` models to avoid any conflict with python `filter` method. This change means GET endpoints with the filter extension enabled will receive `filter_expr=` option instead of `filter=`. Same for POST endpoints where the `body` will now have a `.filter_expr` instead of a `filter` attribute. |
| 112 | + |
| 113 | +Note: This change does not affect the `input` because we use `aliases`. |
| 114 | + |
| 115 | +```python |
| 116 | +from starlette.testclient import TestClient |
| 117 | +from stac_fastapi.api.app import StacApi |
| 118 | +from stac_fastapi.api.models import create_get_request_model, create_post_request_model |
| 119 | +from stac_fastapi.extensions.core import FilterExtension |
| 120 | +from stac_fastapi.types.config import ApiSettings |
| 121 | +from stac_fastapi.types.core import BaseCoreClient |
| 122 | + |
| 123 | +class DummyCoreClient(BaseCoreClient): |
| 124 | + def all_collections(self, *args, **kwargs): |
| 125 | + raise NotImplementedError |
| 126 | + |
| 127 | + def get_collection(self, *args, **kwargs): |
| 128 | + raise NotImplementedError |
| 129 | + |
| 130 | + def get_item(self, *args, **kwargs): |
| 131 | + raise NotImplementedError |
| 132 | + |
| 133 | + def get_search(self, *args, **kwargs): |
| 134 | + return kwargs |
| 135 | + |
| 136 | + def post_search(self, *args, **kwargs): |
| 137 | + return args[0].model_dump() |
| 138 | + |
| 139 | + def item_collection(self, *args, **kwargs): |
| 140 | + raise NotImplementedError |
| 141 | + |
| 142 | +extensions = [FilterExtension()] |
| 143 | +api = StacApi( |
| 144 | + settings=ApiSettings(enable_response_models=False), |
| 145 | + client=DummyCoreClient(), |
| 146 | + extensions=extensions, |
| 147 | + search_get_request_model=create_get_request_model(extensions), |
| 148 | + search_post_request_model=create_post_request_model(extensions), |
| 149 | +) |
| 150 | + |
| 151 | + |
| 152 | +# before |
| 153 | +with TestClient(api.app) as client: |
| 154 | + response = client.post( |
| 155 | + "/search", |
| 156 | + json={ |
| 157 | + "filter": {"op": "=", "args": [{"property": "test_property"}, "test-value"]}, |
| 158 | + }, |
| 159 | + ) |
| 160 | + assert response.json()["filter"] |
| 161 | + |
| 162 | + response = client.get( |
| 163 | + "/search", |
| 164 | + params={ |
| 165 | + "filter": "id='item_id' AND collection='collection_id'", |
| 166 | + }, |
| 167 | + ) |
| 168 | + assert response.json()["filter"] |
| 169 | + |
| 170 | +# now |
| 171 | +with TestClient(api.app) as client: |
| 172 | + response = client.post( |
| 173 | + "/search", |
| 174 | + json={ |
| 175 | + "filter": {"op": "=", "args": [{"property": "test_property"}, "test-value"]}, |
| 176 | + }, |
| 177 | + ) |
| 178 | + assert response.json()["filter_expr"] |
| 179 | + |
| 180 | + response = client.get( |
| 181 | + "/search", |
| 182 | + params={ |
| 183 | + "filter": "id='item_id' AND collection='collection_id'", |
| 184 | + }, |
| 185 | + ) |
| 186 | + assert response.json()["filter_expr"] |
| 187 | +``` |
| 188 | + |
| 189 | + |
0 commit comments