Tableau REST API & Python - Part 2
Written on 12/07/16 - Tested on Tableau 9.3
Welcome to part 2 of the Tableau REST API tutorial. From here we assume you have the Tableau REST API library installed and that you can import it in your code. If you are not there yet, I would recommend you to go to Tableau REST API PART 1.
Part 2 is here to help you understand the code of the REST API and to be able to create your own scripts quickly.
STEP 1: Understand the basics
Here is where we left off in Part 1:
import math import xml.etree.ElementTree as ET # Contains methods used to build and parse XML import requests # Contains methods used to make HTTP requests import sys from tableau_rest_api.tableau_rest_api import * print('Testing... By You') logger = Logger(u"log_file.txt") t = TableauRestApi(u"http://127.0.0.1", u"admin", u"admin") #Connect to different stes t.enable_logging(logger) t.signin()
Let me break it down to understand what is going on here:
Importing the library is pretty straight forward but a necessary step for your script to work. If you are already confused, try this course on python from Google. Straight after this we record everything by creating a log file with logger = Logger(u"log_file.txt"). Python reference here.
The library comes with ALL Tableau's API native function already coded for you. No need to know how it works behind the scenes. The first line we always need to call on is the login one:
t = TableauRestApi(u"http://127.0.0.1", u"admin", u"admin", site_content_url="test")
Now, for all of our code we simply need to use the letter "t" to carry out any action on our Tableau Server. This will make our lives easier as we can begin to call on and perform some actions. Let's connect and add all XML results to our log file with:
t.enable_logging(logger) t.signin()
STEP 2: Simple Call - Get the information we need
Okay, we are connected and we can ask our Tableau server for a number of things. Let's ask for a user list.
users = t.query_users() users_dict = t.convert_xml_list_to_name_id_dict(users) for user_name in users_dict: print "User name {} is LUID {}".format(user_name, users_dict[user_name])
Hold on...
What is this? "LUID"? An explanation is necessary here. In a Tableau server, everything is a unique identifier. I have directly copied the following from the Tableau document since I think it's crystal clear:
Resources on Tableau Server typically have a friendly name, such as "Default" for a site name, "Developers" for a group, and "Adam" for a user. These are the names that you see when you work in the Tableau Server user interface.
These resources are also identified internally by Tableau Server using a locally unique identifier (LUID). LUIDs are 32-character hexadecimal strings separated by hyphens in a format like this: 9f8e7d6c5b-4a3f-2e1d-0c9b-8a7f6e5d4c
.
Tableau Server uses LUIDs because friendly names may change or be reassigned. For example, you can change the friendly name of a user without affecting the user's permissions or membership in groups, because the LUID for that user does not change."
So here in our code we see what LUID is assigned to a user. We can now re-use those LUID's to change any user/group/datasource in the Tabeau server.
TO BE CONTINUED....