-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtemplateless.go
130 lines (108 loc) · 2.88 KB
/
templateless.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package templateless
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
)
type ObjectId = string
type EmailResponsePreview struct {
Email string `json:"email"`
Preview string `json:"preview"`
}
type EmailResponse struct {
Emails []ObjectId `json:"emails"`
Previews *[]EmailResponsePreview `json:"previews"`
}
type Templateless struct {
apiKey string
domain string
}
func NewTemplateless(apiKey string) *Templateless {
domain := os.Getenv("TEMPLATELESS_DOMAIN")
if domain == "" {
domain = "https://api.templateless.com"
}
return &Templateless{
apiKey: apiKey,
domain: domain,
}
}
func (t *Templateless) Domain(domain string) *Templateless {
t.domain = domain
return t
}
func (t *Templateless) Send(email Email) ([]ObjectId, *CustomError) {
return t.SendMany([]Email{email})
}
func (t *Templateless) SendMany(emails []Email) ([]ObjectId, *CustomError) {
emailSlices := make([]EmailSlice, 0)
for _, email := range emails {
emailSlices = append(emailSlices, *email.ToSlice())
}
payload, err := json.Marshal(map[string][]EmailSlice{"payload": emailSlices})
if err != nil {
return nil, UnknownError()
}
client := &http.Client{}
req, err := http.NewRequest("POST", fmt.Sprintf("%s/v1/emails", t.domain), bytes.NewBuffer(payload))
if err != nil {
return nil, UnknownError()
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", t.apiKey))
req.Header.Set("Referer", "templateless-go v0")
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
return nil, UnknownError()
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, UnknownError()
}
switch resp.StatusCode {
case http.StatusUnauthorized:
return nil, UnauthorizedError()
case http.StatusForbidden:
return nil, ForbiddenError()
case http.StatusUnprocessableEntity:
type InvalidParameter struct {
Field string `json:"field"`
}
var e InvalidParameter
if err := json.Unmarshal(body, &e); err == nil {
return nil, InvalidParameterError(e.Field)
}
return nil, UnknownError()
case http.StatusBadRequest:
type BadRequest struct {
Code BadRequestCode `json:"code"`
Error string `json:"error"`
}
var e BadRequest
if err := json.Unmarshal(body, &e); err == nil {
return nil, BadRequestError(e.Code, e.Error)
}
return nil, UnknownError()
case http.StatusInternalServerError:
return nil, UnavailableError()
case http.StatusOK:
var emailResponse EmailResponse
if err := json.Unmarshal(body, &emailResponse); err != nil {
return nil, UnknownError()
}
if emailResponse.Previews != nil {
for _, preview := range *emailResponse.Previews {
log.Printf("Templateless [TEST MODE]: Emailed %s, preview: https://tmpl.sh/%s\n",
preview.Email, preview.Preview)
}
}
return emailResponse.Emails, nil
default:
return nil, UnknownError()
}
}