-
Notifications
You must be signed in to change notification settings - Fork 447
Initial support for Tableau Metadata API #431
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
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from .endpoint import Endpoint, api | ||
| from .exceptions import GraphQLError | ||
| import logging | ||
| import json | ||
|
|
||
| logger = logging.getLogger('tableau.endpoint.metadata') | ||
|
|
||
|
|
||
| class Metadata(Endpoint): | ||
| @property | ||
| def baseurl(self): | ||
| return "{0}/api/exp/metadata/graphql".format(self.parent_srv._server_address) | ||
|
|
||
| @api("3.2") | ||
| def query(self, query, abort_on_error=False): | ||
| logger.info('Querying Metadata API') | ||
| url = self.baseurl | ||
|
|
||
| try: | ||
| graphql_query = json.dumps({'query': query}) | ||
| except Exception: | ||
| # Place holder for now | ||
| raise Exception('Must provide a string') | ||
|
|
||
| # Setting content type because post_reuqest defaults to text/xml | ||
| server_response = self.post_request(url, graphql_query, content_type='text/json') | ||
| results = server_response.json() | ||
|
|
||
| if abort_on_error and results.get('errors', None): | ||
| raise GraphQLError(results['errors']) | ||
|
|
||
| return results | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "data": { | ||
| "publishedDatasources": [ | ||
| { | ||
| "id": "01cf92b2-2d17-b656-fc48-5c25ef6d5352", | ||
| "name": "Batters (TestV1)" | ||
| }, | ||
| { | ||
| "id": "020ae1cd-c356-f1ad-a846-b0094850d22a", | ||
| "name": "SharePoint_List_sharepoint2010.test.tsi.lan" | ||
| }, | ||
| { | ||
| "id": "061493a0-c3b2-6f39-d08c-bc3f842b44af", | ||
| "name": "Batters_mongodb" | ||
| }, | ||
| { | ||
| "id": "089fe515-ad2f-89bc-94bd-69f55f69a9c2", | ||
| "name": "Sample - Superstore" | ||
| } | ||
| ] | ||
| }, | ||
| "errors": [ | ||
| { | ||
| "message": "Reached time limit of PT5S for query execution.", | ||
| "path": null, | ||
| "extensions": null | ||
| } | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "data": { | ||
| "publishedDatasources": [ | ||
| { | ||
| "id": "01cf92b2-2d17-b656-fc48-5c25ef6d5352", | ||
| "name": "Batters (TestV1)" | ||
| }, | ||
| { | ||
| "id": "020ae1cd-c356-f1ad-a846-b0094850d22a", | ||
| "name": "SharePoint_List_sharepoint2010.test.tsi.lan" | ||
| }, | ||
| { | ||
| "id": "061493a0-c3b2-6f39-d08c-bc3f842b44af", | ||
| "name": "Batters_mongodb" | ||
| }, | ||
| { | ||
| "id": "089fe515-ad2f-89bc-94bd-69f55f69a9c2", | ||
| "name": "Sample - Superstore" | ||
| } | ||
| ] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| import unittest | ||
| import os.path | ||
| import json | ||
| import requests_mock | ||
| import tableauserverclient as TSC | ||
|
|
||
| from tableauserverclient.server.endpoint.exceptions import GraphQLError | ||
|
|
||
| TEST_ASSET_DIR = os.path.join(os.path.dirname(__file__), 'assets') | ||
|
|
||
| METADATA_QUERY_SUCCESS = os.path.join(TEST_ASSET_DIR, 'metadata_query_success.json') | ||
| METADATA_QUERY_ERROR = os.path.join(TEST_ASSET_DIR, 'metadata_query_error.json') | ||
|
|
||
| EXPECTED_DICT = {'publishedDatasources': | ||
| [{'id': '01cf92b2-2d17-b656-fc48-5c25ef6d5352', 'name': 'Batters (TestV1)'}, | ||
| {'id': '020ae1cd-c356-f1ad-a846-b0094850d22a', 'name': 'SharePoint_List_sharepoint2010.test.tsi.lan'}, | ||
| {'id': '061493a0-c3b2-6f39-d08c-bc3f842b44af', 'name': 'Batters_mongodb'}, | ||
| {'id': '089fe515-ad2f-89bc-94bd-69f55f69a9c2', 'name': 'Sample - Superstore'}]} | ||
|
|
||
| EXPECTED_DICT_ERROR = [ | ||
| { | ||
| "message": "Reached time limit of PT5S for query execution.", | ||
| "path": None, | ||
| "extensions": None | ||
| } | ||
| ] | ||
|
|
||
|
|
||
| class MetadataTests(unittest.TestCase): | ||
| def setUp(self): | ||
| self.server = TSC.Server('http://test') | ||
| self.baseurl = self.server.metadata.baseurl | ||
| self.server.version = "3.2" | ||
|
|
||
| self.server._site_id = 'dad65087-b08b-4603-af4e-2887b8aafc67' | ||
| self.server._auth_token = 'j80k54ll2lfMZ0tv97mlPvvSCRyD0DOM' | ||
|
|
||
| def test_metadata_query(self): | ||
| with open(METADATA_QUERY_SUCCESS, 'rb') as f: | ||
| response_json = json.loads(f.read().decode()) | ||
| with requests_mock.mock() as m: | ||
| m.post(self.baseurl, json=response_json) | ||
| actual = self.server.metadata.query('fake query') | ||
|
|
||
| datasources = actual['data'] | ||
|
|
||
| self.assertDictEqual(EXPECTED_DICT, datasources) | ||
|
|
||
| def test_metadata_query_ignore_error(self): | ||
| with open(METADATA_QUERY_ERROR, 'rb') as f: | ||
| response_json = json.loads(f.read().decode()) | ||
| with requests_mock.mock() as m: | ||
| m.post(self.baseurl, json=response_json) | ||
| actual = self.server.metadata.query('fake query') | ||
| datasources = actual['data'] | ||
|
|
||
| self.assertNotEqual(actual.get('errors', None), None) | ||
| self.assertListEqual(EXPECTED_DICT_ERROR, actual['errors']) | ||
| self.assertDictEqual(EXPECTED_DICT, datasources) | ||
|
|
||
| def test_metadata_query_abort_on_error(self): | ||
| with open(METADATA_QUERY_ERROR, 'rb') as f: | ||
| response_json = json.loads(f.read().decode()) | ||
| with requests_mock.mock() as m: | ||
| m.post(self.baseurl, json=response_json) | ||
|
|
||
| with self.assertRaises(GraphQLError) as e: | ||
| self.server.metadata.query('fake query', abort_on_error=True) | ||
| self.assertListEqual(e.error, EXPECTED_DICT_ERROR) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gaoang2148 what's the latest, is it 3.4?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
3.4 is 19.2, 3.5 is 19.3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This also jumps out to me, minor issue so just merged!