-
Notifications
You must be signed in to change notification settings - Fork 23
/
transactional.go
94 lines (80 loc) · 2.11 KB
/
transactional.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
package customerio
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"time"
)
type TransactionalType int
const (
TransactionalTypeEmail = 0
TransactionalTypePush = 1
)
var typeToApi = map[TransactionalType]string{
TransactionalTypeEmail: "email",
TransactionalTypePush: "push",
}
var ErrInvalidTransactionalMessageType = errors.New("unknown transactional message type")
func (c *APIClient) sendTransactional(ctx context.Context, typ TransactionalType, req interface{}) (*TransactionalResponse, error) {
api, ok := typeToApi[typ]
if !ok {
return nil, ErrInvalidTransactionalMessageType
}
body, statusCode, err := c.doRequest(ctx, "POST", fmt.Sprintf("/v1/send/%s", api), req)
if err != nil {
return nil, err
}
if statusCode != http.StatusOK {
var meta struct {
Meta struct {
Err string `json:"error"`
} `json:"meta"`
}
if err := json.Unmarshal(body, &meta); err != nil {
return nil, &TransactionalError{
StatusCode: statusCode,
Err: string(body),
}
}
return nil, &TransactionalError{
StatusCode: statusCode,
Err: meta.Meta.Err,
}
}
var resp TransactionalResponse
if err := json.Unmarshal(body, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// TransactionalResponse is a response to the send of a transactional message.
type TransactionalResponse struct {
// DeliveryID is a unique id for the given message.
DeliveryID string `json:"delivery_id"`
// QueuedAt is when the message was queued.
QueuedAt time.Time `json:"queued_at"`
}
func (t *TransactionalResponse) UnmarshalJSON(b []byte) error {
var r struct {
DeliveryID string `json:"delivery_id"`
QueuedAt int64 `json:"queued_at"`
}
if err := json.Unmarshal(b, &r); err != nil {
return err
}
t.DeliveryID = r.DeliveryID
t.QueuedAt = time.Unix(r.QueuedAt, 0)
return nil
}
// TransactionalError is returned if a transactional message fails to send.
type TransactionalError struct {
// Err is a more specific error message.
Err string
// StatusCode is the http status code for the error.
StatusCode int
}
func (e *TransactionalError) Error() string {
return e.Err
}