1- # Split Python API
1+ # Split Python Admin API
22
33This API wrapper is designed to work with [ Split] ( https://www.split.io ) , the platform for controlled rollouts, serving features to your users via the Split feature flag to manage your complete customer experience.
44
@@ -7,55 +7,85 @@ This API wrapper is designed to work with [Split](https://www.split.io), the pla
77For specific instructions on how to use this API refer to our [ official API documentation] ( https://docs.split.io/reference ) .
88
99Install the split-admin-api:
10+
1011` pip install split-admin-api `
1112
1213Import the client object and initialize connection using an Admin API Key:
13- `from splitapiclient.main import get_client
14+
15+ ```
16+ from splitapiclient.main import get_client
1417client = get_client({'apikey': 'ADMIN API KEY'})
15- `
18+ ```
1619Enable optional logging:
17- `import logging
20+
21+ ```
22+ import logging
1823logging.basicConfig(level=logging.DEBUG)
19- `
24+ ```
25+
2026** Workspaces**
27+
2128Fetch all workspaces:
22- `for ws in client.workspaces.list():
29+
30+ ```
31+ for ws in client.workspaces.list():
2332 print ("\nWorkspace:"+ws.name+", Id: "+ws.id)
24- `
25- Find a specific workspaces:
26- `ws = client.workspaces.find("Defaults")
33+ ```
34+
35+ Find a specific workspace:
36+
37+ ```
38+ ws = client.workspaces.find("Defaults")
2739print ("\nWorkspace:"+ws.name+", Id: "+ws.id)
28- `
40+ ```
41+
2942** Environments**
43+
3044Fetch all Environments:
31- `ws = client.workspaces.find("Defaults")
45+
46+ ```
47+ ws = client.workspaces.find("Defaults")
3248for env in client.environments.list(ws.id):
3349 print ("\nEnvironment: "+env.name+", Id: "+env.id)
50+ ```
51+
3452Add new environment:
35- `ws = client.workspaces.find("Defaults")
53+
54+ ```
55+ ws = client.workspaces.find("Defaults")
3656env = ws.add_environment({'name':'new_environment', 'production':False})
37- `
57+ ```
3858
3959** Splits**
60+
4061Fetch all Splits:
41- `ws = client.workspaces.find("Defaults")
62+
63+ ```
64+ ws = client.workspaces.find("Defaults")
4265for split in client.splits.list(ws.id):
4366 print ("\nSplit: "+split.name+", "+split._workspace_id)
44- `
67+ ```
68+
4569Add new Split:
46- `
70+
71+ ```
4772split = ws.add_split({'name':'split_name', 'description':'new UI feature'}, "user")
4873print(split.name)
49- `
74+ ```
75+
5076Add tags:
51- `
77+
78+ ```
5279split.associate_tags(['my_new_tag', 'another_new_tag'])
53- `
80+ ```
81+
5482Add Split to environment:
55- `
83+
84+ ```
5685ws = client.workspaces.find("Defaults")
5786split = client.splits.find("new_feature", ws.id)
5887env = client.environments.find("Production", ws.id)
88+
5989tr1 = treatment.Treatment({"name":"on","configurations":""})
6090tr2 = treatment.Treatment({"name":"off","configurations":""})
6191bk1 = bucket.Bucket({"treatment":"on","size":50})
@@ -65,106 +95,202 @@ cond = condition.Condition({'matchers':[match.export_dict()]})
6595rl = rule.Rule({'condition':cond.export_dict(), 'buckets':[bk1.export_dict(), bk2.export_dict()]})
6696defrl = default_rule.DefaultRule({"treatment":"off","size":100})
6797data={"treatments":[tr1.export_dict() ,tr2.export_dict()],"defaultTreatment":"off", "baselineTreatment": "off","rules":[rl.export_dict()],"defaultRule":[defrl.export_dict()], "comment": "adding split to production"}
98+
6899splitdef = split.add_to_environment(env.id, data)
69- `
100+ ```
101+
70102Kill Split:
71- `
103+
104+ ```
72105ws = client.workspaces.find("Defaults")
73106env = client.environments.find("Production", ws.id)
74107splitDef = client.split_definitions.find("new_feature", env.id, ws.id)
75108splitDef.kill()
76- `
109+ ```
110+
77111Restore Split:
78- `
112+
113+ ```
79114splitDef.restore()
80- `
115+ ```
116+
81117Fetch all Splits in an Environment:
82- `
118+
119+ ```
83120ws = client.workspaces.find("Defaults")
84121env = client.environments.find("Production", ws.id)
85122for spDef in client.split_definitions.list(env.id, ws.id):
86123 print(spDef.name+str(spDef._default_rule))
87- `
124+ ```
125+
88126Submit a Change request to update a Split definition:
89- `
127+
128+ ```
90129splitDef = client.split_definitions.find("new_feature", env.id, ws.id)
91130definition= {"treatments":[ {"name":"on"},{"name":"off"}],
92131 "defaultTreatment":"off", "baselineTreatment": "off",
93132 "rules": [],
94133 "defaultRule":[{"treatment":"off","size":100}],"comment": "updating default rule"
95134}
96135splitDef.submit_change_request(definition, 'UPDATE', 'updating default rule', 'comment', ['[email protected] '], '') 97- `
136+ ```
137+
98138List all change requests:
99- `
139+
140+ ```
100141for cr in client.change_requests.list():
101142 if cr._split is not None:
102143 print (cr._id+", "+cr._split['name']+", "+cr._title+", "+str(cr._split['environment']['id']))
103144 if cr._segment is not None:
104145 print (cr._id+", "+cr._segment['name']+", "+cr._title)
105- `
146+ ```
147+
106148Approve specific change request:
107- `
149+
150+ ```
108151for cr in client.change_requests.list():
109152 if cr._split['name']=='new_feature':
110153 cr.update_status("APPROVED", "done")
111- `
154+ ```
155+
156+ ** Segments**
157+
158+ Fetch all Segments:
159+
160+ ```
161+ ws = client.workspaces.find("Defaults")
162+ for seg in client.segments.list(ws.id):
163+ print (seg.name)
164+ ```
165+
166+ Fecth all Segments in an Environment:
167+
168+ ```
169+ ws = client.workspaces.find("Defaults")
170+ env = client.environments.find("Production", ws.id)
171+ for segDef in client.segment_definitions.list(env.id, ws.id):
172+ print(segDef.name+", "+segDef._trafficType.name)
173+
174+ ```
175+
176+ Add new Segment:
177+
178+ ```
179+ ws = client.workspaces.find("Defaults")
180+ seg = ws.add_segment({'name':'segment_from_python', 'description':'users'}, "user")
181+ ```
182+
183+ Add Segment to an Environment:
184+
185+ ```
186+ ws = client.workspaces.find("Defaults")
187+ env = client.environments.find("Production", ws.id)
188+ seg = client.segments.find("admin_api_test",ws.id)
189+ segDef = seg.add_to_environment(env.id)
190+ ```
191+
192+ Add Segment Keys:
193+
194+ ```
195+ ws = client.workspaces.find("Defaults")
196+ env = client.environments.find("Production", ws.id)
197+ segDef = client.segment_definitions.find("admin_api_test", env.id, ws.id)
198+ segDef.import_keys_from_json("false", {"keys":["id4", "id5", "id6"], "comment":"a comment"})
199+ ```
200+
201+ List Segment Keys:
202+
203+ ```
204+ ws = client.workspaces.find("Defaults")
205+ env = client.environments.find("Production", ws.id)
206+ segDef = client.segment_definitions.find("admin_api_test", env.id, ws.id)
207+ for key in segDef.get_keys():
208+ print(key)
209+ ```
210+
211+ Export Segment Keys to csv:
212+
213+ ```
214+ ws = client.workspaces.find("Defaults")
215+ env = client.environments.find("Production", ws.id)
216+ segDef = client.segment_definitions.find("admin_api_test", env.id, ws.id)
217+ segDef.export_keys_to_csv("seg.csv")
218+ ```
219+
112220** Users and Groups**
221+
113222Fetch all Active users:
114- `
223+
224+ ```
115225for user in client.users.list('ACTIVE'):
116226 print(user.email+", "+user._id)
117- `
227+ ```
228+
118229Invite new user:
119- `
230+
231+ ```
120232group = client.groups.find('Administrators')
121233userData = {'email':'[email protected] ', 'groups':[{'id':'', 'type':'group'}]} 122234userData['groups'][0]['id'] = group._id
123235client.users.invite_user(userData)
124- `
236+ ```
237+
125238Delete a pending invite:
126- `
239+
240+ ```
127241for user in client.users.list('PENDING'):
128242 print(user.email+", "+user._id+", "+user._status)
129243 if user.email == '[email protected] ': 130244 client.users.delete(user._id)
131- `
245+ ```
246+
132247Update user info:
133- `
248+
249+ ```
134250data = {'name':'new_name', 'email':'[email protected] ', '2fa':False, 'status':'ACTIVE'} 135251user = client.users.find('[email protected] ') 136252user.update_user(user._id, data)
137- `
253+ ```
254+
138255Fetch all Groups:
139- `
256+
257+ ```
140258for group in client.groups.list():
141259 print (group._id+", "+group._name)
142- `
260+ ```
261+
143262Create Group:
144- `
263+
264+ ```
145265client.groups.create_group({'name':'new_group', 'description':''})
146- `
266+ ```
267+
147268Delete Group:
148- `
269+
270+ ```
149271group = client.groups.find('new_group')
150272client.groups.delete_group(group._id)
151- `
273+ ```
274+
152275Replace existing user group:
153- `
276+
277+ ```
154278user = client.users.find('[email protected] ') 155279group = client.groups.find('Administrators')
156280data = [{'op': 'replace', 'path': '/groups/0', 'value': {'id': '<groupId>', 'type':'group'}}]
157281data[0]['value']['id'] = group._id
158282user.update_user_group(data)
159- `
283+ ```
284+
160285Add user to new group
161- `
286+
287+ ```
162288user = client.users.find('[email protected] ') 163289group = client.groups.find('Administrators')
164290data = [{'op': 'add', 'path': '/groups/-', 'value': {'id': '<groupId>', 'type':'group'}}]
165291data[0]['value']['id'] = group._id
166292user.update_user_group(data)
167- `
293+ ```
168294
169295### Commitment to Quality:
170296
@@ -185,3 +311,4 @@ Visit [split.io/product](https://www.split.io/product) for an overview of Split,
185311** System Status:**
186312
187313We use a status page to monitor the availability of Split’s various services. You can check the current status at [ status.split.io] ( http://status.split.io ) .
314+
0 commit comments