-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtemplates.go
101 lines (82 loc) · 2.55 KB
/
templates.go
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
97
98
99
100
101
package mailersend
import (
"context"
"fmt"
"net/http"
"time"
)
const templateBasePath = "/templates"
type TemplateService service
// templateRoot format of template response
type templateRoot struct {
Data []template `json:"data"`
Links Links `json:"links"`
Meta Meta `json:"meta"`
}
type template struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
ImagePath string `json:"image_path"`
CreatedAt string `json:"created_at"`
}
type singleTemplateRoot struct {
Data singleTemplate `json:"data"`
}
type singleTemplate struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
ImagePath string `json:"image_path"`
CreatedAt time.Time `json:"created_at"`
Category interface{} `json:"category"`
Domain Domain `json:"domain"`
TemplateStats templateStats `json:"template_stats"`
}
type templateStats struct {
Total int `json:"total"`
Queued int `json:"queued"`
Sent int `json:"sent"`
Rejected int `json:"rejected"`
Delivered int `json:"delivered"`
LastEmailSentAt time.Time `json:"last_email_sent_at"`
}
// ListTemplateOptions - modifies the behavior of TemplateService.List Method
type ListTemplateOptions struct {
DomainID string `url:"domain_id,omitempty"`
Page int `url:"page,omitempty"`
Limit int `url:"limit,omitempty"`
}
func (s *TemplateService) List(ctx context.Context, options *ListTemplateOptions) (*templateRoot, *Response, error) {
req, err := s.client.newRequest(http.MethodGet, templateBasePath, options)
if err != nil {
return nil, nil, err
}
root := new(templateRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *TemplateService) Get(ctx context.Context, templateID string) (*singleTemplateRoot, *Response, error) {
path := fmt.Sprintf("%s/%s", templateBasePath, templateID)
req, err := s.client.newRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}
root := new(singleTemplateRoot)
res, err := s.client.do(ctx, req, root)
if err != nil {
return nil, res, err
}
return root, res, nil
}
func (s *TemplateService) Delete(ctx context.Context, templateID string) (*Response, error) {
path := fmt.Sprintf("%s/%s", templateBasePath, templateID)
req, err := s.client.newRequest(http.MethodDelete, path, nil)
if err != nil {
return nil, err
}
return s.client.do(ctx, req, nil)
}