|
3 | 3 | import logging |
4 | 4 | import os |
5 | 5 | import re |
6 | | -from datetime import datetime |
7 | 6 | from typing import Annotated, Optional |
8 | 7 | from urllib.parse import unquote_plus |
9 | 8 |
|
|
25 | 24 | SortExtension, |
26 | 25 | TokenPaginationExtension, |
27 | 26 | ) |
28 | | -from stac_fastapi.types.rfc3339 import DateTimeType |
29 | 27 | from stac_fastapi.types.search import BaseSearchPostRequest |
30 | 28 | from stac_pydantic import Item, ItemCollection |
31 | 29 | from starlette.middleware.base import BaseHTTPMiddleware |
@@ -77,7 +75,6 @@ async def dispatch(self, request, call_next): |
77 | 75 |
|
78 | 76 | MAX_ITEMS = int(os.environ.get("MAX_ITEMS", "10")) |
79 | 77 |
|
80 | | - |
81 | 78 | def get_base_url(request): |
82 | 79 | global default_base_url |
83 | 80 |
|
@@ -148,7 +145,6 @@ async def get_collection_queryables( |
148 | 145 |
|
149 | 146 |
|
150 | 147 | @app.get("/search") |
151 | | -@app.post("/search") |
152 | 148 | async def get_search( |
153 | 149 | request: Request, |
154 | 150 | credentials: Annotated[ |
@@ -244,7 +240,84 @@ async def get_search( |
244 | 240 |
|
245 | 241 |
|
246 | 242 | async def prepare_search( |
247 | | - search_request: BaseSearchPostRequest, |
| 243 | + search_request: POST_REQUEST_MODEL, |
| 244 | + request: Request, |
| 245 | + credentials: Annotated[ |
| 246 | + fastapi.security.HTTPBasicCredentials, fastapi.Depends(security) |
| 247 | + ], |
| 248 | +) -> ItemCollection: |
| 249 | + """Search planet items. |
| 250 | +
|
| 251 | + Args: |
| 252 | + search request (BaseSearchPostRequest): The search request. |
| 253 | +
|
| 254 | + Returns: |
| 255 | + ItemCollection: The items. |
| 256 | + """ |
| 257 | + |
| 258 | + client = get_authenticated_client(credentials) |
| 259 | + base_url = get_base_url(request) |
| 260 | + |
| 261 | + auth = get_auth(credentials) |
| 262 | + |
| 263 | + search_request.limit = MAX_ITEMS if search_request.limit > MAX_ITEMS else search_request.limit |
| 264 | + |
| 265 | + if search_request.ids: |
| 266 | + |
| 267 | + all_collections = search_request.collections if search_request.collections else await get_collections(client) |
| 268 | + |
| 269 | + all_items = [] |
| 270 | + |
| 271 | + for item_id in search_request.ids: |
| 272 | + for collection_id in all_collections: |
| 273 | + try: |
| 274 | + item = await get_item(collection_id=collection_id, item_id=item_id, request=request, credentials=credentials) |
| 275 | + all_items.append(item) |
| 276 | + except httpx.HTTPStatusError: # unable to find item in catalogue |
| 277 | + pass |
| 278 | + |
| 279 | + return ItemCollection(**{ |
| 280 | + "type": "FeatureCollection", |
| 281 | + "features": all_items, |
| 282 | + "links": [ |
| 283 | + { |
| 284 | + "rel": "self", |
| 285 | + "href": f"{base_url}search", |
| 286 | + "type": "application/geo+json" |
| 287 | + }, |
| 288 | + { |
| 289 | + "rel": "root", |
| 290 | + "href": base_url, |
| 291 | + "type": "application/json" |
| 292 | + }], |
| 293 | + }) |
| 294 | + |
| 295 | + else: |
| 296 | + if getattr(search_request, "token", False): |
| 297 | + token_url = FERNET.decrypt(search_request.token).decode("utf-8") |
| 298 | + planet_response = await client.get(token_url) |
| 299 | + |
| 300 | + else: |
| 301 | + planet_parameters, planet_request = stac_to_planet_request( |
| 302 | + stac_request=search_request |
| 303 | + ) |
| 304 | + |
| 305 | + planet_response = await client.post( |
| 306 | + "https://api.planet.com/data/v1/quick-search", |
| 307 | + params=planet_parameters, |
| 308 | + json=planet_request, |
| 309 | + ) |
| 310 | + |
| 311 | + planet_response.raise_for_status() |
| 312 | + |
| 313 | + return planet_to_stac_response( |
| 314 | + planet_response=planet_response.json(), base_url=base_url, auth=auth |
| 315 | + ) |
| 316 | + |
| 317 | + |
| 318 | +@app.post("/search") |
| 319 | +async def post_search( |
| 320 | + search_request: POST_REQUEST_MODEL, |
248 | 321 | request: Request, |
249 | 322 | credentials: Annotated[ |
250 | 323 | fastapi.security.HTTPBasicCredentials, fastapi.Depends(security) |
|
0 commit comments