1111
1212from flagsmith .analytics import AnalyticsProcessor
1313from flagsmith .exceptions import FlagsmithAPIError , FlagsmithClientError
14- from flagsmith .models import Flags
14+ from flagsmith .models import DefaultFlag , Flags
1515from flagsmith .polling_manager import EnvironmentDataPollingManager
1616from flagsmith .utils .identities import generate_identities_data
1717
1818logger = logging .getLogger (__name__ )
1919
20- API_URL = "https://api.flagsmith.com/api/v1/"
21- FLAGS_ENDPOINT = "flags/"
22- IDENTITY_ENDPOINT = "identities/"
23- TRAITS_ENDPOINT = "traits/"
24-
25- # TODO:
26- # - defaults
20+ DEFAULT_API_URL = "https://api.flagsmith.com/api/v1/"
2721
2822
2923class Flagsmith :
3024 def __init__ (
3125 self ,
3226 environment_key : str ,
33- api_url : str = API_URL ,
27+ api_url : str = DEFAULT_API_URL ,
3428 custom_headers : typing .Dict [str , typing .Any ] = None ,
3529 request_timeout : int = None ,
3630 enable_client_side_evaluation : bool = False ,
3731 environment_refresh_interval_seconds : int = 60 ,
3832 retries : Retry = None ,
3933 enable_analytics : bool = False ,
34+ defaults : typing .List [DefaultFlag ] = None ,
4035 ):
4136 self .session = requests .Session ()
4237 self .session .headers .update (
@@ -70,6 +65,8 @@ def __init__(
7065 else None
7166 )
7267
68+ self .defaults = defaults or []
69+
7370 def get_environment_flags (self ) -> Flags :
7471 """
7572 Get all the default for flags for the current environment.
@@ -110,6 +107,7 @@ def _get_environment_flags_from_document(self) -> Flags:
110107 return Flags .from_feature_state_models (
111108 feature_states = engine .get_environment_feature_states (self ._environment ),
112109 analytics_processor = self ._analytics_processor ,
110+ defaults = self .defaults ,
113111 )
114112
115113 def _get_identity_flags_from_document (
@@ -123,12 +121,18 @@ def _get_identity_flags_from_document(
123121 feature_states = feature_states ,
124122 analytics_processor = self ._analytics_processor ,
125123 identity_id = identity_model .composite_key ,
124+ defaults = self .defaults ,
126125 )
127126
128127 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+
129132 return Flags .from_api_flags (
130- flags = self . _get_json_response ( url = self . environment_flags_url , method = "GET" ) ,
133+ api_flags = api_flags ,
131134 analytics_processor = self ._analytics_processor ,
135+ defaults = self .defaults ,
132136 )
133137
134138 def _get_identity_flags_from_api (
@@ -139,7 +143,9 @@ def _get_identity_flags_from_api(
139143 url = self .identities_url , method = "POST" , body = data
140144 )
141145 return Flags .from_api_flags (
142- flags = json_response ["flags" ], analytics_processor = self ._analytics_processor
146+ api_flags = json_response ["flags" ],
147+ analytics_processor = self ._analytics_processor ,
148+ defaults = self .defaults ,
143149 )
144150
145151 def _get_json_response (self , url : str , method : str , body : dict = None ):
0 commit comments