-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize_webhook.py
76 lines (56 loc) · 1.99 KB
/
initialize_webhook.py
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
import httpx
from flask_loguru import logger
from config import settings
def initialize_webhook():
url = f"https://api.trello.com/1/tokens/{settings.TRELLO_TOKEN}/webhooks"
try:
headers = {"Accept": "application/json"}
param = {"key": settings.TRELLO_KEY, "token": settings.TRELLO_TOKEN}
rsp = httpx.get(url, headers=headers, params={**param})
rsp.raise_for_status()
webhooks = rsp.json()
except httpx.HTTPStatusError as exc:
webhooks = None
logger.error(exc)
if webhooks:
for webhook in webhooks:
if webhook["idModel"] == settings.BOARD_ID:
if not is_webhook_updated(webhook):
delete_webhook(webhook["id"])
break
create_webhook()
def delete_webhook(webhook_id):
delete_webhook_url = (
f"https://api.trello.com/1/tokens/"
f"{settings.TRELLO_TOKEN}/webhooks/{webhook_id}"
)
try:
param = {"key": settings.TRELLO_KEY, "token": settings.TRELLO_TOKEN}
rsp = httpx.delete(delete_webhook_url, params={**param})
rsp.raise_for_status()
except httpx.HTTPStatusError as exc:
logger.error(exc)
def is_webhook_updated(webhook):
return webhook["callbackURL"] == f"{settings.BASE_URL}/board_webhook"
def create_webhook():
import requests
import json
url = f"https://api.trello.com/1/tokens/{settings.TRELLO_TOKEN}/webhooks"
headers = {"Accept": "application/json"}
query = {
"callbackURL": f"{settings.BASE_URL}/board_webhook",
"idModel": settings.BOARD_ID,
"key": settings.TRELLO_KEY,
"token": settings.TRELLO_TOKEN,
}
response = requests.request("POST", url, headers=headers, params=query)
return_json = json.dumps(
json.loads(response.text),
sort_keys=True,
indent=2,
separators=(",", ": "),
)
logger.info(
f"Webhook created successfully:\n {return_json}"
)
initialize_webhook()