7
7
from flag_engine .environments .builders import build_environment_model
8
8
from flag_engine .environments .models import EnvironmentModel
9
9
from flag_engine .identities .models import IdentityModel , TraitModel
10
- from requests .adapters import HTTPAdapter , Retry
10
+ from requests .adapters import HTTPAdapter
11
+ from urllib3 import Retry
11
12
12
13
from flagsmith .analytics import AnalyticsProcessor
13
14
from flagsmith .exceptions import FlagsmithAPIError , FlagsmithClientError
21
22
22
23
23
24
class Flagsmith :
25
+ """A Flagsmith client.
26
+
27
+ Provides an interface for interacting with the Flagsmith http API.
28
+
29
+ Basic Usage::
30
+
31
+ >>> from flagsmith import Flagsmith
32
+ >>> flagsmith = Flagsmith(environment_key="<your API key>")
33
+ >>> environment_flags = flagsmith.get_environment_flags()
34
+ >>> feature_enabled = environment_flags.is_feature_enabled("foo")
35
+ >>> identity_flags = flagsmith.get_identity_flags("identifier", {"foo": "bar"})
36
+ >>> feature_enabled_for_identity = identity_flags.is_feature_enabled("foo")
37
+ """
38
+
24
39
def __init__ (
25
40
self ,
26
41
environment_key : str ,
@@ -31,8 +46,26 @@ def __init__(
31
46
environment_refresh_interval_seconds : int = 60 ,
32
47
retries : Retry = None ,
33
48
enable_analytics : bool = False ,
34
- defaults : typing .List [ DefaultFlag ] = None ,
49
+ default_flag_handler : typing .Callable [[ str ], DefaultFlag ] = None ,
35
50
):
51
+ """
52
+ :param environment_key: The environment key obtained from Flagsmith interface
53
+ :param api_url: Override the URL of the Flagsmith API to communicate with
54
+ :param custom_headers: Additional headers to add to requests made to the
55
+ Flagsmith API
56
+ :param request_timeout_seconds: Number of seconds to wait for a request to
57
+ complete before terminating the request
58
+ :param enable_local_evaluation: Enables local evaluation of flags
59
+ :param environment_refresh_interval_seconds: If using local evaluation,
60
+ specify the interval period between refreshes of local environment data
61
+ :param retries: a urllib3.Retry object to use on all http requests to the
62
+ Flagsmith API
63
+ :param enable_analytics: if enabled, sends additional requests to the Flagsmith
64
+ API to power flag analytics charts
65
+ :param default_flag_handler: callable which will be used in the case where
66
+ flags cannot be retrieved from the API or a non existent feature is
67
+ requested
68
+ """
36
69
self .session = requests .Session ()
37
70
self .session .headers .update (
38
71
** {"X-Environment-Key" : environment_key }, ** (custom_headers or {})
@@ -65,7 +98,7 @@ def __init__(
65
98
else None
66
99
)
67
100
68
- self .defaults = defaults or []
101
+ self .default_flag_handler = default_flag_handler
69
102
70
103
def get_environment_flags (self ) -> Flags :
71
104
"""
@@ -107,7 +140,7 @@ def _get_environment_flags_from_document(self) -> Flags:
107
140
return Flags .from_feature_state_models (
108
141
feature_states = engine .get_environment_feature_states (self ._environment ),
109
142
analytics_processor = self ._analytics_processor ,
110
- defaults = self .defaults ,
143
+ default_flag_handler = self .default_flag_handler ,
111
144
)
112
145
113
146
def _get_identity_flags_from_document (
@@ -121,32 +154,41 @@ def _get_identity_flags_from_document(
121
154
feature_states = feature_states ,
122
155
analytics_processor = self ._analytics_processor ,
123
156
identity_id = identity_model .composite_key ,
124
- defaults = self .defaults ,
157
+ default_flag_handler = self .default_flag_handler ,
125
158
)
126
159
127
160
def _get_environment_flags_from_api (self ) -> Flags :
128
- api_flags = self ._get_json_response (
129
- url = self .environment_flags_url , method = "GET"
130
- )
131
-
132
- return Flags .from_api_flags (
133
- api_flags = api_flags ,
134
- analytics_processor = self ._analytics_processor ,
135
- defaults = self .defaults ,
136
- )
161
+ try :
162
+ api_flags = self ._get_json_response (
163
+ url = self .environment_flags_url , method = "GET"
164
+ )
165
+ return Flags .from_api_flags (
166
+ api_flags = api_flags ,
167
+ analytics_processor = self ._analytics_processor ,
168
+ default_flag_handler = self .default_flag_handler ,
169
+ )
170
+ except FlagsmithAPIError :
171
+ if self .default_flag_handler :
172
+ return Flags (default_flag_handler = self .default_flag_handler )
173
+ raise
137
174
138
175
def _get_identity_flags_from_api (
139
176
self , identifier : str , traits : typing .Dict [str , typing .Any ]
140
177
) -> Flags :
141
- data = generate_identities_data (identifier , traits )
142
- json_response = self ._get_json_response (
143
- url = self .identities_url , method = "POST" , body = data
144
- )
145
- return Flags .from_api_flags (
146
- api_flags = json_response ["flags" ],
147
- analytics_processor = self ._analytics_processor ,
148
- defaults = self .defaults ,
149
- )
178
+ try :
179
+ data = generate_identities_data (identifier , traits )
180
+ json_response = self ._get_json_response (
181
+ url = self .identities_url , method = "POST" , body = data
182
+ )
183
+ return Flags .from_api_flags (
184
+ api_flags = json_response ["flags" ],
185
+ analytics_processor = self ._analytics_processor ,
186
+ default_flag_handler = self .default_flag_handler ,
187
+ )
188
+ except FlagsmithAPIError :
189
+ if self .default_flag_handler :
190
+ return Flags (default_flag_handler = self .default_flag_handler )
191
+ raise
150
192
151
193
def _get_json_response (self , url : str , method : str , body : dict = None ):
152
194
try :
0 commit comments