|
| 1 | + |
| 2 | +## Using the SDK |
| 3 | + |
| 4 | +To use the SDK, the Optimizely instance can be initialized in three different ways as per your requirement. |
| 5 | + |
| 6 | + 1. Initialize Optimizely with a datafile. This datafile will be used as ProjectConfig throughout the life of Optimizely instance. |
| 7 | + ~~~~~~~~~~~~ |
| 8 | + optimizely.Optimizely( |
| 9 | + datafile |
| 10 | + ) |
| 11 | + |
| 12 | + 2. Initialize Optimizely by providing an 'sdk_key'. This will initialize a PollingConfigManager that makes an HTTP GET request to the URL ( formed using your provided sdk key and the default datafile CDN url template) to asynchronously download the project datafile at regular intervals and update ProjectConfig when a new datafile is recieved. A hard-coded datafile can also be provided along with the sdk_key that will be used initially before any update. |
| 13 | + ~~~~~~~~~~~~ |
| 14 | + optimizely.Optimizely( |
| 15 | + datafile=None, |
| 16 | + sdk_key='put_your_sdk_key_here' |
| 17 | + ) |
| 18 | + |
| 19 | + 3. Initialize Optimizely by providing a Config Manager that implements a 'get_config' method.You may use our Polling Config Manager and customize it to your need. |
| 20 | + ~~~~~~~~~~~~ |
| 21 | + optimizely.Optimizely( |
| 22 | + config_manager=custom_config_manager |
| 23 | + ) |
| 24 | +
|
| 25 | +##### PollingConfigManager |
| 26 | +
|
| 27 | +The PollingConfigManager asynchronously polls for datafiles from a specified URL at regular intervals by making HTTP request. |
| 28 | +
|
| 29 | + polling_config_manager = PollingConfigManager( |
| 30 | + sdk_key=None, |
| 31 | + datafile=None, |
| 32 | + update_interval=None, |
| 33 | + url=None, |
| 34 | + url_template=None, |
| 35 | + logger=None, |
| 36 | + error_handler=None, |
| 37 | + notification_center=None, |
| 38 | + skip_json_validation=False |
| 39 | + ) |
| 40 | + |
| 41 | +**Note**: One of the sdk_key or url must be provided. When both are provided, url takes the preference. |
| 42 | +
|
| 43 | +**sdk_key** |
| 44 | +The sdk_key is used to compose the outbound HTTP request to the default datafile location on the Optimizely CDN. |
| 45 | +
|
| 46 | +**datafile** |
| 47 | +You can provide an initial datafile to bootstrap the `ProjectConfigManager` so that it can be used immediately. The initial datafile also serves as a fallback datafile if HTTP connection cannot be established. The initial datafile will be discarded after the first successful datafile poll. |
| 48 | +
|
| 49 | +**update_interval** |
| 50 | +The update_interval is used to specify a fixed delay in seconds between consecutive HTTP requests for the datafile. |
| 51 | +
|
| 52 | +**url_template** |
| 53 | +A string with placeholder `{sdk_key}` can be provided so that this template along with the provided sdk key is used to form the target URL. |
| 54 | +
|
| 55 | +You may also provide your own logger, error_handler or notification_center. |
| 56 | +
|
| 57 | +
|
| 58 | +###### Advanced configuration |
| 59 | +The following properties can be set to override the default configurations for PollingConfigManager. |
| 60 | +
|
| 61 | +| **PropertyName** | **Default Value** | **Description** |
| 62 | +| -- | -- | -- |
| 63 | +| update_interval | 5 minutes | Fixed delay between fetches for the datafile |
| 64 | +| sdk_key | None | Optimizely project SDK key |
| 65 | +| url | None | URL override location used to specify custom HTTP source for the Optimizely datafile. |
| 66 | +| url_template | 'https://cdn.optimizely.com/datafiles/{sdk_key}.json' | Parameterized datafile URL by SDK key. |
| 67 | +| datafile | None | Initial datafile, typically sourced from a local cached source. |
| 68 | +
|
| 69 | +A notification signal will be triggered whenever a _new_ datafile is fetched and Project Config is updated. To subscribe to these notifications you can use the `notification_center.add_notification_listener(NotificationTypes.OPTIMIZELY_CONFIG_UPDATE, update_callback)` |
| 70 | +
|
| 71 | + |
0 commit comments