stac-fastapi-core 4.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (31) hide show
  1. stac_fastapi_core-4.0.0/PKG-INFO +360 -0
  2. stac_fastapi_core-4.0.0/README.md +342 -0
  3. stac_fastapi_core-4.0.0/setup.cfg +7 -0
  4. stac_fastapi_core-4.0.0/setup.py +46 -0
  5. stac_fastapi_core-4.0.0/stac_fastapi/core/__init__.py +1 -0
  6. stac_fastapi_core-4.0.0/stac_fastapi/core/base_database_logic.py +54 -0
  7. stac_fastapi_core-4.0.0/stac_fastapi/core/base_settings.py +12 -0
  8. stac_fastapi_core-4.0.0/stac_fastapi/core/basic_auth.py +61 -0
  9. stac_fastapi_core-4.0.0/stac_fastapi/core/core.py +1071 -0
  10. stac_fastapi_core-4.0.0/stac_fastapi/core/database_logic.py +226 -0
  11. stac_fastapi_core-4.0.0/stac_fastapi/core/datetime_utils.py +39 -0
  12. stac_fastapi_core-4.0.0/stac_fastapi/core/extensions/__init__.py +5 -0
  13. stac_fastapi_core-4.0.0/stac_fastapi/core/extensions/aggregation.py +577 -0
  14. stac_fastapi_core-4.0.0/stac_fastapi/core/extensions/fields.py +41 -0
  15. stac_fastapi_core-4.0.0/stac_fastapi/core/extensions/filter.py +202 -0
  16. stac_fastapi_core-4.0.0/stac_fastapi/core/extensions/query.py +79 -0
  17. stac_fastapi_core-4.0.0/stac_fastapi/core/models/__init__.py +1 -0
  18. stac_fastapi_core-4.0.0/stac_fastapi/core/models/links.py +205 -0
  19. stac_fastapi_core-4.0.0/stac_fastapi/core/models/search.py +1 -0
  20. stac_fastapi_core-4.0.0/stac_fastapi/core/rate_limit.py +44 -0
  21. stac_fastapi_core-4.0.0/stac_fastapi/core/route_dependencies.py +176 -0
  22. stac_fastapi_core-4.0.0/stac_fastapi/core/serializers.py +177 -0
  23. stac_fastapi_core-4.0.0/stac_fastapi/core/session.py +25 -0
  24. stac_fastapi_core-4.0.0/stac_fastapi/core/utilities.py +164 -0
  25. stac_fastapi_core-4.0.0/stac_fastapi/core/version.py +2 -0
  26. stac_fastapi_core-4.0.0/stac_fastapi_core.egg-info/PKG-INFO +360 -0
  27. stac_fastapi_core-4.0.0/stac_fastapi_core.egg-info/SOURCES.txt +30 -0
  28. stac_fastapi_core-4.0.0/stac_fastapi_core.egg-info/dependency_links.txt +1 -0
  29. stac_fastapi_core-4.0.0/stac_fastapi_core.egg-info/not-zip-safe +1 -0
  30. stac_fastapi_core-4.0.0/stac_fastapi_core.egg-info/requires.txt +13 -0
  31. stac_fastapi_core-4.0.0/stac_fastapi_core.egg-info/top_level.txt +1 -0
@@ -0,0 +1,360 @@
1
+ Metadata-Version: 2.1
2
+ Name: stac_fastapi_core
3
+ Version: 4.0.0
4
+ Summary: Core library for the Elasticsearch and Opensearch stac-fastapi backends.
5
+ Home-page: https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch
6
+ License: MIT
7
+ Classifier: Intended Audience :: Developers
8
+ Classifier: Intended Audience :: Information Technology
9
+ Classifier: Intended Audience :: Science/Research
10
+ Classifier: Programming Language :: Python :: 3.9
11
+ Classifier: Programming Language :: Python :: 3.10
12
+ Classifier: Programming Language :: Python :: 3.11
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: 3.13
15
+ Classifier: License :: OSI Approved :: MIT License
16
+ Requires-Python: >=3.9
17
+ Description-Content-Type: text/markdown
18
+
19
+ # stac-fastapi-elasticsearch-opensearch (sfeos)
20
+
21
+ <!-- markdownlint-disable MD033 MD041 -->
22
+
23
+ <p align="left">
24
+ <img src="https://github.com/radiantearth/stac-site/raw/master/images/logo/stac-030-long.png" width=600>
25
+ <p align="left"><b>Elasticsearch and Opensearch backends for the stac-fastapi project.</b></p>
26
+ <p align="left"><b>Featuring stac-fastapi.core for simplifying the creation and maintenance of custom STAC api backends.</b></p>
27
+ </p>
28
+
29
+
30
+ [![PyPI version](https://badge.fury.io/py/stac-fastapi-elasticsearch.svg)](https://badge.fury.io/py/stac-fastapi-elasticsearch) [![PyPI version](https://badge.fury.io/py/stac-fastapi-opensearch.svg)](https://badge.fury.io/py/stac-fastapi-opensearch)
31
+ [![Join the chat at https://gitter.im/stac-fastapi-elasticsearch/community](https://badges.gitter.im/stac-fastapi-elasticsearch/community.svg)](https://gitter.im/stac-fastapi-elasticsearch/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
32
+
33
+
34
+ ---
35
+
36
+ **Online Documentation**: [https://stac-utils.github.io/stac-fastapi-elasticsearch-opensearch](https://stac-utils.github.io/stac-fastapi-elasticsearch-opensearch/)
37
+
38
+ **Source Code**: [https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch)
39
+
40
+
41
+ ---
42
+
43
+ ### Notes:
44
+
45
+ - Our Api core library can be used to create custom backends. See [stac-fastapi-mongo](https://github.com/Healy-Hyperspatial/stac-fastapi-mongo) for a working example.
46
+ - Reach out on our [Gitter](https://app.gitter.im/#/room/#stac-fastapi-elasticsearch_community:gitter.im) channel or feel free to add to our [Discussions](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/discussions) page here on github.
47
+ - There is [Postman](https://documenter.getpostman.com/view/12888943/2s8ZDSdRHA) documentation here for examples on how to run some of the API routes locally - after starting the elasticsearch backend via the compose.yml file.
48
+ - The `/examples` folder shows an example of running stac-fastapi-elasticsearch from PyPI in docker without needing any code from the repository. There is also a Postman collection here that you can load into Postman for testing the API routes.
49
+
50
+
51
+ ### Performance Note
52
+
53
+ The `enable_direct_response` option is provided by the stac-fastapi core library (introduced in stac-fastapi 5.2.0) and is available in this project starting from v4.0.0.
54
+
55
+ **You can now control this setting via the `ENABLE_DIRECT_RESPONSE` environment variable.**
56
+
57
+ When enabled (`ENABLE_DIRECT_RESPONSE=true`), endpoints return Starlette Response objects directly, bypassing FastAPI's default serialization for improved performance. **However, all FastAPI dependencies (including authentication, custom status codes, and validation) are disabled for all routes.**
58
+
59
+ This mode is best suited for public or read-only APIs where authentication and custom logic are not required. Default is `false` for safety.
60
+
61
+ See: [issue #347](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/issues/347)
62
+
63
+
64
+ ### To install from PyPI:
65
+
66
+ ```bash
67
+ # For versions 4.0.0a1 and newer (PEP 625 compliant naming):
68
+ pip install stac-fastapi-elasticsearch # Elasticsearch backend
69
+ pip install stac-fastapi-opensearch # Opensearch backend
70
+ pip install stac-fastapi-core # Core library
71
+
72
+ # For versions 4.0.0a0 and older:
73
+ pip install stac-fastapi.elasticsearch # Elasticsearch backend
74
+ pip install stac-fastapi.opensearch # Opensearch backend
75
+ pip install stac-fastapi.core # Core library
76
+ ```
77
+
78
+ > **Important Note:** Starting with version 4.0.0a1, package names have changed from using periods (e.g., `stac-fastapi.core`) to using hyphens (e.g., `stac-fastapi-core`) to comply with PEP 625. The internal package structure uses underscores, but users should install with hyphens as shown above. Please update your requirements files accordingly.
79
+
80
+ ### To install and run via pre-built Docker Images
81
+
82
+ We provide ready-to-use Docker images through GitHub Container Registry ([ElasticSearch](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pkgs/container/stac-fastapi-es) and [OpenSearch](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pkgs/container/stac-fastapi-os) backends). You can easily pull and run these images:
83
+
84
+ ```shell
85
+ # For Elasticsearch backend
86
+ docker pull ghcr.io/stac-utils/stac-fastapi-es:latest
87
+
88
+ # For OpenSearch backend
89
+ docker pull ghcr.io/stac-utils/stac-fastapi-os:latest
90
+ ```
91
+
92
+ ## Run Elasticsearch API backend on localhost:8080
93
+
94
+ You need to ensure [**Docker Compose**](https://docs.docker.com/compose/install/) or [**Podman Compose**](https://podman-desktop.io/docs/compose) installed and running on your machine. In the following command instead of `docker compose` you can use `podman-compose` as well.
95
+
96
+ ```shell
97
+ docker compose up elasticsearch app-elasticsearch
98
+ ```
99
+
100
+ By default, Docker Compose uses Elasticsearch 8.x and OpenSearch 2.11.1.
101
+ If you wish to use a different version, put the following in a
102
+ file named `.env` in the same directory you run Docker Compose from:
103
+
104
+ ```shell
105
+ ELASTICSEARCH_VERSION=8.11.0
106
+ OPENSEARCH_VERSION=2.11.1
107
+ ENABLE_DIRECT_RESPONSE=false
108
+ ```
109
+ The most recent Elasticsearch 7.x versions should also work. See the [opensearch-py docs](https://github.com/opensearch-project/opensearch-py/blob/main/COMPATIBILITY.md) for compatibility information.
110
+
111
+ #### **Configuration reference keys:**
112
+
113
+ You can customize additional settings in your `.env` file:
114
+ ###### Key variables to configure:
115
+
116
+ | Variable | Description | Default | Required |
117
+ |------------------------------|--------------------------------------------------------------------------------------|--------------------------|---------------------------------------------------------------------------------------------|
118
+ | `ES_HOST` | Hostname for external Elasticsearch/OpenSearch. | `localhost` | Optional |
119
+ | `ES_PORT` | Port for Elasticsearch/OpenSearch. | `9200` (ES) / `9202` (OS)| Optional |
120
+ | `ES_USE_SSL` | Use SSL for connecting to Elasticsearch/OpenSearch. | `false` | Optional |
121
+ | `ES_VERIFY_CERTS` | Verify SSL certificates when connecting. | `false` | Optional |
122
+ | `STAC_FASTAPI_TITLE` | Title of the API in the documentation. | `stac-fastapi-elasticsearch` or `stac-fastapi-opensearch` | Optional |
123
+ | `STAC_FASTAPI_DESCRIPTION` | Description of the API in the documentation. | N/A | Optional |
124
+ | `STAC_FASTAPI_VERSION` | API version. | `2.1` | Optional |
125
+ | `APP_HOST` | Server bind address. | `0.0.0.0` | Optional |
126
+ | `APP_PORT` | Server port. | `8080` | Optional |
127
+ | `ENVIRONMENT` | Runtime environment. | `local` | Optional |
128
+ | `WEB_CONCURRENCY` | Number of worker processes. | `10` | Optional |
129
+ | `RELOAD` | Enable auto-reload for development. | `true` | Optional |
130
+ | `STAC_FASTAPI_RATE_LIMIT` | API rate limit per client. | `200/minute` | Optional |
131
+ | `BACKEND` | Tests-related variable | `elasticsearch` or `opensearch` based on the backend | Optional |
132
+ | `ELASTICSEARCH_VERSION` | Version of Elasticsearch to use. | `8.11.0` | Optional |
133
+ | `ENABLE_DIRECT_RESPONSE` | Enable direct response for maximum performance (disables all FastAPI dependencies, including authentication, custom status codes, and validation) | `false` | Optional |
134
+ | `OPENSEARCH_VERSION` | OpenSearch version | `2.11.1` | Optional |
135
+
136
+ > [!NOTE]
137
+ > The variables `ES_HOST`, `ES_PORT`, `ES_USE_SSL`, and `ES_VERIFY_CERTS` apply to both Elasticsearch and OpenSearch backends, so there is no need to rename the key names to `OS_` even if you're using OpenSearch.
138
+
139
+ ## Interacting with the API
140
+
141
+ To create a new Collection:
142
+
143
+ ```shell
144
+ curl -X "POST" "http://localhost:8080/collections" \
145
+ -H 'Content-Type: application/json; charset=utf-8' \
146
+ -d $'{
147
+ "id": "my_collection"
148
+ }'
149
+ ```
150
+
151
+ Note: this "Collections Transaction" behavior is not part of the STAC API, but may be soon.
152
+
153
+ ## Configure the API
154
+
155
+ By default the API title and description are set to `stac-fastapi-<backend>`. Change the API title and description from the default by setting the `STAC_FASTAPI_TITLE` and `STAC_FASTAPI_DESCRIPTION` environment variables, respectively.
156
+
157
+ By default the API will read from and write to the `collections` and `items_<collection name>` indices. To change the API collections index and the items index prefix, change the `STAC_COLLECTIONS_INDEX` and `STAC_ITEMS_INDEX_PREFIX` environment variables.
158
+
159
+ The application root path is left as the base url by default. If deploying to AWS Lambda with a Gateway API, you will need to define the app root path to be the same as the Gateway API stage name where you will deploy the API. The app root path can be defined with the `STAC_FASTAPI_ROOT_PATH` environment variable (`/v1`, for example)
160
+
161
+ ## Collection pagination
162
+
163
+ The collections route handles optional `limit` and `token` parameters. The `links` field that is
164
+ returned from the `/collections` route contains a `next` link with the token that can be used to
165
+ get the next page of results.
166
+
167
+ ```shell
168
+ curl -X "GET" "http://localhost:8080/collections?limit=1&token=example_token"
169
+ ```
170
+
171
+ ## Ingesting Sample Data CLI Tool
172
+
173
+ ```shell
174
+ Usage: data_loader.py [OPTIONS]
175
+
176
+ Load STAC items into the database.
177
+
178
+ Options:
179
+ --base-url TEXT Base URL of the STAC API [required]
180
+ --collection-id TEXT ID of the collection to which items are added
181
+ --use-bulk Use bulk insert method for items
182
+ --data-dir PATH Directory containing collection.json and feature
183
+ collection file
184
+ --help Show this message and exit.
185
+ ```
186
+
187
+ ```shell
188
+ python3 data_loader.py --base-url http://localhost:8080
189
+ ```
190
+
191
+
192
+ ## Elasticsearch Mappings
193
+
194
+ Mappings apply to search index, not source. The mappings are stored in index templates on application startup.
195
+ These templates will be used implicitly when creating new Collection and Item indices.
196
+
197
+
198
+ ## Managing Elasticsearch Indices
199
+ ### Snapshots
200
+
201
+ This section covers how to create a snapshot repository and then create and restore snapshots with this.
202
+
203
+ Create a snapshot repository. This puts the files in the `elasticsearch/snapshots` in this git repo clone, as
204
+ the elasticsearch.yml and compose files create a mapping from that directory to
205
+ `/usr/share/elasticsearch/snapshots` within the Elasticsearch container and grant permissions on using it.
206
+
207
+ ```shell
208
+ curl -X "PUT" "http://localhost:9200/_snapshot/my_fs_backup" \
209
+ -H 'Content-Type: application/json; charset=utf-8' \
210
+ -d $'{
211
+ "type": "fs",
212
+ "settings": {
213
+ "location": "/usr/share/elasticsearch/snapshots/my_fs_backup"
214
+ }
215
+ }'
216
+ ```
217
+
218
+ The next step is to create a snapshot of one or more indices into this snapshot repository. This command creates
219
+ a snapshot named `my_snapshot_2` and waits for the action to be completed before returning. This can also be done
220
+ asynchronously, and queried for status. The `indices` parameter determines which indices are snapshotted, and
221
+ can include wildcards.
222
+
223
+ ```shell
224
+ curl -X "PUT" "http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2?wait_for_completion=true" \
225
+ -H 'Content-Type: application/json; charset=utf-8' \
226
+ -d $'{
227
+ "metadata": {
228
+ "taken_because": "dump of all items",
229
+ "taken_by": "pvarner"
230
+ },
231
+ "include_global_state": false,
232
+ "ignore_unavailable": false,
233
+ "indices": "items_my-collection"
234
+ }'
235
+ ```
236
+
237
+ To see the status of this snapshot:
238
+
239
+ ```shell
240
+ curl http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2
241
+ ```
242
+
243
+ To see all the snapshots:
244
+
245
+ ```shell
246
+ curl http://localhost:9200/_snapshot/my_fs_backup/_all
247
+ ```
248
+
249
+ To restore a snapshot, run something similar to the following. This specific command will restore any indices that
250
+ match `items_*` and rename them so that the new index name will be suffixed with `-copy`.
251
+
252
+ ```shell
253
+ curl -X "POST" "http://localhost:9200/_snapshot/my_fs_backup/my_snapshot_2/_restore?wait_for_completion=true" \
254
+ -H 'Content-Type: application/json; charset=utf-8' \
255
+ -d $'{
256
+ "include_aliases": false,
257
+ "include_global_state": false,
258
+ "ignore_unavailable": true,
259
+ "rename_replacement": "items_$1-copy",
260
+ "indices": "items_*",
261
+ "rename_pattern": "items_(.+)"
262
+ }'
263
+ ```
264
+
265
+ Now the item documents have been restored in to the new index (e.g., `my-collection-copy`), but the value of the
266
+ `collection` field in those documents is still the original value of `my-collection`. To update these to match the
267
+ new collection name, run the following Elasticsearch Update By Query command, substituting the old collection name
268
+ into the term filter and the new collection name into the script parameter:
269
+
270
+ ```shell
271
+ curl -X "POST" "http://localhost:9200/items_my-collection-copy/_update_by_query" \
272
+ -H 'Content-Type: application/json; charset=utf-8' \
273
+ -d $'{
274
+ "query": {
275
+ "match_all": {}
276
+ },
277
+ "script": {
278
+ "lang": "painless",
279
+ "params": {
280
+ "collection": "my-collection-copy"
281
+ },
282
+ "source": "ctx._source.collection = params.collection"
283
+ }
284
+ }'
285
+ ```
286
+
287
+ Then, create a new collection through the api with the new name for each of the restored indices:
288
+
289
+ ```shell
290
+ curl -X "POST" "http://localhost:8080/collections" \
291
+ -H 'Content-Type: application/json' \
292
+ -d $'{
293
+ "id": "my-collection-copy"
294
+ }'
295
+ ```
296
+
297
+ Voila! You have a copy of the collection now that has a resource URI (`/collections/my-collection-copy`) and can be
298
+ correctly queried by collection name.
299
+
300
+ ### Reindexing
301
+ This section covers how to reindex documents stored in Elasticsearch/OpenSearch.
302
+ A reindex operation might be useful to apply changes to documents or to correct dynamically generated mappings.
303
+
304
+ The index templates will make sure that manually created indices will also have the correct mappings and settings.
305
+
306
+ In this example, we will make a copy of an existing Item index `items_my-collection-lower_my-collection-hex-000001` but change the Item identifier to be lowercase.
307
+
308
+ ```shell
309
+ curl -X "POST" "http://localhost:9200/_reindex" \
310
+ -H 'Content-Type: application/json' \
311
+ -d $'{
312
+ "source": {
313
+ "index": "items_my-collection-lower_my-collection-hex-000001"
314
+ },
315
+ "dest": {
316
+ "index": "items_my-collection-lower_my-collection-hex-000002"
317
+ },
318
+ "script": {
319
+ "source": "ctx._source.id = ctx._source.id.toLowerCase()",
320
+ "lang": "painless"
321
+ }
322
+ }'
323
+ ```
324
+
325
+ If we are happy with the data in the newly created index, we can move the alias `items_my-collection` to the new index `items_my-collection-lower_my-collection-hex-000002`.
326
+ ```shell
327
+ curl -X "POST" "http://localhost:9200/_aliases" \
328
+ -h 'Content-Type: application/json' \
329
+ -d $'{
330
+ "actions": [
331
+ {
332
+ "remove": {
333
+ "index": "*",
334
+ "alias": "items_my-collection"
335
+ }
336
+ },
337
+ {
338
+ "add": {
339
+ "index": "items_my-collection-lower_my-collection-hex-000002",
340
+ "alias": "items_my-collection"
341
+ }
342
+ }
343
+ ]
344
+ }'
345
+ ```
346
+
347
+ The modified Items with lowercase identifiers will now be visible to users accessing `my-collection` in the STAC API.
348
+
349
+
350
+ ## Auth
351
+
352
+ Authentication is an optional feature that can be enabled through `Route Dependencies` examples can be found and a more detailed explanation in [examples/auth](examples/auth).
353
+
354
+ ## Aggregation
355
+
356
+ Aggregation of points and geometries, as well as frequency distribution aggregation of any other property including dates is supported in stac-fatsapi-elasticsearch-opensearch. Aggregations can be defined at the root Catalog level (`/aggregations`) and at the Collection level (`/<collection_id>/aggregations`). Details for supported aggregations can be found in [the aggregation docs](./docs/src/aggregation.md)
357
+
358
+ ## Rate Limiting
359
+
360
+ Rate limiting is an optional security feature that controls API request frequency on a remote address basis. It's enabled by setting the `STAC_FASTAPI_RATE_LIMIT` environment variable, e.g., `500/minute`. This limits each client to 500 requests per minute, helping prevent abuse and maintain API stability. Implementation examples are available in the [examples/rate_limit](examples/rate_limit) directory.