class CatalogRequestManager:
"""
Pre-process catalog requests before they are routed to stac-fastapi.
Responsibilities include owner/collection authorization, frontend-to-pgstac
route/body/query rewriting, S3 publication validation, and deleting S3
assets before DELETE mutations reach pgstac.
"""
def __init__(self, client: CoreCrudClient, request_ids: dict[Any, Any]):
self.client = client
self.request_ids = request_ids
self.s3_files_to_be_deleted: list = []
@lru_cache
def s3_manager(self, request: Request):
"""
Creates a cached instance of S3Manager for this class instance (self).
Use S3 object storage credentials of the logged user.
"""
return S3Manager(authentication.get_s3_credentials(request))
def _override_request_body(self, request: Request, content: Any) -> Request:
"""
Replace the request body consumed by downstream stac-fastapi.
Starlette caches parsed body/json on the Request object. When middleware
changes catalog ids, timestamps or filters, both cached values must be
updated so later handlers observe the rewritten payload.
"""
request._body = json.dumps(content).encode("utf-8") # pylint: disable=protected-access
request._json = content # pylint: disable=protected-access
logger.info(
"Overrode catalog request body for %s %s; owner=%s, collections=%s, item=%s",
request.method,
request.scope["path"],
self.request_ids.get("owner_id"),
self.request_ids.get("collection_ids"),
self.request_ids.get("item_id"),
)
logger.debug("new request body and json: %s", request._body) # pylint: disable=protected-access
return request
def _override_request_query_string(self, request: Request, query_params: dict) -> Request:
"""
Replace the request query string consumed by downstream stac-fastapi.
Query parameters are rewritten after owner resolution and filter
normalization, then re-encoded with doseq support for list-like values.
"""
request.scope["query_string"] = urlencode(query_params, doseq=True).encode("utf-8")
logger.info("Overrode catalog query string for %s %s", request.method, request.scope["path"])
logger.debug("Updated catalog query params: %s", query_params)
logger.debug("new request query_string: %s", request.scope["query_string"])
return request
async def _collection_exists(self, request: Request, collection_id: str) -> bool:
"""Check if the collection exists.
Returns:
bool: True if the collection exists, False otherwise
"""
try:
logger.debug("Checking collection existence for %s", collection_id)
await self.client.get_collection(collection_id, request)
logger.debug("Collection %s exists", collection_id)
return True
except Exception: # pylint: disable=broad-exception-caught
logger.debug("Collection %s does not exist or cannot be retrieved", collection_id)
return False
async def _get_item_from_collection(self, request: Request):
"""Get an item from the collection.
Args:
request (Request): The request object.
Returns:
Optional[Dict]: The item from the collection if found, else None.
"""
item_id = self.request_ids["item_id"]
collection_id = f"{self.request_ids['owner_id']}_{self.request_ids['collection_ids'][0]}"
try:
logger.debug("Retrieving item %s from collection %s", item_id, collection_id)
item = await self.client.get_item(item_id=item_id, collection_id=collection_id, request=request)
logger.info("Retrieved existing item %s from collection %s", item_id, collection_id)
return item
except NotFoundError:
logger.info(f"The item '{item_id}' does not exist in collection '{collection_id}'")
return None
except Exception as e: # pylint: disable=broad-exception-caught
logger.exception(f"Exception: {e}")
raise HTTPException(
detail=f"Exception when trying to get the item {item_id} from the collection '{collection_id}'",
status_code=HTTP_400_BAD_REQUEST,
) from e
async def build_filelist_to_be_deleted(self, request):
"""
Build the S3 cleanup list for DELETE requests.
Collection deletes require scanning all items in the collection, while
item deletes only need the requested item. The resulting list is deleted
before the request is forwarded to pgstac.
"""
logger.info(
"Building S3 deletion list for owner=%s collections=%s item=%s",
self.request_ids["owner_id"],
self.request_ids["collection_ids"],
self.request_ids["item_id"],
)
for ci in self.request_ids["collection_ids"]:
collection_id = f"{self.request_ids['owner_id']}_{ci}"
items = []
try:
if "/items" not in request.scope["path"]:
# this is the case for delete endpoint /collections/<collection_name>
# use pagination, otherwise a maximum of the default limit (10) items is returned
# NOTE: Unable to use the pagination from pgstac client. Temporary, use a limit of 100
token = None
while True:
items_collection = await self.client.item_collection(
request=request,
collection_id=collection_id,
limit=100,
token=token,
)
items.extend(items_collection.get("features", []))
logger.debug(
"Fetched %d item(s) for deletion scan from collection %s; token=%s",
len(items_collection.get("features", [])),
collection_id,
token,
)
# Check if there's a next token for pagination
token = get_token_for_pagination(items_collection)
if not token:
# No more pages left, break the loop
break
else:
# this is the case for delete endpoint /collections/<collection_name>/items/<item_name>
item = await self.client.get_item(
item_id=self.request_ids["item_id"],
collection_id=collection_id,
request=request,
)
items = [item]
except NotFoundError as nfe:
logger.error(f"Failed to find the requested object to be deleted. {nfe}")
return
except KeyError as e:
logger.error(f"Failed to build the list of items to be deleted due to missing key: {e}")
raise HTTPException(
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to prepare S3 cleanup; catalog metadata was not deleted.",
) from e
logger.debug(f"Found {len(items)} items: {items}")
try:
for item in items:
assets = item.get("assets", {})
for _, asset_info in assets.items():
s3_href = asset_info.get("href")
if s3_href:
self.s3_files_to_be_deleted.append(s3_href)
logger.debug("Scheduled S3 deletion for %s", s3_href)
except KeyError as e:
logger.error(
f"Failed to build the list of S3 files to be deleted due to missing key in dictionary: {e}",
)
raise HTTPException(
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to prepare S3 cleanup; catalog metadata was not deleted.",
) from e
logger.info(
"Successfully built the list of S3 files to be deleted. "
f"There are {len(self.s3_files_to_be_deleted)} files to be deleted",
)
async def manage_requests(self, request: Request) -> Request | Response:
"""
Dispatch catalog request pre-processing by method/path.
This is the main entry point used by CatalogMiddleware. It keeps the
endpoint-specific transformations isolated while returning either the
rewritten Request or an early Response when authorization fails.
Args:
request (Request): request received by the Catalog.
Returns:
Request|Response: Request processed to be sent to stac-fastapi OR a response if the operation
is not authorized
"""
logger.info(
"Managing catalog request %s %s; owner=%s, collections=%s, item=%s",
request.method,
request.scope["path"],
self.request_ids["owner_id"],
self.request_ids["collection_ids"],
self.request_ids["item_id"],
)
if request.method in ("POST", "PUT") and "/search" not in request.scope["path"]:
# URL: POST / PUT: '/catalog/collections/{USER}:{COLLECTION}'
# or '/catalog/collections/{USER}:{COLLECTION}/items'
request_or_response = await self.manage_put_post_request(request)
if hasattr(request_or_response, "status_code"): # Unauthorized
return cast(Response, request_or_response)
request = request_or_response
elif request.method == "DELETE":
if not await self.manage_delete_request(request):
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Deletion not allowed.")
elif "/search" in request.scope["path"]:
# URL: GET: '/catalog/search'
request_or_response = await self.manage_search_request(request)
if hasattr(request_or_response, "status_code"): # Unauthorized
return cast(Response, request_or_response)
request = request_or_response
elif request.method == "GET" and request.scope["path"] == CATALOG_COLLECTIONS:
# override default pgstac limit of 10 items if not explicitely set
if "limit" not in request.query_params:
request = self._override_request_query_string(request, {**request.query_params, "limit": 1000})
elif request.method == "PATCH":
request_or_response = await self.manage_patch_request(request)
if hasattr(request_or_response, "status_code"): # Unauthorized
return cast(Response, request_or_response)
request = request_or_response
return request
async def manage_put_post_request( # pylint: disable=too-many-statements,too-many-return-statements,too-many-branches # noqa: E501
self,
request: Request,
) -> Request | JSONResponse:
"""
Adapt POST/PUT request bodies for stac-fastapi.
Collection writes get owner-prefixed ids and timestamps. Item writes
validate authorization, collection existence, geometry/bbox consistency,
S3 availability and checksums before the request is forwarded.
Args:
request (Request): The Client request to be updated.
Returns:
Request: The request updated.
"""
try:
original_content = await request.json()
content = copy.deepcopy(original_content)
logger.info(
"Managing %s catalog write request for owner=%s collections=%s item=%s",
request.method,
self.request_ids["owner_id"],
self.request_ids["collection_ids"],
self.request_ids["item_id"],
)
logger.debug("Original catalog write content: %s", original_content)
check_user_authorization(self.request_ids)
logger.debug("Catalog write authorization succeeded for request ids %s", self.request_ids)
if len(self.request_ids["collection_ids"]) > 1:
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail="Cannot create or update more than one collection !",
)
if len(self.request_ids["collection_ids"]) == 0:
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail="Cannot create or update -> no collection specified !",
)
collection = self.request_ids["collection_ids"][0]
if (
# POST collection
request.scope["path"]
== CATALOG_COLLECTIONS
) or (
# PUT collection
request.scope["path"]
== f"{CATALOG_COLLECTIONS}/{self.request_ids['owner_id']}_{collection}"
):
# Manage a collection creation. The apikey user should be the same as the owner
# field in the body request. In other words, an apikey user cannot create a
# collection owned by another user.
# We don't care for local mode, any user may create / delete collection owned by another user
if common_settings.CLUSTER_MODE and (self.request_ids["owner_id"] != self.request_ids["user_login"]):
error = f"The '{self.request_ids['user_login']}' user cannot create a \
collection owned by the '{self.request_ids['owner_id']}' user. Additionally, modifying the 'owner' \
field is not permitted also."
raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail=error)
content["id"] = owner_id_and_collection_id(self.request_ids["owner_id"], content["id"])
if not content.get("owner"):
content["owner"] = self.request_ids["owner_id"]
logger.info("Preparing collection %s for catalog write", content["id"])
# See if there is already a collection with this ID. If yes, retrieve its "created" value.
try:
existing_collection = await self.client.get_collection(content["id"], request)
date_of_creation = existing_collection.get("created", "")
except Exception as e: # pylint: disable=broad-exception-caught
logger.debug("Collection %s doesn't exist and will be created: %s", content["id"], e)
date_of_creation = ""
# Update timestamps ("updated", and "created" if it's a new collection)
content = timestamps_extension.set_timestamps_to_collection(content, original_created=date_of_creation)
logger.debug(f"Handling for collection {content['id']}")
# TODO update the links also?
# The following section handles the request to create/update an item
elif "/items" in request.scope["path"]:
logger.info(
"Preparing item %s for publication/update in collection %s",
content.get("id"),
collection,
)
# first check if the collection exists
if not await self._collection_exists(request, f"{self.request_ids['owner_id']}_{collection}"):
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=f"Collection {collection} does not exist.",
)
# STAC-CORE-ITEM-REQ-0270 requires a product type on created and replaced items.
properties = content.get("properties")
product_type = properties.get("product:type") if isinstance(properties, dict) else None
if not isinstance(product_type, str) or not product_type.strip():
raise HTTPException(
status_code=HTTP_403_FORBIDDEN,
detail=(
"Cannot create or update item: "
"'product:type' must be a non-empty string in 'properties'."
),
)
# try to get the item if it is already part from the collection
item = await self._get_item_from_collection(request)
content = self.s3_manager(request).update_stac_item_publication(
content,
request,
self.request_ids,
item,
)
logger.debug(
"Checking if all item assets are available in S3 before allowing the publication of the item",
)
# Geometry checks and bbox enforcement are done before any S3 side effect.
logger.debug("Validating geometry/bbox for item %s", content.get("id"))
content = validate_geometry_and_enforce_bbox(content)
# Keep ESA behavior (accept null geometry+bbox) while ensuring pgstac persistence compatibility.
content = enforce_pgstac_defaults_for_null_geometry(content)
if not self.s3_manager(request).check_if_item_can_be_published(content):
logger.debug("The item cannot be published because some of its assets are not yet available in S3")
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail=f"Not all assets for item {content['id']} are available in S3.",
)
logger.debug("All assets of the item are available in S3, the item can be published or updated")
logger.info("All assets are available for catalog item %s", content.get("id"))
content = self.s3_manager(request).update_assets_checksums(content)
if content:
if request.method == "POST":
# Add UUID to the item's properties
if "externalIds" not in content.get("properties", {}):
content["properties"]["externalIds"] = []
else:
# If there is already an externalIds property, remove any "rs-odata-prip" UUID it contains
content["properties"]["externalIds"] = StacManager.remove_uuid_from_stac_asset_external_ids(
content["properties"]["externalIds"],
)
content["properties"]["externalIds"].append(StacManager.generate_uuid_for_stac_asset())
# Set timestamps for the new item
content = timestamps_extension.set_timestamps_for_creation(content)
content = timestamps_extension.set_timestamps_for_insertion(content)
logger.debug("Set creation/insertion timestamps for item %s", content.get("id"))
else: # PUT
published = ""
if item and item.get("properties"):
published = item["properties"].get("published", "")
logger.debug("Got published = %s", published)
if not published:
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail=f"Item {content['id']} not found.",
)
content = timestamps_extension.set_timestamps_for_update(
content,
original_published=published,
)
logger.debug("Set update timestamps for item %s", content.get("id"))
# Retrieve the existing UUID from the existing item and add it to the given item if any
content = StacManager.retrieve_existing_uuid_or_create_one(existing_item=item, new_item=content)
if hasattr(content, "status_code"):
return content
# update request body if needed
if content != original_content:
request = self._override_request_body(request, content)
logger.debug(f"Sending back the response for {request.method} {request.scope['path']}")
logger.info("Finished managing %s catalog write request for %s", request.method, request.scope["path"])
return request # pylint: disable=protected-access
except KeyError as kerr_msg:
logger.exception("Catalog write request is missing expected key: %s", kerr_msg)
raise HTTPException(
detail=f"Missing key in request body! {kerr_msg}",
status_code=HTTP_400_BAD_REQUEST,
) from kerr_msg
async def manage_delete_request(self, request: Request):
"""Check if the deletion is allowed.
Args:
request (Request): The client request.
Raises:
HTTPException: If the user is not authenticated.
Returns:
bool: Return True if the deletion is allowed, False otherwise.
"""
user_login = getpass.getuser()
auth_roles = []
if common_settings.CLUSTER_MODE: # Get the list of access and the user_login calling the endpoint.
auth_roles = request.state.auth_roles
user_login = request.state.user_login
logger.info(
"Managing delete request for owner=%s collections=%s item=%s as user=%s",
self.request_ids["owner_id"],
self.request_ids["collection_ids"],
self.request_ids["item_id"],
user_login,
)
if ( # If we are in cluster mode and the user_login is not authorized
# to this endpoint returns a HTTP_401_UNAUTHORIZED status.
common_settings.CLUSTER_MODE
and self.request_ids["collection_ids"]
and self.request_ids["owner_id"]
and not get_authorisation(
self.request_ids["collection_ids"],
auth_roles,
"write",
self.request_ids["owner_id"],
user_login,
)
):
logger.warning(
"Delete request denied by authorization; owner=%s collections=%s user=%s",
self.request_ids["owner_id"],
self.request_ids["collection_ids"],
user_login,
)
return False
# Manage a collection deletion. The apikey user (or local user if in local mode)
# should be the same as the owner field in the body request. In other words, the
# apikey user cannot delete a collection owned by another user
# we don't care for local mode, any user may create / delete collection owned by another user
if (
( # DELETE collection
request.scope["path"]
== f"{CATALOG_COLLECTIONS}/{self.request_ids['owner_id']}_{self.request_ids['collection_ids'][0]}"
)
and common_settings.CLUSTER_MODE
and (self.request_ids["owner_id"] != user_login)
):
logger.error(
f"The '{user_login}' user cannot delete a \
collection owned by the '{self.request_ids['owner_id']}' user",
)
return False
# Retry count excludes the initial attempt; range's upper bound is exclusive.
max_attempts = CATALOG_DELETE_MAX_RETRIES + 1
for attempt in range(1, max_attempts + 1):
# Rebuild from catalog metadata on every outer attempt. A previous
# S3 attempt may have deleted only part of a large asset prefix.
self.s3_files_to_be_deleted.clear()
await self.build_filelist_to_be_deleted(request)
try:
logger.info(
"Deleting %d S3 asset target(s) before catalog metadata for %s (outer attempt %d/%d)",
len(self.s3_files_to_be_deleted),
request.scope["path"],
attempt,
max_attempts,
)
await self.s3_manager(request).delete_s3_files(self.s3_files_to_be_deleted)
break
except RuntimeError as exc:
if attempt >= max_attempts:
logger.exception(
"S3 cleanup failed before catalog DELETE for %s after %d outer attempt(s)",
request.scope["path"],
max_attempts,
)
raise HTTPException(
status_code=HTTP_500_INTERNAL_SERVER_ERROR,
detail="Failed to delete S3 assets; catalog metadata was not deleted.",
) from exc
logger.warning(
"S3 cleanup attempt %d/%d failed for %s: %s. Rebuilding targets and retrying from scratch.",
attempt,
max_attempts,
request.scope["path"],
exc,
)
self.s3_files_to_be_deleted.clear()
logger.info("Delete request authorized for %s", request.scope["path"])
return True
async def manage_search_request( # pylint: disable=too-many-statements,too-many-branches
self,
request: Request,
) -> Request | JSONResponse:
"""
Normalize catalog search requests and resolve owner-prefixed collections.
The search endpoint accepts owner hints from URL path, query/body owner,
collections and CQL2 filters. This method resolves those hints, rewrites
collections to pgstac ids, translates `externalIds`, and checks read
authorization before forwarding the request.
Args:
request Request: the client request.
Returns:
Request: the new request with the collection name updated.
"""
# ---------- POST requests
if request.method == "POST":
content = await request.json()
original_content = copy.deepcopy(content)
logger.info("Managing POST catalog search request")
logger.debug("Original POST search body: %s", original_content)
# Pre-processing of filter extensions
if "filter" in content:
content["filter"] = process_filter_extensions(content["filter"])
logger.debug("Processed POST search filter extensions: %s", content["filter"])
# Management of priority for the assignation of the owner_id
if not self.request_ids["owner_id"]:
self.request_ids["owner_id"] = (
(extract_owner_name_from_json_filter(content["filter"]) if "filter" in content else None)
or content.get("owner")
or get_user(self.request_ids["owner_id"], self.request_ids["user_login"])
)
logger.debug("POST search owner resolved to %s", self.request_ids["owner_id"])
# Ensure normalized filters are serialized in request body.
# Add filter-lang option to the content if it doesn't already exist
if "filter" in content:
filter_lang = {"filter-lang": content.get("filter-lang", "cql2-json")}
stac_filter = content.pop("filter")
content = {
**content,
**filter_lang,
"filter": stac_filter,
} # The "filter_lang" field has to be placed BEFORE the filter.
# ----- Call /catalog/search with POST method endpoint
if "collections" in content:
# Check if each collection exist with their raw name, if not concatenate owner_id to the collection name
for i, collection in enumerate(content["collections"]):
normalized = collection.replace(":", "_")
owner_prefixed = f"{self.request_ids['owner_id']}_{normalized}"
if await self._collection_exists(request, normalized):
content["collections"][i] = normalized
elif await self._collection_exists(request, owner_prefixed):
content["collections"][i] = owner_prefixed
logger.debug(f"Using collection name: {content['collections'][i]}")
else:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=f"Collection {collection} not found.",
)
self.request_ids["collection_ids"] = content["collections"]
logger.info("POST search collections resolved to %s", self.request_ids["collection_ids"])
external_ids_filter = translate_external_ids_search(
content.get("filter"),
content.get("filter-lang", "cql2-json"),
content.pop("externalIds", None),
)
if external_ids_filter is not None:
content.pop("filter-lang", None)
content.pop("filter", None)
# The "filter-lang" field has to be placed BEFORE the filter.
content = {**content, "filter-lang": "cql2-json", "filter": external_ids_filter}
if content != original_content:
request = self._override_request_body(request, content)
# ---------- GET requests
elif request.method == "GET":
# Get dictionary of query parameters
query_params_dict = dict(request.query_params)
original_query_params = dict(query_params_dict)
logger.info("Managing GET catalog search request")
logger.debug("Original GET search query params: %s", original_query_params)
# Update owner_id if it is not already defined from path parameters
if not self.request_ids["owner_id"]:
self.request_ids["owner_id"] = (
(
extract_owner_name_from_text_filter(query_params_dict["filter"])
if "filter" in query_params_dict
else ""
)
or query_params_dict.get("owner")
or get_user(self.request_ids["owner_id"], self.request_ids["user_login"])
)
logger.debug("GET search owner resolved to %s", self.request_ids["owner_id"])
# ----- Catch endpoint catalog/search + query parameters (e.g. /search?ids=S3_OLC&collections=titi)
if "collections" in query_params_dict:
coll_list = query_params_dict["collections"].split(",")
# Check if each collection exist with their raw name, if not concatenate owner_id to the collection name
for i, collection in enumerate(coll_list):
# Handle case when user is specified in ?collections=user:collection
normalized = collection.replace(":", "_")
owner_prefixed = f"{self.request_ids['owner_id']}_{normalized}"
if await self._collection_exists(request, normalized):
coll_list[i] = normalized
elif await self._collection_exists(request, owner_prefixed):
coll_list[i] = owner_prefixed
logger.debug(f"Using collection name: {owner_prefixed}")
else:
raise HTTPException(
status_code=HTTP_404_NOT_FOUND,
detail=f"Collection {collection} not found.",
)
self.request_ids["collection_ids"] = coll_list
query_params_dict["collections"] = ",".join(coll_list)
logger.info("GET search collections resolved to %s", self.request_ids["collection_ids"])
external_ids_filter = translate_external_ids_search(
query_params_dict.get("filter"),
query_params_dict.get("filter-lang", "cql2-text"),
query_params_dict.pop("externalIds", None),
)
if external_ids_filter is not None:
query_params_dict["filter"] = json.dumps(external_ids_filter)
query_params_dict["filter-lang"] = "cql2-json"
if query_params_dict != original_query_params:
request = self._override_request_query_string(request, query_params_dict)
# Check that the collection from the request exists
for collection in self.request_ids["collection_ids"]:
if not await self._collection_exists(request, collection):
raise HTTPException(status_code=HTTP_404_NOT_FOUND, detail=f"Collection {collection} not found.")
logger.debug("Catalog search request ids after management: %s", self.request_ids)
# Check authorisation in cluster mode
if common_settings.CLUSTER_MODE:
get_authorisation(
self.request_ids["collection_ids"],
self.request_ids["auth_roles"],
"read",
self.request_ids["owner_id"],
self.request_ids["user_login"],
# When calling the /search endpoints, the catalog ids are always prefixed by their <owner>_
owner_prefix=True,
raise_if_unauthorized=True,
)
logger.info("Catalog search authorization succeeded for user %s", self.request_ids["user_login"])
return request
async def manage_patch_request(self, request: Request):
"""
Pre-processing of a PATCH request to the Catalog.
Checks authorization, validates and enriches patched assets, enforces
spatial consistency, and updates the `updated` timestamp.
Args:
request (Request): The request from the Client
Returns:
Request: Updated request
"""
try:
original_content = await request.json()
content = copy.deepcopy(original_content)
logger.info(
"Managing PATCH catalog request for owner=%s collections=%s item=%s",
self.request_ids["owner_id"],
self.request_ids["collection_ids"],
self.request_ids["item_id"],
)
logger.debug("Original PATCH body: %s", original_content)
check_user_authorization(self.request_ids)
logger.debug("PATCH authorization succeeded for request ids %s", self.request_ids)
is_item = "/items/" in request.scope["path"]
item = None
if is_item and (
"geometry" in content
or "bbox" in content
or "assets" in content
or "externalIds" in content.get("properties", {})
):
# PATCH validation needs the complete item context.
item = await self._get_item_from_collection(request)
if not item:
raise HTTPException(
status_code=HTTP_400_BAD_REQUEST,
detail=f"Item {self.request_ids['item_id']} not found.",
)
if is_item and ("geometry" in content or "bbox" in content):
# Merge patched geometry/bbox over current item, then validate the result.
logger.debug("Merging PATCH geometry/bbox over current item %s", self.request_ids["item_id"])
# The preceding lookup rejects missing items for any geometry/bbox PATCH.
merged_content = copy.deepcopy(cast(dict[str, Any], item))
if "geometry" in content:
merged_content["geometry"] = content["geometry"]
# Force bbox recomputation/removal according to the new geometry.
if "bbox" not in content:
merged_content["bbox"] = None
if "bbox" in content:
merged_content["bbox"] = content["bbox"]
merged_content = validate_geometry_and_enforce_bbox(merged_content)
# Keep ESA behavior for null geometry+bbox while making PATCH payload acceptable for pgstac.
merged_content = enforce_pgstac_defaults_for_null_geometry(merged_content)
# Propagate enforced geometry/bbox back to patch body so stored item stays consistent.
content["geometry"] = merged_content.get("geometry", None)
content["bbox"] = merged_content.get("bbox", None)
logger.debug(
"PATCH geometry/bbox enforced for item %s: geometry=%s bbox=%s",
self.request_ids["item_id"],
content.get("geometry"),
content.get("bbox"),
)
if is_item and isinstance(content.get("assets"), dict):
# Validate and enrich only assets explicitly present in this PATCH.
content["assets"] = self.s3_manager(request).update_patch_assets(item, content["assets"])
# Keep the existing UUID when externalIds are patched.
if is_item and "properties" in content and "externalIds" in content["properties"]:
external_ids = content["properties"]["externalIds"]
if isinstance(external_ids, list):
# From the PATCH request, keep the externalIds element EXCEPT the one with an UUID if any
external_ids = [
external_id
for external_id in external_ids
if not (isinstance(external_id, dict) and external_id.get("scheme") == "rs-odata-prip")
]
# From the existing item, retrieve the UUID if any and add it to the externalIds list
if item:
existing_external_ids = item.get("properties", {}).get("externalIds", [])
existing_uuid = next(
(
external_id
for external_id in existing_external_ids
if external_id.get("scheme") == "rs-odata-prip"
),
None,
)
if existing_uuid:
external_ids.append(existing_uuid)
content["properties"]["externalIds"] = external_ids
# Update "updated" timestamp (different field if it is an item or a collection)
if is_item:
# A valid partial item PATCH does not have to provide properties.
content.setdefault("properties", {})
content = timestamps_extension.set_updated_timestamp_to_now(content, is_item=is_item)
logger.debug("Updated PATCH timestamp for item=%s", is_item)
request = self._override_request_body(request, content)
logger.info("Finished managing PATCH request for %s", request.scope["path"])
return request
except KeyError as kerr_msg:
logger.exception("PATCH request is missing expected key: %s", kerr_msg)
raise HTTPException(
detail=f"Missing key in request body! {kerr_msg}",
status_code=HTTP_400_BAD_REQUEST,
) from kerr_msg