Embed URL Generation Made Easy With Python Script
Creating an embed URL is a multistep process that can easily be botched along the way. Finding where the mistake was made can be tricky, and the generation of multiple embed URLs can be a painful process to repeat. With a simple python script we can create a program that generates the embed URL for us by just entering the json blob. import hashlib import hmac import urllib import json import ast api_key = 'abc-123456' json_input = raw_input('input your json encoded dashboard:') to_dict = ast.literal_eval(json_input) json_data = json.dumps(to_dict) encoded_json = (urllib.urlencode({"data": json_data})).split("=")[1] url = "/api/embedded_dashboard?data=" + encoded_json sig = hmac.new(api_key, url, hashlib.sha256).hexdigest() print "https://www.periscopedata.com" + url + "&signature=" + sig The above example is the fastest way to generate the direct link to the embedded dashboard. However, in some cases, the resulting URL will be too long for the web browser to parse. In those situations, you'll need to let the Sisense for Cloud Data Teams API create the url for you. Consider the following example: import json import requests payload = {"dashboard":MyDashboardID} site_name = 'my-site-name' api_key = 'my-site-api-key' url = 'https://app.periscopedata.com/api/v1/shared_dashboard/create' headers = {'HTTP-X-PARTNER-AUTH': site_name + ":" + api_key} data = json.dumps(payload) response = requests.post(url, headers=headers, data=data) try: print(json.loads(response.text)['url']) print(json.loads(response.text)['url']+'?border=false&embed=v2') except: print('Couldn''t generate url: ' + response.text) Declare three variables payload, site_name, and api_key and copy paste the rest. Payload should be a valid json object as defined here. You can validate yours at a site like this one: https://jsonlint.com/ . Site name and dashboard id can be found in your url bar like in the image below: And lastly, admins can find the api key via Billing & Authentication in the Settings menu. Now that we've created so many embedded dashboards, we need some way to manage them! Below are two scripts that help with that. We have two functions, one to list all embeds for a dashboard and one to delete an embedded dashboard. List all Embedded Dashboards for a Given Dashboard import json import requests site_name = 'my-site-name' api_key = 'my-site-api-key' dashboard_id = my-dashboard-id url = 'https://app.periscopedata.com/api/v1/shared_dashboard/list' headers = {'HTTP-X-PARTNER-AUTH': site_name + ":" + api_key} data = json.dumps({"dashboard":dashboard_id}) response = requests.post(url, headers=headers, data=data) try: [print(i) for i in json.loads(response.text)] except: print('Couldn''t list dashboards: ' + response.text) This list script will provide the tokens (needed for the deletion script) as well as the urls of the embedded dashboards. Delete a specific embedded dashboard import json import requests site_name = 'my-site-name' api_key = 'my-site-api-key' token = 'my-dashboard-token' url = 'https://app.periscopedata.com/api/v1/shared_dashboard/delete' headers = {'HTTP-X-PARTNER-AUTH': site_name + ":" + api_key} data = json.dumps({"token":token}) response = requests.post(url, headers=headers, data=data) try: print(json.loads(response.text)) except: print('Couldn''t delete: ' + response.text) The response for the delete API is an object that tells how many dashboards were deleted. If none were successfully deleted, the value will be 0. If it deletes 1, it will be 1! For Python 3: import hashlib import hmac import urllib import json import ast import base64 api_key = b'insert key here' json_input = input() to_dict = ast.literal_eval(json_input) json_data = json.dumps(to_dict) encoded_json = (urllib.parse.urlencode(({"data": json_data})).split("=")[1]) url = "/api/embedded_dashboard?data=" + encoded_json sig = hmac.new(api_key, url.encode(), hashlib.sha256).hexdigest() print("https://www.periscopedata.com" + url + "&signature=" + sig)2.4KViews1like0CommentsUser & Role Management API Call Examples In Python (RBAC)
Similar to the User and Group API, Sites that have RBAC enabled can use the User and Role Management APIs for RBAC allows us to create, read, update, and destroy users and roles programmatically. Here's the official documentation. Below are simple examples showing how you can test this out in Python 3. Note: To use these APIs, you'll need your site name, api-key, and access to the Users & Roles API for RBAC (upgraded feature) Users GET Users GET Single User CREATE User UPDATE User DELETE User Roles GET Roles GET Single Role CREATE Role UPDATE Role DELETE Role Dashboard GET Dashboards USERS GET Users import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v2/users' response = requests.get(url, headers=headers) print(json.loads(response.text)) GET Single User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v2/users/0000-test-1111-user-id' response = requests.get(url, headers=headers) print(json.loads(response.text)) CREATE User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "first_name": 'MyFirstName' , "last_name": 'MyLastName', "email": '[email protected]', "roles": [ '0000-admin-1111-id' ], "invited_by_email": '[email protected]' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v2/users?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text)) UPDATE User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "first_name": 'newFirstName' , "last_name": 'newLastName' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v2/users/0000-test-1111-user-id?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text)) DELETE User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v2/users/0000-test-1111-user-id?test_mode=true' response = requests.delete(url, headers=headers) print(response.status_code) ROLES GET Roles import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/roles' response = requests.get(url, headers=headers) print(json.loads(response.text)) GET Single Role import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/roles/0000-test-1111-role-id' response = requests.get(url, headers=headers) print(json.loads(response.text)) CREATE Role import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myNewRole" , "description": "testing", "created_by_email": "[email protected]", "privileges": [{ "object_type":"Dashboard", "permissions":["read_dashboards"] }] , "permissions": [{ "object_type":"Dashboard", "object_id":"0000-dashboard-1111-id", "permissions":["create_dashboards"] }] } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/roles?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text)) UPDATE Role import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myExistingRole" , "description": "NewDescription", "updated_by_email": "[email protected]" } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/roles/0000-test-1111-role-id?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text)) DELETE Role import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', } url = 'https://api.periscopedata.com/api/v1/roles/0000-test-1111-role-id?test_mode=true' response = requests.delete(url, headers=headers) print(response.status_code) DASHBOARDS GET Dashboards import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/dashboards' response = requests.get(url, headers=headers) print(json.loads(response.text)) Here's an example script on how to create users in bulk from a CSV file that contains the users' names and emails Note: You'll need to get the role_id's from using the GET Roles call. That will give you the info for all of the site's roles and you'll want to extract the id's from there. If the role parameter is left out in the payload, then the users will automatically be only in the Everyone role when added. import requests import json import pandas as pd #insert csv file that contains names and emails df = pd.read_csv('csv file') #use get roles api call to get role id roles = 'role id' invited = 'site admins email' #split first and last name if they are together in one column df['last_name'] = df['Name'].str.split().str[1] df['first_name'] = df['Name'].str.split().str[0] #keep these variables blank first_name = '' last_name = '' email = '' #loop through each user in csv and create user for index, row in df.iterrows(): first_name = row['first_name'] last_name = row['last_name'] email = row['Email'] #insert site name and api_key headers = { 'HTTP-X-PARTNER-AUTH': 'site_name:api_key', 'Content-Type' : 'application/json' } payload = { "first_name": first_name, "last_name": last_name, "email": email, "roles": [ roles ], "invited_by_email": invited } data = json.dumps(payload) #use test_mode=true to test script url = 'https://api.periscopedata.com/api/v2/users?test_mode=false' response = requests.post(url, headers=headers, data=data) print(payload) print(json.loads(response.text))2.5KViews1like0CommentsUser & Group Management API Call Examples In Python
With the release of our User and Group Management APIs, we can now create, read, update, and destroy users and groups programmatically. CRUD! Here's the official documentation. Here are some super simple examples showing how you can test this out in Python 3. To use these APIs, you'll need your site name, api-key, and access to the Users & Groups API (upgraded feature) Users GET Users GET Single User CREATE User UPDATE User DELETE User Groups GET Groups GET Single Group CREATE Group UPDATE Group DELETE Group USERS GET Users import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/users' response = requests.get(url, headers=headers) print(json.loads(response.text)) GET Single User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': '[email protected]' } url = 'https://api.periscopedata.com/api/v1/users' response = requests.get(url, headers=headers) print(json.loads(response.text)) CREATE User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "first_name": 'MyFirstName' , "last_name": 'MyLastName', "email": '[email protected]', "groups": [ 'Administrators' ], "invited_by_email": '[email protected]' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text)) UPDATE User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': '[email protected]' } payload = { "first_name": 'newFirstName' , "last_name": 'newLastName' } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text)) DELETE User import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': '[email protected]' } url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.delete(url, headers=headers) print(json.loads(response.text)) GROUPS GET Groups import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/groups' response = requests.get(url, headers=headers) print(json.loads(response.text)) GET Single Group import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/groups/Administrators' response = requests.get(url, headers=headers) print(json.loads(response.text)) CREATE Group import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myNewGroup" , "access": "discovery", "created_by_email": "[email protected]" } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/groups?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text)) UPDATE Group import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json' } payload = { "name": "myExistingGroup" , "access": "View" } data = json.dumps(payload) url = 'https://api.periscopedata.com/api/v1/groups/my-new-group?test_mode=true' response = requests.put(url, headers=headers, data=data) print(json.loads(response.text)) DELETE Group import requests import json headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key', 'Content-Type' : 'application/json', } url = 'https://api.periscopedata.com/api/v1/groups/myexistinggroup?test_mode=true' response = requests.delete(url, headers=headers) print(response.status_code) These APIs make it super easy to add a user to a site. What if you have multiple sites and want to add a single user to all of them at once? We can do that too! Here's an example: import requests import json def addUserToSite(firstName, lastName, email, invitedByEmail, siteName, apiKey, groups=[]): payload = { "first_name": firstName , "last_name": lastName, "email": email, "groups": [], "invited_by_email": invitedByEmail } if (groups): payload["groups"] = groups data = json.dumps(payload) auth = siteName + ':' + apiKey headers = { 'HTTP-X-PARTNER-AUTH': auth, 'Content-Type' : 'application/json' } url = 'https://api.periscopedata.com/api/v1/users?test_mode=true' response = requests.post(url, headers=headers, data=data) print(json.loads(response.text)) def addUserToSites(user, sites): for site in sites: addUserToSite(user["first_name"], user["last_name"], user["email"], user["invited_by_email"], site["site_name"], site["api_key"], user["groups"]) mySites = [ {"site_name":"mySiteName1", "api_key":"myAPIKey1"}, {"site_name":"mySiteName2", "api_key":"myAPIKey2"}, {"site_name":"mySiteName3", "api_key":"myAPIKey3"} ] myNewUser = { "first_name": 'newUserFirstName' , "last_name": 'newUserLastName', "email": '[email protected]', "groups": [], "invited_by_email": '[email protected]' } addUserToSites(myNewUser, mySites) With this, all you need to do is update the values in the mySites and myNewUser objects. Be sure to remove "?test_mode=true" once you have it set up the way you like! Here is a script if you have a CSV of users you want to delete in bulk: import requests import json import pandas as pd df = pd.read_csv('my_csv_of_users_to_delete.csv') emails = df.email_address email_array = emails.values email_array for i in email_array: headers = { 'HTTP-X-PARTNER-AUTH': 'site-name:api-key' , 'Content-Type' : 'application/json', 'HTTP-X-EMAIL': i } url = 'https://api.periscopedata.com/api/v1/users?test_mode=False' response = requests.delete(url, headers=headers) print(i) print(json.loads(response.text))1.6KViews1like0Comments