Skip to content

Commit c7dede1

Browse files
authored
Merge branch 'main' into feature/saved-groups
2 parents 07a84c9 + 015ab58 commit c7dede1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

growthbook/growthbook.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,6 +1082,14 @@ async def _fetch_features_async(
10821082
return data
10831083

10841084

1085+
def startAutoRefresh(self, api_host, client_key, cb):
1086+
self.sse_client = self.sse_client or SSEClient(api_host=api_host, client_key=client_key, on_event=cb)
1087+
self.sse_client.connect()
1088+
1089+
def stopAutoRefresh(self):
1090+
self.sse_client.disconnect()
1091+
1092+
10851093
def startAutoRefresh(self, api_host, client_key, cb):
10861094
self.sse_client = self.sse_client or SSEClient(api_host=api_host, client_key=client_key, on_event=cb)
10871095
self.sse_client.connect()
@@ -1212,6 +1220,49 @@ def dispatch_sse_event(self, event_data):
12121220
self.features_event_handler(data)
12131221

12141222

1223+
def startAutoRefresh(self):
1224+
if not self._client_key:
1225+
raise ValueError("Must specify `client_key` to start features streaming")
1226+
1227+
feature_repo.startAutoRefresh(
1228+
api_host=self._api_host,
1229+
client_key=self._client_key,
1230+
cb=self.dispatch_sse_event
1231+
)
1232+
1233+
def stopAutoRefresh(self):
1234+
feature_repo.stopAutoRefresh()
1235+
1236+
def features_event_handler(self, features):
1237+
decoded = json.loads(features)
1238+
if not decoded:
1239+
return None
1240+
1241+
if "encryptedFeatures" in decoded:
1242+
if not self._decryption_key:
1243+
raise ValueError("Must specify decryption_key")
1244+
try:
1245+
decrypted = decrypt(decoded["encryptedFeatures"], self._decryption_key)
1246+
return json.loads(decrypted)
1247+
except Exception:
1248+
logger.warning(
1249+
"Failed to decrypt features from GrowthBook API response"
1250+
)
1251+
return None
1252+
elif "features" in decoded:
1253+
self.set_features(decoded["features"])
1254+
else:
1255+
logger.warning("GrowthBook API response missing features")
1256+
1257+
def dispatch_sse_event(self, event_data):
1258+
event_type = event_data['type']
1259+
data = event_data['data']
1260+
if event_type == 'features-updated':
1261+
self.load_features()
1262+
elif event_type == 'features':
1263+
self.features_event_handler(data)
1264+
1265+
12151266
def startAutoRefresh(self):
12161267
if not self._client_key:
12171268
raise ValueError("Must specify `client_key` to start features streaming")

0 commit comments

Comments
 (0)