Hi There,
We are trying to parse XML using REST API "Query Workbooks for Site" to get the workbook id, workbook name, tags associated with the workbook and the project it belongs to.
Expected Response body:
<tsResponse>
<pagination pageNumber="page-number"
pageSize="page-size"
totalAvailable="total-available" />
<workbooks>
<workbook id="workbook-id" name="name"
contentUrl="content-url"
showTabs="show-tabs-flag"
size="size-in-megabytes"
createdAt="datetime-created"
updatedAt="datetime-updated" >
<project id="project-id" name="project-name" />
<owner id="user-id" />
<tags>
<tag label="tag"/>
... additional tags ...
</tags>
</workbook>
... additional workbooks ...
</workbooks>
We are unable to get the project id, project name, owner id and the tags associated with the workbook
We use the below to parse XML.
import xml.etree.ElementTree as ET
Below is the query
def query_workbooks():
"""
Returns a list of workbooks on the site (a list of <workbook> elements).
The function paginates over results (if required) using a page size of 100.
"""
#GET /api/api-version/sites/site-id/workbooks?pageSize=page-size&pageNumber=page-number
url = SERVER + "/api/{0}/sites/{1}/workbooks".format(API_VERSION,SITE_ID)
pageNum, pageSize = 1, 100
paged_url = url + "?pageSize={}&pageNumber={}".format(pageSize, pageNum)
server_response = requests.get(paged_url, headers={"x-tableau-auth": TOKEN},verify=False)
server_response.encoding = "utf-8";
if server_response.status_code != 200:
print(_encode_for_display(server_response.text))
sys.exit(1)
xml_response = ET.fromstring(_encode_for_display(server_response.text))
total_count_of_workbooks = int(xml_response.find('t:pagination', namespaces=xmlns).attrib.get('totalAvailable'))
if total_count_of_workbooks > pageSize:
workbooks = []
workbooks.extend(xml_response.findall('.//t:workbooks/workbook', namespaces=xmlns))
number_of_pages = int(math.ceil(total_count_of_workbooks / pageSize))
# Starts from page 2 because page 1 has already been returned
for page in range(2, number_of_pages + 1):
paged_url = url + "?pageSize={}&pageNumber={}".format(pageSize, page)
server_response = requests.get(paged_url, headers={"x-tableau-auth": TOKEN},verify=False)
if server_response.status_code != 200:
print(_encode_for_display(server_response.text))
sys.exit(1)
workbooks_from_page = ET.fromstring(_encode_for_display(server_response.text)).findall('.//t:workbook', namespaces=xmlns)
workbooks.extend(workbooks_from_page)
else:
workbooks = xml_response.findall('.//t:workbook', namespaces=xmlns)
return workbooks
````````````````
for WB in list_of_workbooks:
print("IN A WB : "+ str(WB))
'''
a = a + 1
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ :" + str(a))
print("WB #" + str(a) + " is:" + WB.get('name'))
print(" id is: " + WB.get('id'))
print(" contentUrl is:" + WB.get('contentUrl'))
print(" updatedAt is:" + WB.get('updatedAt'))
'''
for tag in list_of_tags:
print("IN A TAG" + tag.get(tag))
Can someone help us to understand what's wrong with this query, why we are not able to get the tags, project id, project name, owner id in the response.
Thanks,
Shoba
You might be interested in looking at the Server-client-python or Tableau_Tools which are both wrappers around our API and handle the XML in the background. You can either look at these implementations to see how they coded the responses or use them directly in your code.