-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathmanage_webhooks.py
More file actions
executable file
·96 lines (77 loc) · 2.66 KB
/
manage_webhooks.py
File metadata and controls
executable file
·96 lines (77 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
"""
An example script showing ways to manage your webhooks using the
python-asana client library, see README.md or run ./manage_webhooks.py -h
for usage examples
"""
import argparse
import os
import asana
WORKSPACE_ID = "15793206719"
client = asana.Client.access_token(os.environ.get("ASANA_ACCESS_TOKEN"))
WORKSPACE_NAME = client.workspaces.find_by_id(WORKSPACE_ID).get("name")
def list_webhooks():
print(
"Displaying webhooks you own associated with workspace: {}".format(
WORKSPACE_NAME
)
)
webhooks = client.webhooks.get_all({"workspace": WORKSPACE_ID})
for w in webhooks:
print(w)
def delete_webhook(gid):
print("Deleting webhook: {}".format(gid))
client.webhooks.delete_by_id(gid)
def delete_all_webhooks():
print(
"Deleting all webhooks you own associated with workspace: {}".format(
WORKSPACE_NAME
)
)
webhooks = client.webhooks.get_all({"workspace": WORKSPACE_ID})
for w in webhooks:
client.webhooks.delete_by_id(w.get("gid"))
def create_webhook(resource, target):
print(
"Creating webhook on {}, make sure that the server at {} is ready to accept requests!".format(
resource, target
)
)
client.webhooks.create({"resource": resource, "target": target})
if __name__ == "__main__":
parser = argparse.ArgumentParser()
subparsers = parser.add_subparsers(
help="What you want to do with Asana webhooks", dest="command", required=True
)
list_parser = subparsers.add_parser("list", help="List existing webhooks")
create_parser = subparsers.add_parser("create", help="Create a new webhook")
create_parser.add_argument(
"--resource",
type=str,
help="A resource ID to subscribe to. The resource can be a task or project",
required=True,
)
create_parser.add_argument(
"--target",
type=str,
help="The webhook URL to receive the HTTP POST",
required=True,
)
delete_parser = subparsers.add_parser(
"delete", help="Delete one or all existing webhooks"
)
delete_group = delete_parser.add_mutually_exclusive_group(required=True)
delete_group.add_argument(
"--id", type=str, help="The ID of the webhook you want to delete"
)
delete_group.add_argument("--all", action="store_true")
args = parser.parse_args()
if args.command == "list":
list_webhooks()
elif args.command == "create":
create_webhook(args.resource, args.target)
elif args.command == "delete":
if args.all:
delete_all_webhooks()
else:
delete_webhook(args.id)