Skip to content

Commit 17e7ef1

Browse files
committed
refactor: add SingletonSettings
1 parent 1d3e991 commit 17e7ef1

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

CHANGELOG.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
## [0.8.1](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.8.0...v0.8.1) (2023-12-30)
22

3-
43
### Bug Fixes
54

6-
* ensure that AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set when running CI ([3d788b0](https://github.com/FullStackWithLawrence/aws-openai/commit/3d788b0d72f327d9f4711586499807c06831f5db))
7-
* fix indentation error ([806865b](https://github.com/FullStackWithLawrence/aws-openai/commit/806865b2c5a0a250862b956319fe121ca452d423))
5+
- ensure that AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY are set when running CI ([3d788b0](https://github.com/FullStackWithLawrence/aws-openai/commit/3d788b0d72f327d9f4711586499807c06831f5db))
6+
- fix indentation error ([806865b](https://github.com/FullStackWithLawrence/aws-openai/commit/806865b2c5a0a250862b956319fe121ca452d423))
87

98
# [0.8.0](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.7.2...v0.8.0) (2023-12-30)
109

api/terraform/python/openai_api/common/conf.py

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
OpenAIAPIConfigurationError,
3838
OpenAIAPIValueError,
3939
)
40-
from pydantic import Field, SecretStr, ValidationInfo, field_validator
40+
from pydantic import Field, SecretStr, ValidationError, ValidationInfo, field_validator
4141
from pydantic_settings import BaseSettings
4242

4343

@@ -217,7 +217,7 @@ class Settings(BaseSettings):
217217
_initialized: bool = False
218218

219219
# pylint: disable=too-many-branches,too-many-statements
220-
def __init__(self, **data: Any):
220+
def __init__(self, **data: Any): # noqa: C901
221221
super().__init__(**data)
222222
if not Services.enabled(Services.AWS_CLI):
223223
self._initialized = True
@@ -240,14 +240,12 @@ def __init__(self, **data: Any):
240240
logger.info("running inside GitHub Actions")
241241
aws_access_key_id = os.environ.get("AWS_ACCESS_KEY_ID", None)
242242
aws_secret_access_key = os.environ.get("AWS_SECRET_ACCESS_KEY", None)
243-
if not aws_access_key_id or not aws_secret_access_key:
243+
if not aws_access_key_id or not aws_secret_access_key and not self.aws_profile:
244244
raise OpenAIAPIConfigurationError(
245245
"required environment variable(s) AWS_ACCESS_KEY_ID and/or AWS_SECRET_ACCESS_KEY not set"
246246
)
247-
self._aws_access_key_id_source = "environ"
248-
self._aws_secret_access_key_source = "environ"
249247
region_name = os.environ.get("AWS_REGION", None)
250-
if not region_name:
248+
if not region_name and not self.aws_profile:
251249
raise OpenAIAPIConfigurationError("required environment variable AWS_REGION not set")
252250
try:
253251
self._aws_session = boto3.Session(
@@ -259,11 +257,17 @@ def __init__(self, **data: Any):
259257
self._aws_access_key_id_source = "environ"
260258
self._aws_secret_access_key_source = "environ"
261259
except ProfileNotFound:
262-
logger.warning("aws_profile %s not found", self.aws_profile)
260+
# only log this if the aws_profile is set
261+
if self.aws_profile:
262+
logger.warning("aws_profile %s not found", self.aws_profile)
263263

264-
if self.aws_profile and self._aws_access_key_id_source == "unset":
264+
if self.aws_profile:
265265
self._aws_access_key_id_source = "aws_profile"
266266
self._aws_secret_access_key_source = "aws_profile"
267+
else:
268+
self._aws_access_key_id_source = "environ"
269+
self._aws_secret_access_key_source = "environ"
270+
267271
self._initialized = True
268272

269273
if not self.initialized:
@@ -392,6 +396,7 @@ def aws_auth(self) -> dict:
392396
}
393397
if self.init_info:
394398
retval["init_info"] = self.init_info
399+
return retval
395400

396401
@property
397402
def aws_session(self):
@@ -702,4 +707,25 @@ def check_pinecone_api_key(cls, v) -> SecretStr:
702707
return v
703708

704709

705-
settings = Settings()
710+
class SingletonSettings:
711+
"""Singleton for Settings"""
712+
713+
_instance = None
714+
715+
def __new__(cls):
716+
"""Create a new instance of Settings"""
717+
if cls._instance is None:
718+
cls._instance = super(SingletonSettings, cls).__new__(cls)
719+
try:
720+
cls._instance._settings = Settings()
721+
except ValidationError as e:
722+
raise OpenAIAPIConfigurationError("Invalid configuration: " + str(e)) from e
723+
return cls._instance
724+
725+
@property
726+
def settings(self):
727+
"""Return the settings"""
728+
return self._settings
729+
730+
731+
settings = SingletonSettings().settings

0 commit comments

Comments
 (0)