|
| 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.7 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", help="server address") |
| 18 | + parser.add_argument("--site", "-S", help="site name") |
| 19 | + parser.add_argument("--token-name", "-n", help="name of the personal access token used to sign into the server") |
| 20 | + parser.add_argument("--token-value", "-v", help="value of the personal access token used to sign into the server") |
| 21 | + parser.add_argument( |
| 22 | + "--logging-level", |
| 23 | + "-l", |
| 24 | + choices=["debug", "info", "error"], |
| 25 | + default="error", |
| 26 | + help="desired logging level (set to error by default)", |
| 27 | + ) |
| 28 | + # Options specific to this sample |
| 29 | + parser.add_argument( |
| 30 | + "datasource_name", |
| 31 | + nargs="?", |
| 32 | + help="The name of the published datasource. If not present, we query all data sources.", |
| 33 | + ) |
| 34 | + |
| 35 | + args = parser.parse_args() |
| 36 | + |
| 37 | + # Set logging level based on user input, or error by default |
| 38 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 39 | + logging.basicConfig(level=logging_level) |
| 40 | + |
| 41 | + # Sign in to server |
| 42 | + tableau_auth = TSC.PersonalAccessTokenAuth(args.token_name, args.token_value, site_id=args.site) |
| 43 | + server = TSC.Server(args.server, use_server_version=True) |
| 44 | + with server.auth.sign_in(tableau_auth): |
| 45 | + # Execute the query |
| 46 | + result = server.metadata.query( |
| 47 | + """ |
| 48 | + # Query must declare that it accepts first and afterToken variables |
| 49 | + query paged($first:Int, $afterToken:String) { |
| 50 | + workbooksConnection(first: $first, after:$afterToken) { |
| 51 | + nodes { |
| 52 | + luid |
| 53 | + name |
| 54 | + projectName |
| 55 | + description |
| 56 | + } |
| 57 | + totalCount |
| 58 | + pageInfo { |
| 59 | + endCursor |
| 60 | + hasNextPage |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + """, |
| 65 | + # "first" adjusts the page size. Here we set it to 5 to demonstrate pagination. |
| 66 | + # Set it to a higher number to reduce the number of pages. Including |
| 67 | + # first and afterToken is optional, and if not included, TSC will |
| 68 | + # use its default page size of 100. |
| 69 | + variables={"first": 5, "afterToken": None}, |
| 70 | + ) |
| 71 | + |
| 72 | + # Multiple pages are captured in result["pages"]. Each page contains |
| 73 | + # the result of one execution of the query above. |
| 74 | + for page in result["pages"]: |
| 75 | + # Display warnings/errors (if any) |
| 76 | + if page.get("errors"): |
| 77 | + print("### Errors/Warnings:") |
| 78 | + pprint(result["errors"]) |
| 79 | + |
| 80 | + # Print the results |
| 81 | + if result.get("data"): |
| 82 | + print("### Results:") |
| 83 | + pprint(result["data"]["workbooksConnection"]["nodes"]) |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == "__main__": |
| 87 | + main() |
0 commit comments