|
1 | 1 | #### |
2 | | -# This script demonstrates how to use the Tableau Server API |
| 2 | +# This script demonstrates how to use the Tableau Server Client |
3 | 3 | # to interact with datasources. It explores the different |
4 | 4 | # functions that the Server API supports on datasources. |
5 | 5 | # |
|
9 | 9 | # on top of the general operations. |
10 | 10 | #### |
11 | 11 |
|
12 | | - |
13 | | -import tableauserverclient as TSC |
14 | | -import os.path |
15 | 12 | import argparse |
16 | 13 | import getpass |
17 | 14 | import logging |
18 | 15 |
|
19 | | -parser = argparse.ArgumentParser(description='Explore datasource functions supported by the Server API.') |
20 | | -parser.add_argument('--server', '-s', required=True, help='server address') |
21 | | -parser.add_argument('--username', '-u', required=True, help='username to sign into server') |
22 | | -parser.add_argument('--publish', '-p', metavar='FILEPATH', help='path to datasource to publish') |
23 | | -parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded datasource') |
24 | | -parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
25 | | - help='desired logging level (set to error by default)') |
26 | | -args = parser.parse_args() |
| 16 | +import tableauserverclient as TSC |
| 17 | + |
| 18 | + |
| 19 | +def main(): |
| 20 | + |
| 21 | + parser = argparse.ArgumentParser(description='Explore datasource functions supported by the Server API.') |
| 22 | + parser.add_argument('--server', '-s', required=True, help='server address') |
| 23 | + parser.add_argument('--username', '-u', required=True, help='username to sign into server') |
| 24 | + parser.add_argument('--publish', '-p', metavar='FILEPATH', help='path to datasource to publish') |
| 25 | + parser.add_argument('--download', '-d', metavar='FILEPATH', help='path to save downloaded datasource') |
| 26 | + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
| 27 | + help='desired logging level (set to error by default)') |
| 28 | + |
| 29 | + args = parser.parse_args() |
| 30 | + |
| 31 | + password = getpass.getpass("Password: ") |
| 32 | + |
| 33 | + # Set logging level based on user input, or error by default |
| 34 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 35 | + logging.basicConfig(level=logging_level) |
27 | 36 |
|
28 | | -password = getpass.getpass("Password: ") |
| 37 | + # SIGN IN |
| 38 | + tableau_auth = TSC.TableauAuth(args.username, password) |
| 39 | + server = TSC.Server(args.server) |
| 40 | + with server.auth.sign_in(tableau_auth): |
| 41 | + # Query projects for use when demonstrating publishing and updating |
| 42 | + all_projects, pagination_item = server.projects.get() |
| 43 | + default_project = next((project for project in all_projects if project.is_default()), None) |
29 | 44 |
|
30 | | -# Set logging level based on user input, or error by default |
31 | | -logging_level = getattr(logging, args.logging_level.upper()) |
32 | | -logging.basicConfig(level=logging_level) |
| 45 | + # Publish datasource if publish flag is set (-publish, -p) |
| 46 | + if args.publish: |
| 47 | + if default_project is not None: |
| 48 | + new_datasource = TSC.DatasourceItem(default_project.id) |
| 49 | + new_datasource = server.datasources.publish( |
| 50 | + new_datasource, args.publish, TSC.Server.PublishMode.Overwrite) |
| 51 | + print("Datasource published. ID: {}".format(new_datasource.id)) |
| 52 | + else: |
| 53 | + print("Publish failed. Could not find the default project.") |
33 | 54 |
|
34 | | -# SIGN IN |
35 | | -tableau_auth = TSC.TableauAuth(args.username, password) |
36 | | -server = TSC.Server(args.server) |
37 | | -with server.auth.sign_in(tableau_auth): |
38 | | - # Query projects for use when demonstrating publishing and updating |
39 | | - all_projects, pagination_item = server.projects.get() |
40 | | - default_project = next((project for project in all_projects if project.is_default()), None) |
| 55 | + # Gets all datasource items |
| 56 | + all_datasources, pagination_item = server.datasources.get() |
| 57 | + print("\nThere are {} datasources on site: ".format(pagination_item.total_available)) |
| 58 | + print([datasource.name for datasource in all_datasources]) |
41 | 59 |
|
42 | | - # Publish datasource if publish flag is set (-publish, -p) |
43 | | - if args.publish: |
44 | | - if default_project is not None: |
45 | | - new_datasource = TSC.DatasourceItem(default_project.id) |
46 | | - new_datasource = server.datasources.publish(new_datasource, args.publish, server.PublishMode.Overwrite) |
47 | | - print("Datasource published. ID: {}".format(new_datasource.id)) |
48 | | - else: |
49 | | - print("Publish failed. Could not find the default project.") |
| 60 | + if all_datasources: |
| 61 | + # Pick one datasource from the list |
| 62 | + sample_datasource = all_datasources[0] |
50 | 63 |
|
51 | | - # Gets all datasource items |
52 | | - all_datasources, pagination_item = server.datasources.get() |
53 | | - print("\nThere are {} datasources on site: ".format(pagination_item.total_available)) |
54 | | - print([datasource.name for datasource in all_datasources]) |
| 64 | + # Populate connections |
| 65 | + server.datasources.populate_connections(sample_datasource) |
| 66 | + print("\nConnections for {}: ".format(sample_datasource.name)) |
| 67 | + print(["{0}({1})".format(connection.id, connection.datasource_name) |
| 68 | + for connection in sample_datasource.connections]) |
55 | 69 |
|
56 | | - if all_datasources: |
57 | | - # Pick one datasource from the list |
58 | | - sample_datasource = all_datasources[0] |
59 | 70 |
|
60 | | - # Populate connections |
61 | | - server.datasources.populate_connections(sample_datasource) |
62 | | - print("\nConnections for {}: ".format(sample_datasource.name)) |
63 | | - print(["{0}({1})".format(connection.id, connection.datasource_name) |
64 | | - for connection in sample_datasource.connections]) |
| 71 | +if __name__ == '__main__': |
| 72 | + main() |
0 commit comments