-
Notifications
You must be signed in to change notification settings - Fork 0
/
postmark.go
221 lines (201 loc) · 4.96 KB
/
postmark.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package postmark
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"
"unicode/utf8"
)
type Message struct {
From string `json:"From"`
To string `json:"To"`
ReplyTo string `json:"ReplyTo,omitempty"`
Cc string `json:"Cc,omitempty"`
Bcc string `json:"Bcc,omitempty"`
Subject string `json:"Subject"`
Tag string `json:"Tag,omitempty"`
TextBody string `json:"TextBody,omitempty"`
HtmlBody string `json:"HtmlBody,omitempty"`
MessageStream string `json:"MessageStream"`
TemplateId int64 `json:"TemplateId,omitempty"`
TemplateModel any `json:"TemplateModel,omitempty"`
}
type Credentials struct {
ServerAccessToken string
}
type Caller struct {
HTTPClient *http.Client
Credentials
}
func (c *Caller) Send(ctx context.Context, msg *Message) error {
return c.call(ctx, "Send", msg)
}
func (c *Caller) call(ctx context.Context, callID string, input *Message) error {
inputRaw := must(json.Marshal(input))
r := must(http.NewRequestWithContext(ctx, "POST", "https://api.postmarkapp.com/email", bytes.NewReader(inputRaw)))
r.Header.Set("Content-Type", "application/json")
r.Header.Set("Accept", "application/json")
if c.ServerAccessToken != "" {
r.Header.Set("X-Postmark-Server-Token", c.ServerAccessToken)
}
log.Printf("postmark.%s: %s", callID, curl(r.Method, r.URL.String(), r.Header, inputRaw))
resp, err := c.HTTPClient.Do(r)
if err != nil {
return &Error{
CallID: callID,
IsNetwork: true,
Cause: err,
}
}
defer resp.Body.Close()
outputRaw, err := io.ReadAll(resp.Body)
if err != nil {
return &Error{
CallID: callID,
IsNetwork: true,
Cause: err,
}
}
var body response
err = json.Unmarshal(outputRaw, &resp)
if err != nil {
return &Error{
CallID: callID,
IsNetwork: len(outputRaw) == 0 || outputRaw[0] != '{',
StatusCode: resp.StatusCode,
Message: "error unmashalling body",
RawResponseBody: outputRaw,
PrintResponseBody: true,
Cause: err,
}
}
if (resp.StatusCode >= 200 && resp.StatusCode <= 299) && body.ErrorCode == 0 {
return nil
} else {
return &Error{
CallID: callID,
IsNetwork: len(outputRaw) == 0 || outputRaw[0] != '{',
StatusCode: resp.StatusCode,
ErrorCode: body.ErrorCode,
Message: body.Message,
RawResponseBody: outputRaw,
PrintResponseBody: true,
Cause: err,
}
}
}
type response struct {
ErrorCode int `json:"ErrorCode"`
Message string `json:"Message"`
}
func must[T any](v T, err error) T {
if err != nil {
panic(err)
}
return v
}
func curl(method, path string, headers http.Header, body []byte) string {
var buf strings.Builder
buf.WriteString("curl")
buf.WriteString(" -i")
if method != "GET" {
buf.WriteString(" -X")
buf.WriteString(method)
} else {
body = nil
}
for k, vv := range headers {
for _, v := range vv {
buf.WriteString(" -H '")
buf.WriteString(k)
buf.WriteString(": ")
buf.WriteString(v)
buf.WriteString("'")
}
}
if body != nil {
buf.WriteString(" -d ")
buf.WriteString(shellQuote(string(body)))
}
buf.WriteString(" '")
buf.WriteString(path)
buf.WriteString("'")
return buf.String()
}
func shellQuote(source string) string {
const specialChars = "\\'\"`${[|&;<>()*?! \t\n~"
const specialInDouble = "$\\\""
var buf strings.Builder
buf.Grow(len(source) + 10)
// pick quotation style, preferring single quotes
if !strings.ContainsAny(source, specialChars) {
buf.WriteString(source)
} else if !strings.ContainsRune(source, '\'') {
buf.WriteByte('\'')
buf.WriteString(source)
buf.WriteByte('\'')
} else {
buf.WriteByte('"')
for {
i := strings.IndexAny(source, specialInDouble)
if i < 0 {
break
}
buf.WriteString(source[:i])
buf.WriteByte('\\')
buf.WriteByte(source[i])
source = source[i+1:]
}
buf.WriteString(source)
buf.WriteByte('"')
}
return buf.String()
}
type Error struct {
CallID string
IsNetwork bool
StatusCode int
ErrorCode int
Message string
RawResponseBody []byte
PrintResponseBody bool
Cause error
}
func (e *Error) Error() string {
var buf strings.Builder
fmt.Fprintf(&buf, "%s: HTTP %d", e.CallID, e.StatusCode)
if e.IsNetwork {
buf.WriteString("network: ")
}
if e.ErrorCode != 0 {
buf.WriteString(": ")
buf.WriteString(strconv.Itoa(e.ErrorCode))
}
if e.Message != "" {
buf.WriteString(": ")
buf.WriteString(e.Message)
}
if e.Cause != nil {
buf.WriteString(": ")
buf.WriteString(e.Cause.Error())
}
if e.PrintResponseBody {
buf.WriteString(" // response: ")
if len(e.RawResponseBody) == 0 {
buf.WriteString("<empty>")
} else if utf8.Valid(e.RawResponseBody) {
buf.Write(e.RawResponseBody)
} else {
return fmt.Sprintf("<binary %d bytes>", len(e.RawResponseBody))
}
}
return buf.String()
}
func (e *Error) Unwrap() error {
return e.Cause
}