From f8aaa1e8120b8aa05871242bd3c6cff0c5d0d366 Mon Sep 17 00:00:00 2001 From: Kamil Monicz Date: Tue, 10 Jun 2025 08:18:57 +0000 Subject: [PATCH 1/2] Make orjson usage more consistent --- .../core/stac_fastapi/core/route_dependencies.py | 12 ++++++------ .../stac_fastapi/elasticsearch/database_logic.py | 6 +++--- .../stac_fastapi/opensearch/database_logic.py | 6 +++--- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/stac_fastapi/core/stac_fastapi/core/route_dependencies.py b/stac_fastapi/core/stac_fastapi/core/route_dependencies.py index 29dcc58b..fa5e4934 100644 --- a/stac_fastapi/core/stac_fastapi/core/route_dependencies.py +++ b/stac_fastapi/core/stac_fastapi/core/route_dependencies.py @@ -2,11 +2,11 @@ import importlib import inspect -import json import logging import os from typing import List +import orjson from fastapi import Depends from jsonschema import validate @@ -84,14 +84,14 @@ def get_route_dependencies_conf(route_dependencies_env: str) -> list: """Get Route dependencies configuration from file or environment variable.""" - if os.path.exists(route_dependencies_env): - with open(route_dependencies_env, encoding="utf-8") as route_dependencies_file: - route_dependencies_conf = json.load(route_dependencies_file) + if os.path.isfile(route_dependencies_env): + with open(route_dependencies_env, "rb") as f: + route_dependencies_conf = orjson.loads(f.read()) else: try: - route_dependencies_conf = json.loads(route_dependencies_env) - except json.JSONDecodeError as exception: + route_dependencies_conf = orjson.loads(route_dependencies_env) + except orjson.JSONDecodeError as exception: _LOGGER.error("Invalid JSON format for route dependencies. %s", exception) raise diff --git a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py index a1ca6250..743c1238 100644 --- a/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py +++ b/stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py @@ -1,7 +1,6 @@ """Database logic.""" import asyncio -import json import logging from base64 import urlsafe_b64decode, urlsafe_b64encode from copy import deepcopy @@ -9,6 +8,7 @@ import attr import elasticsearch.helpers as helpers +import orjson from elasticsearch.dsl import Q, Search from elasticsearch.exceptions import NotFoundError as ESNotFoundError from starlette.requests import Request @@ -527,7 +527,7 @@ async def execute_search( search_after = None if token: - search_after = json.loads(urlsafe_b64decode(token).decode()) + search_after = orjson.loads(urlsafe_b64decode(token)) query = search.query.to_dict() if search.query else None @@ -567,7 +567,7 @@ async def execute_search( next_token = None if len(hits) > limit and limit < max_result_window: if hits and (sort_array := hits[limit - 1].get("sort")): - next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode() + next_token = urlsafe_b64encode(orjson.dumps(sort_array)).decode() matched = ( es_response["hits"]["total"]["value"] diff --git a/stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py b/stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py index 88c7fcdc..66dc8d67 100644 --- a/stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py +++ b/stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py @@ -1,13 +1,13 @@ """Database logic.""" import asyncio -import json import logging from base64 import urlsafe_b64decode, urlsafe_b64encode from copy import deepcopy from typing import Any, Dict, Iterable, List, Optional, Tuple, Type, Union import attr +import orjson from opensearchpy import exceptions, helpers from opensearchpy.helpers.query import Q from opensearchpy.helpers.search import Search @@ -551,7 +551,7 @@ async def execute_search( search_after = None if token: - search_after = json.loads(urlsafe_b64decode(token).decode()) + search_after = orjson.loads(urlsafe_b64decode(token)) if search_after: search_body["search_after"] = search_after @@ -591,7 +591,7 @@ async def execute_search( next_token = None if len(hits) > limit and limit < max_result_window: if hits and (sort_array := hits[limit - 1].get("sort")): - next_token = urlsafe_b64encode(json.dumps(sort_array).encode()).decode() + next_token = urlsafe_b64encode(orjson.dumps(sort_array)).decode() matched = ( es_response["hits"]["total"]["value"] From 1ae4611a2552401a7a23aaae7ff056e775ed70c0 Mon Sep 17 00:00:00 2001 From: Kamil Monicz Date: Thu, 12 Jun 2025 13:15:03 +0000 Subject: [PATCH 2/2] Update CHANGELOG.md with: Make `orjson` usage more consistent --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index cafe4bcb..16f5ab46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Changed +- Make `orjson` usage more consistent [#402](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/402) - Improved datetime query handling to only check start and end datetime values when datetime is None [#396](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/396) - Optimize data_loader.py script [#395](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/395) - Refactored test configuration to use shared app config pattern [#399](https://github.com/stac-utils/stac-fastapi-elasticsearch-opensearch/pull/399)