|
| 1 | +#### |
| 2 | +# This script demonstrates how to use the Tableau Server Client |
| 3 | +# to interact with webhooks. It explores the different |
| 4 | +# functions that the Server API supports on webhooks. |
| 5 | +# |
| 6 | +# With no flags set, this sample will query all webhooks, |
| 7 | +# pick one webhook and print the name of the webhook. |
| 8 | +# Adding flags will demonstrate the specific feature |
| 9 | +# on top of the general operations. |
| 10 | +#### |
| 11 | + |
| 12 | +import argparse |
| 13 | +import getpass |
| 14 | +import logging |
| 15 | +import os.path |
| 16 | + |
| 17 | +import tableauserverclient as TSC |
| 18 | + |
| 19 | + |
| 20 | +def main(): |
| 21 | + |
| 22 | + parser = argparse.ArgumentParser(description='Explore webhook functions supported by the Server API.') |
| 23 | + parser.add_argument('--server', '-s', required=True, help='server address') |
| 24 | + parser.add_argument('--username', '-u', required=True, help='username to sign into server') |
| 25 | + parser.add_argument('--site', '-S', default=None) |
| 26 | + parser.add_argument('-p', default=None, help='password') |
| 27 | + parser.add_argument('--create', '-c', help='create a webhook') |
| 28 | + parser.add_argument('--logging-level', '-l', choices=['debug', 'info', 'error'], default='error', |
| 29 | + help='desired logging level (set to error by default)') |
| 30 | + |
| 31 | + args = parser.parse_args() |
| 32 | + |
| 33 | + |
| 34 | + if args.p is None: |
| 35 | + password = getpass.getpass("Password: ") |
| 36 | + else: |
| 37 | + password = args.p |
| 38 | + |
| 39 | + # Set logging level based on user input, or error by default |
| 40 | + logging_level = getattr(logging, args.logging_level.upper()) |
| 41 | + logging.basicConfig(level=logging_level) |
| 42 | + |
| 43 | + # SIGN IN |
| 44 | + tableau_auth = TSC.TableauAuth(args.username, password, args.site) |
| 45 | + print("Signing in to " + args.server + " [" + args.site + "] as " + args.username) |
| 46 | + server = TSC.Server(args.server) |
| 47 | + |
| 48 | + # Set http options to disable verifying SSL |
| 49 | + server.add_http_options({'verify': False}) |
| 50 | + |
| 51 | + server.use_server_version() |
| 52 | + |
| 53 | + with server.auth.sign_in(tableau_auth): |
| 54 | + |
| 55 | + # Publish webhook if publish flag is set (-publish, -p) |
| 56 | + if args.create: |
| 57 | + |
| 58 | + new_webhook = TSC.WebhookItem() |
| 59 | + new_webhook.name = args.create |
| 60 | + new_webhook.url = "https://ifttt.com/maker-url" |
| 61 | + new_webhook.event = "datasource-created" |
| 62 | + print(new_webhook) |
| 63 | + new_webhook = server.webhooks.create(new_webhook) |
| 64 | + print("Webhook created. ID: {}".format(new_webhook.id)) |
| 65 | + |
| 66 | + # Gets all webhook items |
| 67 | + all_webhooks, pagination_item = server.webhooks.get() |
| 68 | + print("\nThere are {} webhooks on site: ".format(pagination_item.total_available)) |
| 69 | + print([webhook.name for webhook in all_webhooks]) |
| 70 | + |
| 71 | + if all_webhooks: |
| 72 | + # Pick one webhook from the list and delete it |
| 73 | + sample_webhook = all_webhooks[0] |
| 74 | + # sample_webhook.delete() |
| 75 | + print("+++"+sample_webhook.name) |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == '__main__': |
| 81 | + main() |
0 commit comments