|
| 1 | +#### |
| 2 | +# This script demonstrates how to use the metadata API to query information on a published data source |
| 3 | +# |
| 4 | +# To run the script, you must have installed Python 3.5 or later. |
| 5 | +#### |
| 6 | + |
| 7 | +import argparse |
| 8 | +import logging |
| 9 | +from pprint import pprint |
| 10 | + |
| 11 | +import tableauserverclient as TSC |
| 12 | + |
| 13 | + |
| 14 | +def main(): |
| 15 | + parser = argparse.ArgumentParser(description='Use the metadata API to get information on a published data source.') |
| 16 | + # Common options; please keep those in sync across all samples |
| 17 | + parser.add_argument('--server', '-s', required=True, help='server address') |
| 18 | + parser.add_argument('--site', '-S', help='site name') |
| 19 | + parser.add_argument('--token-name', '-n', required=True, |
| 20 | + help='name of the personal access token used to sign into the server') |
| 21 | + parser.add_argument('--token-value', '-v', required=True, |
| 22 | + help='value of the personal access token used to sign into the server') |
| 23 | + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
| 24 | + help='desired logging level (set to error by default)') |
| 25 | + # Options specific to this sample |
| 26 | + parser.add_argument('datasource_name', nargs='?', help="The name of the published datasource. If not present, we query all data sources.") |
| 27 | + |
| 28 | + |
| 29 | + args = parser.parse_args() |
| 30 | + |
| 31 | + # Set logging level based on user input, or error by default |
| 32 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 33 | + logging.basicConfig(level=logging_level) |
| 34 | + |
| 35 | + # Sign in to server |
| 36 | + tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) |
| 37 | + server = TSC.Server(args.server, use_server_version=True) |
| 38 | + with server.auth.sign_in(tableau_auth): |
| 39 | + # Execute the query |
| 40 | + result = server.metadata.query(""" |
| 41 | + query useMetadataApiToQueryOrdersDatabases($name: String){ |
| 42 | + publishedDatasources (filter: {name: $name}) { |
| 43 | + luid |
| 44 | + name |
| 45 | + description |
| 46 | + projectName |
| 47 | + fields { |
| 48 | + name |
| 49 | + } |
| 50 | + } |
| 51 | + }""", {"name": args.datasource_name}) |
| 52 | + |
| 53 | + # Display warnings/errors (if any) |
| 54 | + if result.get("errors"): |
| 55 | + print("### Errors/Warnings:") |
| 56 | + pprint(result["errors"]) |
| 57 | + |
| 58 | + # Print the results |
| 59 | + if result.get("data"): |
| 60 | + print("### Results:") |
| 61 | + pprint(result["data"]["publishedDatasources"]) |
| 62 | + |
| 63 | +if __name__ == '__main__': |
| 64 | + main() |
0 commit comments