Skip to content

Commit e5c50d1

Browse files
committed
Address latest comments
1 parent e0fc87d commit e5c50d1

File tree

3 files changed

+9
-23
lines changed

3 files changed

+9
-23
lines changed

src/dda/config/constants.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class AppEnvVars:
2222
VERBOSE = "DDA_VERBOSE"
2323
NO_DYNAMIC_DEPS = "DDA_NO_DYNAMIC_DEPS"
2424
TELEMETRY_API_KEY = "DDA_TELEMETRY_API_KEY"
25-
FEATURE_FLAGS_CLIENT_TOKEN = "DDA_FEATURE_FLAGS_CLIENT_TOKEN" # noqa: S105 This is not a hardcoded secret but the linter complains on it
25+
FEATURE_FLAGS_CLIENT_TOKEN = "DDA_FEATURE_FLAGS_CLIENT_TOKEN" # noqa: S105i
2626
TELEMETRY_USER_MACHINE_ID = "DDA_TELEMETRY_USER_MACHINE_ID"
2727
# https://no-color.org
2828
NO_COLOR = "NO_COLOR"

src/dda/feature_flags/client.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@
33
# SPDX-License-Identifier: MIT
44
from __future__ import annotations
55

6-
import json
76
from typing import TYPE_CHECKING, Any
87

9-
from httpx import HTTPError
10-
118
from dda._version import __version__
129

1310
if TYPE_CHECKING:
@@ -49,23 +46,15 @@ def _fetch_flags(
4946
if not self.__client_token:
5047
return {}
5148

49+
from httpx import HTTPError
50+
5251
# Build headers
5352
headers = {
5453
"Content-Type": "application/vnd.api+json",
5554
"dd-client-token": self.__client_token,
55+
"dd-application-id": self.__app_id,
5656
}
5757

58-
headers["dd-application-id"] = self.__app_id
59-
60-
# Stringify all targeting attributes
61-
stringified_attributes = {}
62-
if targeting_attributes:
63-
for key, value in targeting_attributes.items():
64-
if isinstance(value, str):
65-
stringified_attributes[key] = value
66-
else:
67-
stringified_attributes[key] = json.dumps(value)
68-
6958
# Build request payload (following JSON:API format)
7059
payload = {
7160
"data": {
@@ -80,7 +69,7 @@ def _fetch_flags(
8069
},
8170
"subject": {
8271
"targeting_key": targeting_key,
83-
"targeting_attributes": stringified_attributes,
72+
"targeting_attributes": targeting_attributes,
8473
},
8574
},
8675
},

src/dda/feature_flags/manager.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,14 @@ class FeatureFlagManager:
2828

2929
def __init__(self, app: Application) -> None:
3030
self.__app = app
31-
32-
self.__client = DatadogFeatureFlag(self.__client_token, self.__app)
33-
3431
# Manually implemented cache to avoid calling several time Feature flag backend on the same flag evaluation.
3532
# Cache key is a tuple of the flag, entity and scopes, to make it hashable.
3633
# For example after calling `enabled("test-flag", default=False, scopes={"user": "user1"}),
3734
# the cache will contain the result for the tuple ("test-flag", "entity", (("user", "user1"),)).
3835
self.__cache: dict[tuple[str, str, tuple[tuple[str, str], ...]], Any] = {}
3936

4037
@cached_property
41-
def __client_token(self) -> str | None:
38+
def __client(self) -> str | None:
4239
if running_in_ci(): # We do not support feature flags token retrieval in the CI yet.
4340
return None
4441

@@ -53,7 +50,7 @@ def __client_token(self) -> str | None:
5350
client_token = fetch_client_token()
5451
save_client_token(client_token)
5552

56-
return client_token
53+
return DatadogFeatureFlag(client_token, self.__app)
5754

5855
@property
5956
def __user(self) -> FeatureFlagUser:
@@ -68,8 +65,8 @@ def __get_entity(self) -> str:
6865
return self.__user.machine_id
6966

7067
def enabled(self, flag: str, *, default: bool = False, scopes: Optional[dict[str, str]] = None) -> bool:
71-
if not self.__client_token:
72-
self.__app.display_debug("No client token found")
68+
if self.__client is None:
69+
self.__app.display_debug("Feature flag client not initialized properly")
7370
return default
7471

7572
entity = self.__get_entity()

0 commit comments

Comments
 (0)