55import atexit
66import hashlib
77
8+ import requests
89from dateutil .tz import tzutc
910from six import string_types
1011
1112from posthog .utils import guess_timezone , clean
1213from posthog .consumer import Consumer
13- from posthog .request import post , get , APIError
14+ from posthog .request import batch_post , decide , get , APIError
1415from posthog .version import VERSION
1516from posthog .poller import Poller
1617
@@ -188,7 +189,7 @@ def _enqueue(self, msg):
188189
189190 if self .sync_mode :
190191 self .log .debug ('enqueued with blocking %s.' , msg ['event' ])
191- post (self .api_key , self .host , gzip = self .gzip ,
192+ batch_post (self .api_key , self .host , gzip = self .gzip ,
192193 timeout = self .timeout , batch = [msg ])
193194
194195 return True , msg
@@ -256,7 +257,6 @@ def load_feature_flags(self):
256257 def feature_enabled (self , key , distinct_id , default = False ):
257258 require ('key' , key , string_types )
258259 require ('distinct_id' , distinct_id , ID_TYPES )
259- error = False
260260
261261 if not self .personal_api_key :
262262 self .log .warning ('[FEATURE FLAGS] You have to specify a personal_api_key to use feature flags.' )
@@ -266,7 +266,6 @@ def feature_enabled(self, key, distinct_id, default=False):
266266 # If loading in previous line failed
267267 if not self .feature_flags :
268268 response = default
269- error = True
270269 else :
271270 try :
272271 feature_flag = [flag for flag in self .feature_flags if flag ['key' ] == key ][0 ]
@@ -277,13 +276,16 @@ def feature_enabled(self, key, distinct_id, default=False):
277276 response = _hash (key , distinct_id ) <= (feature_flag ['rollout_percentage' ] / 100 )
278277 else :
279278 try :
280- request = get (self .api_key , '/decide/' , self .host , timeout = 1 )
281- response = key in request ['featureFlags' ]
279+ request_data = {
280+ "distinct_id" : distinct_id ,
281+ "personal_api_key" : self .personal_api_key ,
282+ }
283+ resp_data = decide (self .api_key , self .host , timeout = 10 , ** request_data )
284+ response = key in resp_data ['featureFlags' ]
282285 except Exception as e :
283286 response = default
284287 self .log .warning ('[FEATURE FLAGS] Unable to get data for flag %s, because of the following error:' % key )
285288 self .log .warning (e )
286- error = True
287289
288290 self .capture (distinct_id , '$feature_flag_called' , {'$feature_flag' : key , '$feature_flag_response' : response })
289291 return response
0 commit comments