Package: ffc-server-python-sdk
ffc-server-python-sdkIntroductionThis is the Python Server SDK for the feature management platformfeatureflag.co. It is
intended for use in a multiple-users python server applications.This SDK has two main purposes:Store the available feature flags and evaluate the feature flags by given user in the server side SDKSends feature flags usage, and custom events for the insights and A/B/n testing.Data synchonizationWe use websocket to make the local data synchronized with the server, and then store them in the memory by default.
Whenever there is any changes to a feature flag or his related data, the changes would be pushed to the SDK, the average
synchronization time is less than100ms. Be aware the websocket connection can be interrupted by any error or
internet interruption, but it would be restored automatically right after the problem is gone.Offline mode supportIn the offline mode, SDK DOES not exchange any data withfeatureflag.co, this mode is only use for internal test for instance.To open the offline mode:config=Config(env_secret,offline=True)Evaluation of a feature flagSDK will initialize all the related data(feature flags, segments etc.) in the bootstrapping and receive the data updates
in real time, as mentioned in the aboveAfter initialization, the SDK has all the feature flags in the memory and all evaluation is done locally and synchronously, the average evaluation time is <10ms.Installationinstall the sdk in using pip, this version of the SDK is compatible with Python 3.6 through 3.9.pip install ffc-server-python-sdkSDKApplications SHOULD instantiate a single instance for the lifetime of the application. In the case where an application
needs to evaluate feature flags from different environments, you may create multiple clients, but they should still be
retained for the lifetime of the application rather than created per request or per thread.BootstrappingThe bootstrapping is in fact the call of constructor ofFFCClient, in which the SDK will be initialized, using
streaming fromfeatureflag.co.The constructor will return when it successfully connects, or when the timeout(default: 15 seconds) expires, whichever comes first. If it has not succeeded in connecting when the timeout elapses, you will receive the client in an uninitialized state where feature flags will return default values; it will still continue trying to connect in the background unless there has been a network error or you close the client(usingstop()). You can detect whether initialization has succeeded by callinginitialize().The best way to use the SDK as a singleton, first make sure you have calledffcclient.set_config()at startup time. Thenffcclient.get()will return the same sharedffcclient.client.FFCClientinstance each time. The client will be initialized if it runs first time.fromffcclient.configimportConfigfromffcclientimportget,set_configset_config(Config(env_secret))client=get()ifclient.initialize:# your codeYou can also manage yourffcclient.client.FFCClient, the SDK will be initialized if you callffcclient.client.FFCClientconstructor.fromffcclient.configimportConfigfromffcclient.clientimportFFCClientclient=FFCClient(Config(env_secret),start_wait=15)ifclient.initialize:# your codeIf you prefer to have the constructor return immediately, and then wait for initialization to finish at some other
point, you can useffcclient.client.FFCClient.update_status_providerobject, which provides an asynchronous way, as follows:fromffcclient.configimportConfigfromffcclient.clientimportFFCClientclient=FFCClient(Config(env_secret),start_wait=0)ifclient._update_status_provider.wait_for_OKState():# your codeEvaluationSDK calculates the value of a feature flag for a given user, and returns a flag vlaue/an object that describes the way
that the value was determined.User: A dictionary of attributes that can affect flag evaluation, usually corresponding to a user of your application.
This object contains built-in properties(key,name,emailandcountry). The only mandatory property is the key,
which must uniquely identify each user; this could be a username or email address for authenticated users, or a ID for anonymous users.All other built-in properties are optional, it's strongly recommended to setnamein order to search your user quickly
You may also define custom properties with arbitrary names and values.ifclient.initialize:user={'key':user_key,'name':user_name,'age':age}flag_value=client.variation(flag_key,user)# your if/else code according to flag valueIf evaluation called before SDK client initialized or you set the wrong flag key or user for the evaluation, SDK will return
the default value you set. Theffcclient.common_types.FlagStatewill explain the details of the last evaluation including error raison.If you would like to get variations of all feature flags in a special environment, you can useffcclient.client.FFCClient.get_all_latest_flag_variations, SDK will returnffcclient.common_types.AllFlagStates, that explain the details of all feature flagsifclient.initialize:user={'key':user_key,'name':user_name}all_flag_values=client.get_all_latest_flag_variations(user)ed=all_flag_values.get(flag_key)flag_value=ed.variation# your if/else code according to flag valueExperiments (A/B/n Testing)We support automatic experiments for pageviews and clicks, you just need to set your experiment on our SaaS platform, then you should be able to see the result in near real time after the experiment is started.In case you need more control over the experiment data sent to our server, we offer a method to send custom event.client.track_metric(user,event_name,numeric_value);numeric_valueis not mandatory, the default value is1.Make suretrack_metricis called after the related feature flag is evaluated by simply callingvariationorvariation_detailotherwise, the custom event won't be included into the experiment result.