import os import time import requests class SplitIoClient: """API Client for split.io.""" def __init__(self): """Initialize split.io class.""" self.features = {} self.base_url = 'https://api.split.io/internal/api/v2' self.split_id_sdk_key = os.environ.get('SPLIT_ID_SDK_KEY') self.headers = self.set_headers() self.workspace_id = self.set_workspaces() self.environment_id = self.set_environments() def set_headers(self): split_headers = {'Content-Type': 'application/json', 'Authorization': 'Bearer {}'.format( self.split_id_sdk_key) } return split_headers def set_workspaces(self): endpoint = '{}/workspaces'.format(self.base_url) response = requests.get(endpoint, headers=self.headers) workspace_id = response.json()['objects'][0]['id'] return workspace_id def set_environments(self): endpoint = '{}/environments/ws/{}'.format( self.base_url, self.workspace_id) response = requests.get(endpoint, headers=self.headers) environment_id = response.json()[1]['id'] return environment_id def add_segment_to_split(self, split): body = [ { 'op': 'add', 'path': '/rules/0', 'value': { 'buckets': [ { 'treatment': 'on', 'size': 100 } ], 'condition': { 'matchers': [ { 'type': 'IN_SEGMENT', 'string': 'cucumber_test_segment' } ] } } } ] endpoint = '{}/splits/ws/{}/{}/environments/QA'.format( self.base_url, self.workspace_id, split) response = requests.patch(endpoint, headers=self.headers, json=body) print(response.json()) print('Segment "cucumber_test_segment" added to split {}' .format(split)) def add_user_to_segment_and_split(self, user, split): body = { 'keys': [ user ] } endpoint = '{}/segments/{}/cucumber_test_segment/uploadKeys'.format( self.base_url, self.environment_id) response = requests.patch(endpoint, headers=self.headers, json=body) print(response.json()) self.add_segment_to_split(split) def add_user_to_split(self, user, split): body = [ { 'op': 'add', 'path': '/rules/0', 'value': { 'buckets': [ { 'treatment': 'on', 'size': 100 } ], 'condition': { 'matchers': [ { 'type': 'IN_LIST_STRING', 'attribute': 'user_id', 'strings': [ str(user) ] } ] } } } ] endpoint = '{}/splits/ws/{}/{}/environments/QA'.format( self.base_url, self.workspace_id, split) response = requests.patch(endpoint, headers=self.headers, json=body) print(response.json()) print('User {} added to {}'.format(str(user), split)) time.sleep(9) def remove_user_from_split(self, user, split): body = [ { 'op': 'add', 'path': '/rules/0', 'value': { 'buckets': [ { 'treatment': 'off', 'size': 100 } ], 'condition': { 'matchers': [ { 'type': 'IN_LIST_STRING', 'attribute': 'user_id', 'strings': [ str(user) ] } ] } } } ] endpoint = '{}/splits/ws/{}/{}/environments/QA'.format( self.base_url, self.workspace_id, split) response = requests.patch(endpoint, headers=self.headers, json=body) print(response.json()) print('User {} removed from {}'.format(str(user), split)) time.sleep(9) def remove_rule_from_split(self, split): body = [ { 'op': 'remove', 'path': '/rules/0' } ] endpoint = '{}/splits/ws/{}/{}/environments/QA'.format( self.base_url, self.workspace_id, split) response = requests.patch(endpoint, headers=self.headers, json=body) print(response.json()) time.sleep(9)