Example: Basic code uses REST Admin API to import user keys into an existing segment in a given environment.
How to use:
- Class wrapper for Admin API, installation instructions in this link: Python Admin API Library Wrapper
- Use the code below, update the following variables:
workspaceName, environmentName, segmentName
Admin API Key
- Example below shows importing Keys from csv file, the code will import keys in batches of 1000 count
from splitapiclient.main import get_client
#############################################
workspaceName = "Default"
environmentName = "Production"
segmentName = "test_24k"
sourceFile = "/Users/bilalal-shahwany/Downloads/all_keys.csv"
#############################################
client = get_client({'apikey': 'ADMIN API KEY'})
ws = client.workspaces.find(workspaceName)
env = client.environments.find(environmentName, ws.id)
segDef = client.segment_definitions.find(segmentName, env.id, ws.id)
with open(sourceFile, "r") as a_file:
cnt=0
keys = []
for line in a_file:
cnt = cnt + 1
if cnt > 1000:
final = {"keys": keys, "comment":"adding 1k keys"}
print(final)
segDef.import_keys_from_json("false", final)
keys = []
cnt = 0
keys.append(line.strip())
if cnt < 1000:
final = {"keys":keys, "comment":"adding 1k keys"}
segDef.import_keys_from_json("false", final)
Comments
0 comments
Please sign in to leave a comment.