22
33import requests
44
5+ from .analytics import AnalyticsProcessor
6+
57logger = logging .getLogger (__name__ )
68
79SERVER_URL = "https://api.flagsmith.com/api/v1/"
@@ -19,12 +21,16 @@ def __init__(self, environment_id, api=SERVER_URL, request_timeout=None):
1921 :param api: (optional) api url to override when using self hosted version
2022 :param request_timeout: (optional) request timeout in seconds
2123 """
24+
2225 self .environment_id = environment_id
2326 self .api = api
2427 self .flags_endpoint = api + FLAGS_ENDPOINT
2528 self .identities_endpoint = api + IDENTITY_ENDPOINT
2629 self .traits_endpoint = api + TRAIT_ENDPOINT
2730 self .request_timeout = request_timeout
31+ self ._analytics_processor = AnalyticsProcessor (
32+ environment_id , api , self .request_timeout
33+ )
2834
2935 def get_flags (self , identity = None ):
3036 """
@@ -62,8 +68,9 @@ def has_feature(self, feature_name):
6268 :return: True if exists, False if not.
6369 """
6470 data = self ._get_flags_response (feature_name )
65-
6671 if data :
72+ feature_id = data ["feature" ]["id" ]
73+ self ._analytics_processor .track_feature (feature_id )
6774 return True
6875
6976 return False
@@ -72,7 +79,7 @@ def feature_enabled(self, feature_name, identity=None):
7279 """
7380 Get enabled state of given feature for an environment.
7481
75- :param feature_name: name of feature to determine if enabled (must match 'ID' on flagsmith.com)
82+ :param feature_name: name of feature to determine if enabled
7683 :param identity: (optional) application's unique identifier for the user to check feature state
7784 :return: True / False if feature exists. None otherwise.
7885 """
@@ -81,21 +88,19 @@ def feature_enabled(self, feature_name, identity=None):
8188
8289 data = self ._get_flags_response (feature_name , identity )
8390
84- if data :
85- if data .get ("flags" ):
86- for flag in data .get ("flags" ):
87- if flag ["feature" ]["name" ] == feature_name :
88- return flag ["enabled" ]
89- else :
90- return data ["enabled" ]
91- else :
91+ if not data :
9292 return None
9393
94+ feature_id = data ["feature" ]["id" ]
95+ self ._analytics_processor .track_feature (feature_id )
96+
97+ return data ["enabled" ]
98+
9499 def get_value (self , feature_name , identity = None ):
95100 """
96101 Get value of given feature for an environment.
97102
98- :param feature_name: name of feature to determine value of (must match 'ID' on flagsmith.com)
103+ :param feature_name: name of feature to determine value of
99104 :param identity: (optional) application's unique identifier for the user to check feature state
100105 :return: value of the feature state if feature exists, None otherwise
101106 """
@@ -104,17 +109,11 @@ def get_value(self, feature_name, identity=None):
104109
105110 data = self ._get_flags_response (feature_name , identity )
106111
107- if data :
108- # using new endpoints means that the flags come back in a list, filter this for the one we want and
109- # return it's value
110- if data .get ("flags" ):
111- for flag in data .get ("flags" ):
112- if flag ["feature" ]["name" ] == feature_name :
113- return flag ["feature_state_value" ]
114- else :
115- return data ["feature_state_value" ]
116- else :
112+ if not data :
117113 return None
114+ feature_id = data ["feature" ]["id" ]
115+ self ._analytics_processor .track_feature (feature_id )
116+ return data ["feature_state_value" ]
118117
119118 def get_trait (self , trait_key , identity ):
120119 """
0 commit comments