-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtemplates.py
More file actions
85 lines (66 loc) · 2.08 KB
/
templates.py
File metadata and controls
85 lines (66 loc) · 2.08 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
from typing import Optional
import mailtrap as mt
from mailtrap.models.common import DeletedObject
from mailtrap.models.templates import EmailTemplate
API_TOKEN = "YOUR_API_TOKEN"
ACCOUNT_ID = "YOUR_ACCOUNT_ID"
client = mt.MailtrapClient(token=API_TOKEN, account_id=ACCOUNT_ID)
templates_api = client.email_templates_api.templates
def list_templates() -> list[EmailTemplate]:
return templates_api.get_list()
def create_template(
name: str,
subject: str,
category: str,
body_text: Optional[str] = None,
body_html: Optional[str] = None,
) -> EmailTemplate:
params = mt.CreateEmailTemplateParams(
name=name,
subject=subject,
category=category,
body_text=body_text,
body_html=body_html,
)
return templates_api.create(params)
def get_template(template_id: int) -> EmailTemplate:
return templates_api.get_by_id(template_id)
def update_template(
template_id: int,
name: Optional[str] = None,
subject: Optional[str] = None,
category: Optional[str] = None,
body_text: Optional[str] = None,
body_html: Optional[str] = None,
) -> EmailTemplate:
params = mt.UpdateEmailTemplateParams(
name=name,
subject=subject,
category=category,
body_text=body_text,
body_html=body_html,
)
return templates_api.update(template_id, params)
def delete_template(template_id: int) -> DeletedObject:
return templates_api.delete(template_id)
if __name__ == "__main__":
created = create_template(
name="Example Template",
subject="Hello",
category="transactional",
body_text="Hello world",
)
print(created)
templates = list_templates()
print(templates)
template = get_template(template_id=created.id)
print(template)
updated = update_template(
template_id=created.id,
name=f"{template.name}-updated",
subject=f"{template.subject}-updated",
body_text=f"{template.body_text}\nUpdated content.",
)
print(updated)
deleted = delete_template(template_id=created.id)
print(deleted)