8000 Add flexibility for wkbk/ds id or item in endpoint by jorwoods · Pull Request #570 · tableau/server-client-python · GitHub
[go: up one dir, main page]

Skip to content

Add flexibility for wkbk/ds id or item in endpoint #570

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion tableauserverclient/server/endpoint/datasources_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ def update_connection(self, datasource_item, connection_item):
connection_item.id))
return connection

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While you're making changes here, can you add the @api(version='2.8') annotation for the refresh method?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shinchris I added the API decorator. I didn't see any existing unit tests for the refresh methods that I can expand upon. I need to read a little more about requests_mock to know how to write them appropriately for what this should accomplish.

@api(version="2.8")
def refresh(self, datasource_item):
url = "{0}/{1}/refresh".format(self.baseurl, datasource_item.id)
id_ = getattr(datasource_item, 'id', datasource_item)
url = "{0}/{1}/refresh".format(self.baseurl, id_)
empty_req = RequestFactory.Empty.empty_req()
server_response = self.post_request(url, empty_req)
new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0]
Expand Down
3 changes: 2 additions & 1 deletion tableauserverclient/server/endpoint/jobs_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ def get(self, job_id=None, req_options=None):

@api(version='3.1')
def cancel(self, job_id):
url = '{0}/{1}'.format(self.baseurl, job_id)
id_ = getattr(job_id, 'id', job_id)
url = '{0}/{1}'.format(self.baseurl, id_)
return self.put_request(url)

@api(version='2.6')
Expand Down
3 changes: 2 additions & 1 deletion tableauserverclient/server/endpoint/workbooks_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def get_by_id(self, workbook_id):

@api(version="2.8")
def refresh(self, workbook_id):
url = "{0}/{1}/refresh".format(self.baseurl, workbook_id)
id_ = getattr(workbook_id, 'id', workbook_id)
url = "{0}/{1}/refresh".format(self.baseurl, id_)
empty_req = RequestFactory.Empty.empty_req()
server_response = self.post_request(url, empty_req)
new_job = JobItem.from_response(server_response.content, self.parent_srv.namespace)[0]
Expand Down
8 changes: 8 additions & 0 deletions test/assets/datasource_refresh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
<job id="7c3d599e-949f-44c3-94a1-f30ba85757e4" mode="Asynchronous" type="RefreshExtract" createdAt="2020-03-05T22:05:32Z">
<extractRefreshJob>
<datasource id="9dbd2263-16b5-46e1-9c43-a76bb8ab65fb" name="datasource-name" />
</extractRefreshJob>
</job>
</tsResponse>
8 changes: 8 additions & 0 deletions test/assets/workbook_refresh.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version='1.0' encoding='UTF-8'?>
<tsResponse xmlns="http://tableau.com/api" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://tableau.com/api http://tableau.com/api/ts-api-2.3.xsd">
<job id="7c3d599e-949f-44c3-94a1-f30ba85757e4" mode="Asynchronous" type="RefreshExtract" createdAt="2020-03-05T22:05:32Z">
<extractRefreshJob>
<workbook id="3cc6cd06-89ce-4fdc-b935-5294135d6d42" name="workbook-name" />
</extractRefreshJob>
</job>
</tsResponse>
21 changes: 21 additions & 0 deletions test/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
POPULATE_PERMISSIONS_XML = 'datasource_populate_permissions.xml'
PUBLISH_XML = 'datasource_publish.xml'
PUBLISH_XML_ASYNC = 'datasource_publish_async.xml'
REFRESH_XML = 'datasource_refresh.xml'
UPDATE_XML = 'datasource_update.xml'
UPDATE_CONNECTION_XML = 'datasource_connection_update.xml'

Expand Down Expand Up @@ -249,6 +250,26 @@ def test_publish_async(self):
self.assertEqual('2018-06-30T00:54:54Z', format_datetime(new_job.created_at))
self.assertEqual('1', new_job.finish_code)

def test_refresh_id(self):
self.server.version = '2.8'
self.baseurl = self.server.datasources.baseurl
response_xml = read_xml_asset(REFRESH_XML)
with requests_mock.mock() as m:
m.post(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/refresh',
status_code=202, text=response_xml)
self.server.datasources.refresh('9dbd2263-16b5-46e1-9c43-a76bb8ab65fb')

def test_refresh_object(self):
self.server.version = '2.8'
self.baseurl = self.server.datasources.baseurl
datasource = TSC.DatasourceItem('')
datasource._id = '9dbd2263-16b5-46e1-9c43-a76bb8ab65fb'
response_xml = read_xml_asset(REFRESH_XML)
with requests_mock.mock() as m:
m.post(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb/refresh',
status_code=202, text=response_xml)
self.server.datasources.refresh(datasource)

def test_delete(self):
with requests_mock.mock() as m:
m.delete(self.baseurl + '/9dbd2263-16b5-46e1-9c43-a76bb8ab65fb', status_code=204)
Expand Down
11 changes: 10 additions & 1 deletion test/test_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,16 @@ def test_get_before_signin(self):
self.server._auth_token = None
self.assertRaises(TSC.NotSignedInError, self.server.jobs.get)

def test_cancel(self):
def test_cancel_id(self):
with requests_mock.mock() as m:
m.put(self.baseurl + '/ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', status_code=204)
self.server.jobs.cancel('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760')

def test_cancel_item(self):
created_at = datetime(2018, 5, 22, 13, 0, 29, tzinfo=utc)
started_at = datetime(2018, 5, 22, 13, 0, 37, tzinfo=utc)
job = TSC.JobItem('ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', 'backgroundJob',
0, created_at, started_at, None, 0)
with requests_mock.mock() as m:
m.put(self.baseurl + '/ee8c6e70-43b6-11e6-af4f-f7b0d8e20760', status_code=204)
self.server.jobs.cancel(job)
23 changes: 23 additions & 0 deletions test/test_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
POPULATE_VIEWS_USAGE_XML = os.path.join(TEST_ASSET_DIR, 'workbook_populate_views_usage.xml')
PUBLISH_XML = os.path.join(TEST_ASSET_DIR, 'workbook_publish.xml')
PUBLISH_ASYNC_XML = os.path.join(TEST_ASSET_DIR, 'workbook_publish_async.xml')
REFRESH_XML = os.path.join(TEST_ASSET_DIR, 'workbook_refresh.xml')
UPDATE_XML = os.path.join(TEST_ASSET_DIR, 'workbook_update.xml')
UPDATE_PERMISSIONS = os.path.join(TEST_ASSET_DIR, 'workbook_update_permissions.xml')

Expand Down Expand Up @@ -114,6 +115,28 @@ def test_get_by_id(self):
def test_get_by_id_missing_id(self):
self.assertRaises(ValueError, self.server.workbooks.get_by_id, '')

def test_refresh_id(self):
self.server.version = '2.8'
self.baseurl = self.server.workbooks.baseurl
with open(REFRESH_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.post(self.baseurl + '/3cc6cd06-89ce-4fdc-b935-5294135d6d42/refresh',
status_code=202, text=response_xml)
self.server.workbooks.refresh('3cc6cd06-89ce-4fdc-b935-5294135d6d42')

def test_refresh_object(self):
self.server.version = '2.8'
self.baseurl = self.server.workbooks.baseurl
workbook = TSC.WorkbookItem('')
workbook._id = '3cc6cd06-89ce-4fdc-b935-5294135d6d42'
with open(REFRESH_XML, 'rb') as f:
response_xml = f.read().decode('utf-8')
with requests_mock.mock() as m:
m.post(self.baseurl + '/3cc6cd06-89ce-4fdc-b935-5294135d6d42/refresh',
status_code=202, text=response_xml)
self.server.workbooks.refresh(workbook)

def test_delete(self):
with requests_mock.mock() as m:
m.delete(self.baseurl + '/3cc6cd06-89ce-4fdc-b935-5294135d6d42', status_code=204)
Expand Down
0