Skip to content

Commit f6c1aa0

Browse files
committed
use settings
1 parent ebac915 commit f6c1aa0

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ class ElasticsearchSettings(ApiSettings, ApiBaseSettings):
8686
indexed_fields: Set[str] = {"datetime"}
8787
enable_response_models: bool = False
8888
enable_direct_response: bool = get_bool_env("ENABLE_DIRECT_RESPONSE", default=False)
89+
raise_on_bulk_error: bool = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
8990

9091
@property
9192
def create_client(self):
@@ -106,6 +107,7 @@ class AsyncElasticsearchSettings(ApiSettings, ApiBaseSettings):
106107
indexed_fields: Set[str] = {"datetime"}
107108
enable_response_models: bool = False
108109
enable_direct_response: bool = get_bool_env("ENABLE_DIRECT_RESPONSE", default=False)
110+
raise_on_bulk_error: bool = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
109111

110112
@property
111113
def create_client(self):

stac_fastapi/elasticsearch/stac_fastapi/elasticsearch/database_logic.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
)
3232
from stac_fastapi.core.extensions import filter
3333
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
34-
from stac_fastapi.core.utilities import MAX_LIMIT, bbox2polygon, get_bool_env
34+
from stac_fastapi.core.utilities import MAX_LIMIT, bbox2polygon
3535
from stac_fastapi.elasticsearch.config import AsyncElasticsearchSettings
3636
from stac_fastapi.elasticsearch.config import (
3737
ElasticsearchSettings as SyncElasticsearchSettings,
@@ -128,8 +128,20 @@ async def delete_item_index(collection_id: str):
128128
class DatabaseLogic(BaseDatabaseLogic):
129129
"""Database logic."""
130130

131-
client = AsyncElasticsearchSettings().create_client
132-
sync_client = SyncElasticsearchSettings().create_client
131+
async_settings: AsyncElasticsearchSettings = attr.ib(
132+
factory=AsyncElasticsearchSettings
133+
)
134+
sync_settings: SyncElasticsearchSettings = attr.ib(
135+
factory=SyncElasticsearchSettings
136+
)
137+
138+
client = attr.ib(init=False)
139+
sync_client = attr.ib(init=False)
140+
141+
def __attrs_post_init__(self):
142+
"""Initialize clients after the class is instantiated."""
143+
self.client = self.async_settings.create_client
144+
self.sync_client = self.sync_settings.create_client
133145

134146
item_serializer: Type[ItemSerializer] = attr.ib(default=ItemSerializer)
135147
collection_serializer: Type[CollectionSerializer] = attr.ib(
@@ -767,7 +779,7 @@ async def bulk_async_prep_create_item(
767779
error_message = (
768780
f"Item {item['id']} in collection {item['collection']} already exists."
769781
)
770-
if get_bool_env("RAISE_ON_BULK_ERROR", default=False):
782+
if self.async_settings.raise_on_bulk_error:
771783
raise ConflictError(error_message)
772784
else:
773785
logger.warning(
@@ -818,7 +830,7 @@ def bulk_sync_prep_create_item(
818830
error_message = (
819831
f"Item {item['id']} in collection {item['collection']} already exists."
820832
)
821-
if get_bool_env("RAISE_ON_BULK_ERROR", default=False):
833+
if self.sync_settings.raise_on_bulk_error:
822834
raise ConflictError(error_message)
823835
else:
824836
logger.warning(
@@ -1047,7 +1059,7 @@ async def bulk_async(
10471059
The `mk_actions` function is called to generate a list of actions for the bulk insert. If `refresh` is set to True,
10481060
the index is refreshed after the bulk insert.
10491061
"""
1050-
raise_on_error = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
1062+
raise_on_error = self.async_settings.raise_on_bulk_error
10511063
success, errors = await helpers.async_bulk(
10521064
self.client,
10531065
mk_actions(collection_id, processed_items),
@@ -1081,7 +1093,7 @@ def bulk_sync(
10811093
completed. The `mk_actions` function is called to generate a list of actions for the bulk insert. If `refresh` is set to
10821094
True, the index is refreshed after the bulk insert.
10831095
"""
1084-
raise_on_error = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
1096+
raise_on_error = self.sync_settings.raise_on_bulk_error
10851097
success, errors = helpers.bulk(
10861098
self.sync_client,
10871099
mk_actions(collection_id, processed_items),

stac_fastapi/opensearch/stac_fastapi/opensearch/config.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class OpensearchSettings(ApiSettings, ApiBaseSettings):
8383
indexed_fields: Set[str] = {"datetime"}
8484
enable_response_models: bool = False
8585
enable_direct_response: bool = get_bool_env("ENABLE_DIRECT_RESPONSE", default=False)
86+
raise_on_bulk_error: bool = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
8687

8788
@property
8889
def create_client(self):
@@ -103,6 +104,7 @@ class AsyncOpensearchSettings(ApiSettings, ApiBaseSettings):
103104
indexed_fields: Set[str] = {"datetime"}
104105
enable_response_models: bool = False
105106
enable_direct_response: bool = get_bool_env("ENABLE_DIRECT_RESPONSE", default=False)
107+
raise_on_bulk_error: bool = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
106108

107109
@property
108110
def create_client(self):

stac_fastapi/opensearch/stac_fastapi/opensearch/database_logic.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
)
3232
from stac_fastapi.core.extensions import filter
3333
from stac_fastapi.core.serializers import CollectionSerializer, ItemSerializer
34-
from stac_fastapi.core.utilities import MAX_LIMIT, bbox2polygon, get_bool_env
34+
from stac_fastapi.core.utilities import MAX_LIMIT, bbox2polygon
3535
from stac_fastapi.opensearch.config import (
3636
AsyncOpensearchSettings as AsyncSearchSettings,
3737
)
@@ -143,8 +143,16 @@ async def delete_item_index(collection_id: str) -> None:
143143
class DatabaseLogic(BaseDatabaseLogic):
144144
"""Database logic."""
145145

146-
client = AsyncSearchSettings().create_client
147-
sync_client = SyncSearchSettings().create_client
146+
async_settings: AsyncSearchSettings = attr.ib(factory=AsyncSearchSettings)
147+
sync_settings: SyncSearchSettings = attr.ib(factory=SyncSearchSettings)
148+
149+
client = attr.ib(init=False)
150+
sync_client = attr.ib(init=False)
151+
152+
def __attrs_post_init__(self):
153+
"""Initialize clients after the class is instantiated."""
154+
self.client = self.async_settings.create_client
155+
self.sync_client = self.sync_settings.create_client
148156

149157
item_serializer: Type[ItemSerializer] = attr.ib(default=ItemSerializer)
150158
collection_serializer: Type[CollectionSerializer] = attr.ib(
@@ -791,7 +799,7 @@ async def bulk_async_prep_create_item(
791799
error_message = (
792800
f"Item {item['id']} in collection {item['collection']} already exists."
793801
)
794-
if get_bool_env("RAISE_ON_BULK_ERROR", default=False):
802+
if self.async_settings.raise_on_bulk_error:
795803
raise ConflictError(error_message)
796804
else:
797805
logger.warning(
@@ -841,7 +849,7 @@ def bulk_sync_prep_create_item(
841849
error_message = (
842850
f"Item {item['id']} in collection {item['collection']} already exists."
843851
)
844-
if get_bool_env("RAISE_ON_BULK_ERROR", default=False):
852+
if self.sync_settings.raise_on_bulk_error:
845853
raise ConflictError(error_message)
846854
else:
847855
logger.warning(
@@ -1070,7 +1078,7 @@ async def bulk_async(
10701078
The `mk_actions` function is called to generate a list of actions for the bulk insert. If `refresh` is set to True,
10711079
the index is refreshed after the bulk insert.
10721080
"""
1073-
raise_on_error = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
1081+
raise_on_error = self.async_settings.raise_on_bulk_error
10741082
success, errors = await helpers.async_bulk(
10751083
self.client,
10761084
mk_actions(collection_id, processed_items),
@@ -1104,7 +1112,7 @@ def bulk_sync(
11041112
completed. The `mk_actions` function is called to generate a list of actions for the bulk insert. If `refresh` is set to
11051113
True, the index is refreshed after the bulk insert.
11061114
"""
1107-
raise_on_error = get_bool_env("RAISE_ON_BULK_ERROR", default=False)
1115+
raise_on_error = self.sync_settings.raise_on_bulk_error
11081116
success, errors = helpers.bulk(
11091117
self.sync_client,
11101118
mk_actions(collection_id, processed_items),

0 commit comments

Comments
 (0)