Skip to content

Instantly share code, notes, and snippets.

@asaschachar
Forked from jo-migo/generate_fullstack_mvt.py
Last active October 17, 2017 16:29
Show Gist options
  • Save asaschachar/620d466361500dbdef15e00a0330ec38 to your computer and use it in GitHub Desktop.
Save asaschachar/620d466361500dbdef15e00a0330ec38 to your computer and use it in GitHub Desktop.
Script to Generate a Generic Fullstack MVT
import json
import os
import pprint
import requests
import sys
'''
DIRECTIONS:
- Create a feature flag in the UI, add feature variables with default values
- run `export FEATURE_FLAG_ID=<insert feature flag ID>` in the terminal (insert the ID of a real feature flag)
- run `export ACCESS_TOKEN=<insert personal access token>` in the terminal (go to Profile > API Access to generate a PAT if you don't have one already)
- run setup_script.sh
- If you don't have trailmix installed, you have to run `sudo pip install -i https://localshop.dz.optimizely.com/simple/ --upgrade --ignore-installed trailmix` first!!
- Run this script woOooo `python generate_fullstack_mvt.py` in the same terminal window you ran setup_script.sh in
'''
def make_request(method, url, headers, data=None):
response = requests.request(method=method, url=url, json=data, headers=headers, verify=False)
if response.ok:
return response.json()
print(response.reason)
print(response.text)
sys.exit(1)
base_url = os.environ['OPTLY_APP_URL'] or 'https://app.optimizely.test'
feature_flag_id = os.environ['FEATURE_FLAG_ID']
proxy_shared_secret = os.environ['PROXY_SHARED_SECRET']
headers = {'Authorization': 'Bearer {}'.format(os.environ['ACCESS_TOKEN']), 'X-3scale-proxy-secret-token': proxy_shared_secret}
feature_flag = make_request('GET', '{}/api/v1/feature_flags/{}'.format(base_url, feature_flag_id), headers=headers)
project_id = feature_flag['project_id']
# Create layer with multivariate type and full-factorial policy
layer_formdata = {
'policy': 'multivariate',
'multivariate_traffic_policy': 'full_factorial',
'name': 'Test MVT Layer',
'project_id': project_id,
}
headers.update({'Content-Type': 'application/json'})
mvt_layer = make_request('POST', '{}/api/v1/projects/{}/layers'.format(base_url, project_id), headers=headers, data=layer_formdata)
experiment_formdata ={
'layer_id': mvt_layer['id'],
# TODO: If you don't add 'feature_flag_id', generating the experiment section without explicitly giving 'feature_flag_id' enters infinite recursion.....
'feature_flag_id': feature_flag['id']
}
mvt_experiment = make_request('POST', '{}/api/v1/projects/{}/layer_experiments'.format(base_url, project_id), headers=headers, data=experiment_formdata)
# Get the feature variables on the feature flag
# For each, create a section with one variation (the variation will start with the feature variable and its default value)
sections = []
for feature_variable in feature_flag['variables']:
print('Generating default section for variable {}'.format(feature_variable['api_name']))
section_formdata = {
'name': 'Testing Variable {}'.format(feature_variable['api_name']),
'variable_id': feature_variable['id'],
'variations': [
{
'api_name': 'default_{}_variation'.format(feature_variable['api_name']),
'weight': 10000,
'variable_value': feature_variable['default_value']
}]
}
section = make_request('POST', '{}/api/v1/layer_experiments/{}/experiment_sections'.format(base_url, mvt_experiment['id']), headers=headers, data=section_formdata)
sections.append(section)
mvt_update = {'section_ids': [section['id'] for section in sections]}
updated_layer = make_request('PUT', '{}/api/v1/layers/{}'.format(base_url, mvt_layer['id']), headers=headers, data=mvt_update)
print('\n MVT LAYER:')
pprint.pprint(updated_layer)
print('\n SECTIONS:')
pprint.pprint(sections)
mvt_experiment_with_combos = make_request('GET', '{}/api/v1/layer_experiments/{}'.format(base_url, mvt_experiment['id']), headers=headers)
print('\n MVT EXPERIMENT:')
pprint.pprint(mvt_experiment_with_combos)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment