You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
install the sdk in using pip, this version of the SDK is compatible with Python 3.6 through 3.11.
22
+
23
+
```shell
24
+
pip install fb-python-sdk
27
25
```
28
26
29
-
##Evaluation of a feature flag
27
+
### Quick Start
30
28
31
-
SDK will initialize all the related data(feature flags, segments etc.) in the bootstrapping and receive the data updates
32
-
in real time, as mentioned in the above
29
+
> Note that the _**env_secret**_, _**streaming_url**_ and _**event_url**_ are required to initialize the SDK.
33
30
34
-
After initialization, the SDK has all the feature flags in the memory and all evaluation is done locally and synchronously, the average evaluation time is < **10** ms.
31
+
The following code demonstrates basic usage of the SDK.
35
32
36
-
## Installation
37
-
install the sdk in using pip, this version of the SDK is compatible with Python 3.6 through 3.10.
Applications **SHOULD instantiate a single FBClient instance** for the lifetime of the application. In the case where an application
46
63
needs to evaluate feature flags from different environments, you may create multiple clients, but they should still be
47
64
retained for the lifetime of the application rather than created per request or per thread.
48
65
49
-
### Bootstrapping
66
+
####Bootstrapping
50
67
51
-
The bootstrapping is in fact the call of constructor of `FFCClient`, in which the SDK will be initialized and connect to feature flag center
68
+
The bootstrapping is in fact the call of constructor of `FBClient`, in which the SDK will be initialized and connect to FeatBit.
52
69
53
70
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(using `stop()`). You can detect whether initialization has succeeded by calling `initialize()`.
54
71
55
-
The best way to use the SDK as a singleton, first make sure you have called `fbclient.set_config()` at startup time. Then `fbclient.get()` will return the same shared `fbclient.client.FFCClient` instance each time. The client will be initialized if it runs first time.
72
+
The best way to use the SDK as a singleton, first make sure you have called `fbclient.set_config()` at startup time. Then `fbclient.get()` will return the same shared `fbclient.client.FBClient` instance each time. The client will be initialized if it runs first time.
You can also manage your `fbclient.client.FBClient`, the SDK will be initialized if you call `fbclient.client.FBClient` constructor.
83
+
You can also manage your `fbclient.client.FBClient`, the SDK will be initialized if you call `fbclient.client.FBClient` constructor. With constructor, you can set the timeout for initialization, the default value is 15 seconds.
If you prefer to have the constructor return immediately, and then wait for initialization to finish at some other point, you can use `fbclient.client.fbclient.update_status_provider` object, which provides an asynchronous way, as follows:
if client.update_status_provider.wait_for_OKState():
101
+
# the client is ready
88
102
```
89
103
104
+
It's possible to set a timeout in seconds for the `wait_for_OKState` method. If the timeout is reached, the method will return `False` and the client will still be in an uninitialized state. If you do not specify a timeout, the method will wait indefinitely.
90
105
91
-
### Evaluation
92
106
93
-
SDK 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.
107
+
> To check if the client is ready is optional. Even if the client is not ready, you can still evaluate feature flags, but the default value will be returned if SDK is not yet initialized.
108
+
109
+
### FBUser
94
110
95
-
`User`: A dictionary of attributes that can affect flag evaluation, usually corresponding to a user of your application.
111
+
A dictionary of attributes that can affect flag evaluation, usually corresponding to a user of your application.
96
112
This object contains built-in properties(`key`, `name`). The `key` and `name` are required. The `key` must uniquely identify each user; this could be a username or email address for authenticated users, or a ID for anonymous users. The `name` is used to search your user quickly. You may also define custom properties with arbitrary names and values.
97
-
For instance, the custom key should be a string; the custom value should be a string or a number
113
+
For instance, the custom key should be a string; the custom value should be a string, number or boolean value
114
+
115
+
```python
116
+
user = {'key': user_key, 'name': user_name, 'age': age}
117
+
```
118
+
119
+
### Evaluation
120
+
121
+
SDK 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.
98
122
99
123
```python
100
124
if client.initialize:
101
125
user = {'key': user_key, 'name': user_name, 'age': age}
If evaluation called before SDK client initialized or you set the wrong flag key or user for the evaluation, SDK will return
107
-
the default value you set. The `fbclient.common_types.FlagState` will explain the details of the last evaluation including error raison.
108
131
109
-
If you would like to get variations of all feature flags in a special environment, you can use `fbclient.client.FBClient.get_all_latest_flag_variations`, SDK will return `fbclient.common_types.AllFlagStates`, that explain the details of all feature flags
132
+
If you would like to get variations of all feature flags in a special environment, you can use `fbclient.client.FBClient.get_all_latest_flag_variations`, SDK will return `fbclient.common_types.AllFlagStates`, that explain the details of all feature flags. `fbclient.common_types.AllFlagStates.get()` returns the detail of a given feature flag key.
> Note that if evaluation called before Go SDK client initialized, you set the wrong flag key/user for the evaluation or the related feature flag is not found, SDK will return the default value you set. The `fbclient.common_types.EvalDetail` will explain the details of the latest evaluation including error raison.
142
+
143
+
### Offline Mode
144
+
145
+
In some situations, you might want to stop making remote calls to FeatBit. Here is how:
When you put the SDK in offline mode, no insight message is sent to the server and all feature flag evaluations return
151
+
fallback values because there are no feature flags or segments available. If you want to use your own data source,
152
+
SDK allows users to populate feature flags and segments data from a JSON string. Here is an example: [fbclient_test_data.json](tests/fbclient_test_data.json).
153
+
154
+
The format of the data in flags and segments is defined by FeatBit and is subject to change. Rather than trying to construct these objects yourself, it's simpler to request existing flags directly from the FeatBit server in JSON format and use this output as the starting point for your file. Here's how:
155
+
156
+
```shell
157
+
# replace http://localhost:5100 with your evaluation server url
0 commit comments