-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathwebhooks.py
More file actions
59 lines (41 loc) · 1.55 KB
/
webhooks.py
File metadata and controls
59 lines (41 loc) · 1.55 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
import mailtrap as mt
from mailtrap.models.common import DeletedObject
from mailtrap.models.webhooks import Webhook
from mailtrap.models.webhooks import WebhookWithSecret
API_TOKEN = "YOUR_API_TOKEN"
ACCOUNT_ID = "YOUR_ACCOUNT_ID"
client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID)
webhooks_api = client.webhooks_api.webhooks
def list_webhooks() -> list[Webhook]:
return webhooks_api.get_list()
def get_webhook(webhook_id: int) -> Webhook:
return webhooks_api.get_by_id(webhook_id=webhook_id)
def create_webhook() -> WebhookWithSecret:
# The signing_secret is only returned once on creation — store it
# securely and use it to verify webhook signatures (HMAC SHA-256).
return webhooks_api.create(
mt.CreateWebhookParams(
url="https://example.com/mailtrap/webhooks",
webhook_type="email_sending",
sending_stream="transactional",
event_types=["delivery", "bounce"],
)
)
def update_webhook(webhook_id: int) -> Webhook:
return webhooks_api.update(
webhook_id=webhook_id,
webhook_params=mt.UpdateWebhookParams(active=False),
)
def delete_webhook(webhook_id: int) -> DeletedObject:
return webhooks_api.delete(webhook_id=webhook_id)
if __name__ == "__main__":
webhooks = list_webhooks()
print(webhooks)
created = create_webhook()
print(created)
fetched = get_webhook(created.id)
print(fetched)
updated = update_webhook(created.id)
print(updated)
deleted = delete_webhook(created.id)
print(deleted)