-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate.go
More file actions
89 lines (79 loc) · 2.13 KB
/
template.go
File metadata and controls
89 lines (79 loc) · 2.13 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
package plunk
import (
"context"
"net/http"
"net/url"
"strconv"
)
// Template type constants.
const (
TemplateTransactional = "TRANSACTIONAL"
TemplateMarketing = "MARKETING"
)
// Template represents a Plunk email template.
type Template struct {
ID string `json:"id"`
Name string `json:"name"`
Subject string `json:"subject"`
Body string `json:"body"`
Type string `json:"type"`
CreatedAt string `json:"createdAt"`
}
// CreateTemplateRequest is the request for [Client.CreateTemplate].
type CreateTemplateRequest struct {
Name string `json:"name"`
Subject string `json:"subject"`
Body string `json:"body"`
Type string `json:"type"`
}
// CreateTemplate creates a new email template. It requires a secret API key.
func (c *Client) CreateTemplate(ctx context.Context, req *CreateTemplateRequest) (*Template, error) {
var resp Template
if err := c.do(ctx, http.MethodPost, "/templates", req, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// ListTemplatesRequest is the request for [Client.ListTemplates].
type ListTemplatesRequest struct {
Limit int
Cursor string
Type string
Search string
}
// ListTemplatesResponse is the response from [Client.ListTemplates].
type ListTemplatesResponse struct {
Templates []Template `json:"templates"`
Total int `json:"total"`
Page int `json:"page"`
PageSize int `json:"pageSize"`
TotalPages int `json:"totalPages"`
}
// ListTemplates retrieves a paginated list of templates. It requires a secret
// API key.
func (c *Client) ListTemplates(ctx context.Context, req *ListTemplatesRequest) (*ListTemplatesResponse, error) {
q := url.Values{}
if req != nil {
if req.Limit > 0 {
q.Set("limit", strconv.Itoa(req.Limit))
}
if req.Cursor != "" {
q.Set("cursor", req.Cursor)
}
if req.Type != "" {
q.Set("type", req.Type)
}
if req.Search != "" {
q.Set("search", req.Search)
}
}
path := "/templates"
if len(q) > 0 {
path += "?" + q.Encode()
}
var resp ListTemplatesResponse
if err := c.do(ctx, http.MethodGet, path, nil, &resp); err != nil {
return nil, err
}
return &resp, nil
}