10000 Add workbook to tasks (#206) · coliff/server-client-python@0d146fa · GitHub
[go: up one dir, main page]

Skip to content

Commit 0d146fa

Browse files
William Langt8y8
authored andcommitted
Add workbook to tasks (tableau#206)
* add workbook id to tasks and add tests * add target class to abstract references to other objects * add test for schedule id, refactor target
1 parent c6ab7da commit 0d146fa

File tree

7 files changed

+179
-3
lines changed

7 files changed

+179
-3
lines changed

tableauserverclient/models/target.py

Lines changed: 10 additions & 0 deletions
10000
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""Target class meant to abstract mappings to other objects"""
2+
3+
4+
class Target():
5+
def __init__(self, id_, target_type):
6+
self.id = id_
7+
self.type = target_type
8+
9+
def __repr__(self):
10+
return "<Target#{id}, {type}>".format(**self.__dict__)

tableauserverclient/models/task_item.py

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import xml.etree.ElementTree as ET
22
from .. import NAMESPACE
33
from .schedule_item import ScheduleItem
4+
from .target import Target
45

56

67
class TaskItem(object):
7-
def __init__(self, id_, task_type, priority, consecutive_failed_count=0, schedule_id=None):
8+
def __init__(self, id_, task_type, priority, consecutive_failed_count=0, schedule_id=None, target=None):
89
self.id = id_
910
self.task_type = task_type
1011
self.priority = priority
1112
self.consecutive_failed_count = consecutive_failed_count
1213
self.schedule_id = schedule_id
14+
self.target = target
1315

1416
def __repr__(self):
1517
return "<Task#{id} {task_type} pri({priority}) failed({consecutive_failed_count}) schedule_id({" \
16-
"schedule_id})>".format(**self.__dict__)
18+
"schedule_id}) target({target})>".format(**self.__dict__)
1719

1820
@classmethod
1921
def from_response(cls, xml):
@@ -28,11 +30,24 @@ def from_response(cls, xml):
2830
@classmethod
2931
def _parse_element(cls, element):
3032
schedule = None
33+
target = None
3134
schedule_element = element.find('.//t:schedule', namespaces=NAMESPACE)
35+
workbook_element = element.find('.//t:workbook', namespaces=NAMESPACE)
36+
datasource_element = element.find('.//t:datasource', namespaces=NAMESPACE)
3237
if schedule_element is not None:
3338
schedule = schedule_element.get('id', None)
39+
40+
# according to the Tableau Server REST API documentation,
41+
# there should be only one of workbook or datasource
42+
if workbook_element is not None:
43+
workbook_id = workbook_element.get('id', None)
44+
target = Target(workbook_id, "workbook")
45+
if datasource_element is not None:
46+
datasource_id = datasource_element.get('id', None)
47+
target = Target(datasource_id, "datasource")
48+
3449
task_type = element.get('type', None)
3550
priority = int(element.get('priority', -1))
3651
consecutive_failed_count = int(element.get('consecutiveFailedCount', 0))
3752
id_ = element.get('id', None)
38-
return cls(id_, task_type, priority, consecutive_failed_count, schedule)
53+
return cls(id_, task_type, priority, consecutive_failed_count, schedule, target)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse
3+
xmlns="http://tableau.com/api"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
5+
<tasks>
6+
<task>
7+
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
8+
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
9+
</extractRefresh>
10+
</task>
11+
<task>
12+
<extractRefresh id="6e8255f1-142b-4e17-bd9a-9fe6736812a1" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
13+
<schedule id="82974721-6831-4c78-875c-b7c2d5237bc2" name="Refresh daily [15:00 - 17:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:39:39Z" updatedAt="2017-07-12T22:00:05Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T22:00:00Z" />
14+
</extractRefresh>
15+
</task>
16+
<task>
17+
<extractRefresh id="90cbc49a-b668-4355-9321-46f7fefd4197" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
18+
<schedule id="c39a5a41-ab5b-4661-90f6-78384916748c" name="Refresh daily [00:00 - 02:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:33:51Z" updatedAt="2017-07-12T07:00:08Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T07:00:00Z" />
19+
</extractRefresh>
20+
</task>
21+
</tasks>
22+
</tsResponse>

test/assets/tasks_with_datasource.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse
3+
xmlns="http://tableau.com/api"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
5+
<tasks>
6+
<task>
7+
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
8+
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
9+
<datasource id="c7a9327e-1cda-4504-b026-ddb43b976d1d" />
10+
</extractRefresh>
11+
</task>
12+
</tasks>
13+
</tsResponse>

test/assets/tasks_with_workbook.xml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse
3+
xmlns="http://tableau.com/api"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
5+
<tasks>
6+
<task>
7+
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
8+
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
9+
<workbook id="c7a9327e-1cda-4504-b026-ddb43b976d1d" />
10+
</extractRefresh>
11+
</task>
12+
</tasks>
13+
</tsResponse>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version='1.0' encoding='UTF-8'?>
2+
<tsResponse
3+
xmlns="http://tableau.com/api"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.6.xsd">
5+
<tasks>
6+
<task>
7+
<extractRefresh id="f84901ac-72ad-4f9b-a87e-7a3500402ad6" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
8+
<schedule id="b60b4efd-a6f7-4599-beb3-cb677e7abac1" name="Refresh daily [23:00 - 01:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:42:55Z" updatedAt="2017-07-12T06:00:06Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T06:00:00Z" />
9+
<workbook id="c7a9327e-1cda-4504-b026-ddb43b976d1d" />
10+
</extractRefresh>
11+
</task>
12+
<task>
13+
<extractRefresh id="6e8255f1-142b-4e17-bd9a-9fe6736812a1" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
14+
<schedule id="82974721-6831-4c78-875c-b7c2d5237bc2" name="Refresh daily [15:00 - 17:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:39:39Z" updatedAt="2017-07-12T22:00:05Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T22:00:00Z" />
15+
<datasource id="61334430-fb0c-43af-a5a3-8db3323644b6" />
16+
</extractRefresh>
17+
</task>
18+
<task>
19+
<extractRefresh id="90cbc49a-b668-4355-9321-46f7fefd4197" priority="50" consecutiveFailedCount="0" type="REFRESH_EXTRACT">
20+
<schedule id="c39a5a41-ab5b-4661-90f6-78384916748c" name="Refresh daily [00:00 - 02:00, Pacific US]" state="Active" priority="50" createdAt="2016-02-11T01:33:51Z" updatedAt="2017-07-12T07:00:08Z" type="Extract" frequency="Daily" nextRunAt="2017-07-13T07:00:00Z" />
21+
<workbook id="952663f4-1bb4-4fed-91f8-5967f48c533a" />
22+
</extractRefresh>
23+
</task>
24+
</tasks>
25+
</tsResponse>

test/test_task.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import unittest
2+
import os
3+
import requests_mock
4+
import tableauserverclient as TSC
5+
6+
TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), "assets")
7+
8+
GET_XML_NO_WORKBOOK = os.path.join(TEST_ASSET_DIR, "tasks_no_workbook_or_datasource.xml")
9+
GET_XML_WITH_WORKBOOK = os.path.join(TEST_ASSET_DIR, "tasks_with_workbook.xml")
10+
GET_XML_WITH_DATASOURCE = os.path.join(TEST_ASSET_DIR, "tasks_with_datasource.xml")
11+
GET_XML_WITH_WORKBOOK_AND_DATASOURCE = os.path.join(TEST_ASSET_DIR, "tasks_with_workbook_and_datasource.xml")
12+
13+
14+
class TaskTests(unittest.TestCase):
15+
def setUp(self):
16+
self.server = TSC.Server("http://test")
17+
self.server.version = '2.6'
18+
19+
# Fake Signin
20+
self.server._site_id = "dad65087-b08b-4603-af4e-2887b8aafc67"
21+
self.server._auth_token = "j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM"
22+
23+
self.baseurl = self.server.tasks.baseurl
24+
25+
def test_get_tasks_with_no_workbook(self):
26+
with open(GET_XML_NO_WORKBOOK, "rb") as f:
27+
response_xml = f.read().decode("utf-8")
28+
with requests_mock.mock() as m:
29+
m.get(self.baseurl, text=response_xml)
30+
all_tasks, pagination_item = self.server.tasks.get()
31+
32+
task = all_tasks[0]
33+
self.assertEqual(None, task.target)
34+
35+
def test_get_tasks_with_workbook(self):
36+
with open(GET_XML_WITH_WORKBOOK, "rb") as f:
37+
response_xml = f.read().decode("utf-8")
38+
with requests_mock.mock() as m:
39+
m.get(self.baseurl, text=response_xml)
40+
all_tasks, pagination_item = self.server.tasks.get()
41+
42+
task = all_tasks[0]
43+
self.assertEqual('c7a9327e-1cda-4504-b026-ddb43b976d1d', task.target.id)
44+
self.assertEqual('workbook', task.target.type)
45+
46+
def test_get_tasks_with_datasource(self):
47+
with open(GET_XML_WITH_DATASOURCE, "rb") as f:
48+
response_xml = f.read().decode("utf-8")
49+
with requests_mock.mock() as m:
50+
m.get(self.baseurl, text=response_xml)
51+
all_tasks, pagination_item = self.server.tasks.get()
52+
53+
task = all_tasks[0]
54+
self.assertEqual('c7a9327e-1cda-4504-b026-ddb43b976d1d', task.target.id)
55+
self.assertEqual('datasource', task.target.type)
56+
57+
def test_get_tasks_with_workbook_and_datasource(self):
58+
with open(GET_XML_WITH_WORKBOOK_AND_DATASOURCE, "rb") as f:
59+
response_xml = f.read().decode("utf-8")
60+
with requests_mock.mock() as m:
61+
m.get(self.baseurl, text=response_xml)
62+
all_tasks, pagination_item = self.server.tasks.get()
63+
64+
self.assertEqual('workbook', all_tasks[0].target.type)
65+
self.assertEqual('datasource', all_tasks[1].target.type)
66+
self.assertEqual('workbook', all_tasks[2].target.type)
67+
68+
def test_get_task_with_schedule(self):
69+
with open(GET_XML_WITH_WORKBOOK, "rb") as f:
70+
response_xml = f.read().decode("utf-8")
71+
with requests_mock.mock() as m:
72+
m.get(self.baseurl, text=response_xml)
73+
all_tasks, pagination_item = self.server.tasks.get()
74+
75+
task = all_tasks[0]
76+
self.assertEqual('c7a9327e-1cda-4504-b026-ddb43b976d1d', task.target.id)
77+
self.assertEqual('workbook', task.target.type)
78+
self.assertEqual('b60b4efd-a6f7-4599-beb3-cb677e7abac1', task.schedule_id)

0 commit comments

Comments
 (0)
0