-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement environment-variable based config (#62)
Signed-off-by: Federico Bond <[email protected]>
- Loading branch information
1 parent
7a7210f
commit a8b78b2
Showing
4 changed files
with
73 additions
and
16 deletions.
There are no files selected for viewing
31 changes: 31 additions & 0 deletions
31
providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/config.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import os | ||
import typing | ||
|
||
|
||
def str_to_bool(val: str) -> bool: | ||
return val.lower() == "true" | ||
|
||
|
||
def env_or_default(env_var, default, cast=None): | ||
val = os.environ.get(env_var) | ||
if val is None: | ||
return default | ||
return val if cast is None else cast(val) | ||
|
||
|
||
class Config: | ||
def __init__( | ||
self, | ||
host: typing.Optional[str] = None, | ||
port: typing.Optional[str] = None, | ||
tls: typing.Optional[bool] = None, | ||
timeout: typing.Optional[int] = None, | ||
): | ||
self.host = env_or_default("FLAGD_HOST", "localhost") if host is None else host | ||
self.port = ( | ||
env_or_default("FLAGD_PORT", 8013, cast=int) if port is None else port | ||
) | ||
self.tls = ( | ||
env_or_default("FLAGD_TLS", False, cast=str_to_bool) if tls is None else tls | ||
) | ||
self.timeout = 5 if timeout is None else timeout |
5 changes: 0 additions & 5 deletions
5
providers/openfeature-provider-flagd/src/openfeature/contrib/provider/flagd/defaults.py
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from openfeature.contrib.provider.flagd.config import Config | ||
|
||
|
||
def test_return_default_values(): | ||
config = Config() | ||
assert config.host == "localhost" | ||
assert config.port == 8013 | ||
assert config.tls is False | ||
assert config.timeout == 5 | ||
|
||
|
||
def test_overrides_defaults_with_environment(monkeypatch): | ||
monkeypatch.setenv("FLAGD_HOST", "flagd") | ||
monkeypatch.setenv("FLAGD_PORT", "1234") | ||
monkeypatch.setenv("FLAGD_TLS", "true") | ||
|
||
config = Config() | ||
assert config.host == "flagd" | ||
assert config.port == 1234 | ||
assert config.tls is True | ||
|
||
|
||
def test_uses_arguments_over_environments_and_defaults(monkeypatch): | ||
monkeypatch.setenv("FLAGD_HOST", "flagd") | ||
|
||
config = Config(host="flagd2", port=12345, tls=True) | ||
assert config.host == "flagd2" | ||
assert config.port == 12345 | ||
assert config.tls is True |