Skip to content

Commit

Permalink
🚑 Fix loading strinng env vars in SecretBytes using validator
Browse files Browse the repository at this point in the history
  • Loading branch information
lig committed Sep 4, 2023
1 parent 3770893 commit d9dfed8
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pydantic import FilePath, RedisDsn, SecretBytes
from pydantic import FilePath, RedisDsn, SecretBytes, validator
from pydantic_settings import BaseSettings

__all__ = 'Settings', 'log'
Expand All @@ -21,12 +21,16 @@ def load_cached(cls, **kwargs) -> 'Settings':
"""
global _SETTINGS_CACHE
if _SETTINGS_CACHE is None:
for str2bytes_kwarg in ('webhook_secret', 'marketplace_webhook_secret'):
if str2bytes_kwarg in kwargs and isinstance(kwargs[str2bytes_kwarg], str):
kwargs[str2bytes_kwarg] = kwargs[str2bytes_kwarg].encode()
_SETTINGS_CACHE = cls(**kwargs)
return _SETTINGS_CACHE

@validator('webhook_secret', 'marketplace_webhook_secret', pre=True)
@staticmethod
def str2bytes(value: str | bytes) -> bytes:
if isinstance(value, str):
value = value.encode()
return value


def log(msg: str) -> None:
print(msg, flush=True)

0 comments on commit d9dfed8

Please sign in to comment.