Skip to content

Commit 88ec723

Browse files
committed
test: add live targeting smoke script
1 parent 9048101 commit 88ec723

1 file changed

Lines changed: 61 additions & 0 deletions

File tree

‎scripts/live_targeting_check.py‎

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""Evaluate caller-supplied users against a live FeatBit flag.
2+
3+
Set FEATBIT_TARGET_USERS to a JSON array of FeatBit user dictionaries. The
4+
script does not print the environment secret or modify the supplied users.
5+
"""
6+
7+
import json
8+
import os
9+
import sys
10+
from pathlib import Path
11+
12+
ROOT = Path(__file__).resolve().parents[1]
13+
sys.path.insert(0, str(ROOT))
14+
15+
from fbclient.client import FBClient # noqa: E402
16+
from fbclient.config import Config # noqa: E402
17+
18+
19+
def main():
20+
secret = os.environ.get("FEATBIT_ENV_SECRET")
21+
users_json = os.environ.get("FEATBIT_TARGET_USERS")
22+
if not secret:
23+
raise SystemExit("FEATBIT_ENV_SECRET is required")
24+
if not users_json:
25+
raise SystemExit("FEATBIT_TARGET_USERS is required (JSON array)")
26+
27+
try:
28+
users = json.loads(users_json)
29+
except json.JSONDecodeError as exc:
30+
raise SystemExit("FEATBIT_TARGET_USERS must be valid JSON: %s" % exc)
31+
if not isinstance(users, list) or not users or not all(isinstance(user, dict) for user in users):
32+
raise SystemExit("FEATBIT_TARGET_USERS must be a non-empty JSON array of objects")
33+
34+
event_url = os.environ.get("FEATBIT_EVENT_URL", "https://app-eval.featbit.co")
35+
streaming_url = os.environ.get("FEATBIT_STREAMING_URL", "wss://app-eval.featbit.co")
36+
flag_key = os.environ.get("FEATBIT_FLAG_KEY", "python-app-release")
37+
timeout = float(os.environ.get("FEATBIT_START_WAIT_SECONDS", "15"))
38+
39+
client = FBClient(Config(secret, event_url=event_url, streaming_url=streaming_url),
40+
start_wait=timeout)
41+
try:
42+
if not client.initialize:
43+
raise SystemExit("FeatBit SDK did not become ready")
44+
evaluations = []
45+
for user in users:
46+
detail = client.variation_detail(flag_key, user, False)
47+
evaluations.append({
48+
"user_key": user.get("key"),
49+
"variation": detail.variation,
50+
"variation_id": detail.variation_id,
51+
"reason": detail.reason,
52+
})
53+
print(json.dumps({"flag_key": flag_key, "evaluations": evaluations},
54+
indent=2, sort_keys=True, default=str))
55+
finally:
56+
client.stop()
57+
client.stop()
58+
59+
60+
if __name__ == "__main__":
61+
main()

0 commit comments

Comments
 (0)