Skip to content

Commit 94d6427

Browse files
committed
feat: retreive templates with contents
1 parent 7a8c774 commit 94d6427

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

cmd/sendgridman/main.go

+126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"os"
8+
9+
"github.com/sendgrid/rest"
10+
sendgrid "github.com/sendgrid/sendgrid-go"
11+
)
12+
13+
const sendgridHost = "https://api.sendgrid.com"
14+
15+
type sendgridman struct {
16+
host string // a sendgrid service hostname
17+
apiKey string // must be exactly 39 symbols
18+
}
19+
20+
type templateInfo struct {
21+
ID string `json:"id"`
22+
Name string `json:"name"`
23+
Versions []templateVersionInfo `json:"versions"`
24+
}
25+
26+
type templateVersionInfo struct {
27+
ID string `json:"id"`
28+
TemplateID string `json:"template_id"`
29+
Active uint `json:"active"`
30+
Name string `json:"name"`
31+
UpdatedAt string `json:"updated_at"`
32+
Editor string `json:"editor"`
33+
}
34+
35+
// see https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/templates.html#-GET
36+
type mailTemplate struct {
37+
ID string `json:"id"`
38+
Name string `json:"name"`
39+
Versions []mailTemplateVersion `json:"versions"`
40+
}
41+
42+
// see https://sendgrid.com/docs/API_Reference/Web_API_v3/Transactional_Templates/templates.html#-GET
43+
type mailTemplateVersion struct {
44+
templateVersionInfo
45+
46+
Subject string `json:"subject"`
47+
HTMLContent string `json:"html_content"`
48+
PlainContent string `json:"plain_content"`
49+
}
50+
51+
// returns a list of dynamic templates descriptions (without a template code and test data)
52+
func (s sendgridman) getTemplateList() (list []templateInfo, err error) {
53+
request := sendgrid.GetRequest(s.apiKey, "/v3/templates", s.host)
54+
request.Method = "GET"
55+
queryParams := make(map[string]string)
56+
queryParams["generations"] = "dynamic" // only dynamic templates
57+
request.QueryParams = queryParams
58+
response, err := sendgrid.API(request)
59+
if err != nil {
60+
return list, fmt.Errorf("load templates fail: %w", err)
61+
}
62+
// TODO: check status code?
63+
// fmt.Println(response.StatusCode)
64+
// fmt.Println(response.Headers)
65+
66+
type templateList struct {
67+
Templates []templateInfo `json:"templates"`
68+
}
69+
tl := &templateList{}
70+
err = json.Unmarshal([]byte(response.Body), tl)
71+
if err != nil {
72+
return list, fmt.Errorf("parse templates json fail: %w", err)
73+
}
74+
return tl.Templates, nil
75+
76+
}
77+
78+
func (s sendgridman) getTemplate(templateID string) (template mailTemplate, err error) {
79+
request := sendgrid.GetRequest(s.apiKey, fmt.Sprintf("/v3/templates/%s", templateID), s.host)
80+
request.Method = "GET"
81+
var response *rest.Response
82+
response, err = sendgrid.API(request)
83+
if err != nil {
84+
return template, fmt.Errorf("load template ID='%s' fail: %w", templateID, err)
85+
}
86+
// TODO: check status code?
87+
// fmt.Println(response.StatusCode)
88+
// fmt.Println(response.Headers)
89+
err = json.Unmarshal([]byte(response.Body), &template)
90+
if err != nil {
91+
return template, fmt.Errorf("parse template ID='%s' fail: %w", templateID, err)
92+
}
93+
return template, nil
94+
}
95+
96+
// func printTemplateList()
97+
98+
func main() {
99+
var apiKey string
100+
flag.StringVar(&apiKey, "apikey", "", "a sendgrid APIKey (not API Key ID!) to access service")
101+
102+
// if len(apiKey) != 39 {
103+
// fmt.Println("Error: Invalid apikey len, must be 39 symbols exactly")
104+
// os.Exit(2)
105+
// }
106+
flag.Parse()
107+
sm := &sendgridman{apiKey: apiKey, host: sendgridHost}
108+
tl, err := sm.getTemplateList()
109+
if err != nil {
110+
fmt.Printf("Error: getTemplateList %s", err.Error())
111+
os.Exit(1)
112+
}
113+
fmt.Printf("Found %d dynamic templates\n", len(tl))
114+
fmt.Println("Retreive templates data")
115+
for i, tplInfo := range tl {
116+
var tpl mailTemplate
117+
tpl, err = sm.getTemplate(tplInfo.ID)
118+
if err != nil {
119+
fmt.Printf("Error: failed to retreive template data ID=%s, %s", tplInfo.ID, err.Error())
120+
os.Exit(1)
121+
}
122+
fmt.Printf("%d. Template ID=%s\n %+v\n\n", i, tplInfo.ID, tpl)
123+
}
124+
fmt.Println("Retreive templates data: OK")
125+
// fmt.Printf("tpl list %+v\n", tl)
126+
}

go.mod

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module github.com/r3code/sendgrid-man
2+
3+
go 1.14
4+
5+
require (
6+
github.com/sendgrid/rest v2.4.1+incompatible
7+
github.com/sendgrid/sendgrid-go v3.5.0+incompatible
8+
)

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
github.com/sendgrid/rest v1.0.2 h1:xdfALkR1m9eqf41/zEnUmV0fw4b31ZzGZ4Dj5f2/w04=
2+
github.com/sendgrid/rest v2.4.1+incompatible h1:HDib/5xzQREPq34lN3YMhQtMkdXxS/qLp5G3k9a5++4=
3+
github.com/sendgrid/rest v2.4.1+incompatible/go.mod h1:kXX7q3jZtJXK5c5qK83bSGMdV6tsOE70KbHoqJls4lE=
4+
github.com/sendgrid/sendgrid-go v1.2.0 h1:2K3teZdhaPe12ftFyFL4AWDH4QmNPc+sCi6mWFx5+oo=
5+
github.com/sendgrid/sendgrid-go v3.5.0+incompatible h1:kosbgHyNVYVaqECDYvFVLVD9nvThweBd6xp7vaCT3GI=
6+
github.com/sendgrid/sendgrid-go v3.5.0+incompatible/go.mod h1:QRQt+LX/NmgVEvmdRw0VT/QgUn499+iza2FnDca9fg8=

0 commit comments

Comments
 (0)