-
Notifications
You must be signed in to change notification settings - Fork 0
/
email.go
72 lines (60 loc) · 1.52 KB
/
email.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
package sendios
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
"net/http"
)
const (
CategorySystem = 1
CategoryTrigger = 2
)
type (
EmailRequest struct {
TypeID int `json:"type_id,omitempty"`
Category int `json:"category,omitempty"`
Data map[string]interface{} `json:"data"`
Meta map[string]interface{} `json:"meta,omitempty"`
}
)
type EmailResponse struct {
Meta `json:"_meta"`
Data struct {
Error string `json:"error"`
Queued bool `json:"queued"`
RTime int `json:"r_time"`
} `json:"data"`
}
// https://sendios.readme.io/reference/send-system-email
func (c *Client) SendEmail(r EmailRequest) (*EmailResponse, error) {
type req struct {
EmailRequest
ClientID int `json:"client_id"`
ProjectID int `json:"project_id"`
}
data, err := json.Marshal(req{
EmailRequest: r,
ClientID: c.Config.ClientID,
ProjectID: c.Config.Project,
})
if err != nil {
return nil, errors.Wrap(err, "marshal payload")
}
statusCode, body, err := c.makeRequest(http.MethodPost, "https://api.sendios.io/v1/push/system", bytes.NewReader(data))
if err != nil {
return nil, errors.Wrap(err, "send email")
}
if statusCode == http.StatusBadGateway {
return nil, errors.New("bad gateway")
}
var resp EmailResponse
if err = json.Unmarshal(body, &resp); err != nil {
fmt.Println(string(body))
return nil, errors.Wrap(err, "map email response")
}
if resp.Data.Error != "" {
return &resp, errors.New(resp.Data.Error)
}
return &resp, nil
}