|
| 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 | +} |
0 commit comments