forked from SparkPost/gosparkpost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_events.go
169 lines (140 loc) · 4.27 KB
/
message_events.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
package gosparkpost
import (
"context"
"encoding/json"
"fmt"
"net/url"
"strings"
"github.com/SparkPost/gosparkpost/events"
"github.com/pkg/errors"
)
// https://www.sparkpost.com/api#/reference/message-events
var (
MessageEventsPathFormat = "/api/v%d/message-events"
MessageEventsSamplesPathFormat = "/api/v%d/message-events/events/samples"
)
type EventsPage struct {
Events events.Events
TotalCount int
Errors []interface{}
NextPage string
PrevPage string
FirstPage string
LastPage string
Client *Client `json:"-"`
Params map[string]string `json:"-"`
}
// https://developers.sparkpost.com/api/#/reference/message-events/events-samples/search-for-message-events
func (c *Client) MessageEventsSearch(page *EventsPage) (*Response, error) {
return c.MessageEventsSearchContext(context.Background(), page)
}
// MessageEventsSearchContext is the same as MessageEventsSearch, and it accepts a context.Context
func (c *Client) MessageEventsSearchContext(ctx context.Context, page *EventsPage) (*Response, error) {
path := fmt.Sprintf(MessageEventsPathFormat, c.Config.ApiVersion)
url, err := url.Parse(c.Config.BaseUrl + path)
if err != nil {
return nil, errors.Wrap(err, "parsing url")
}
if len(page.Params) > 0 {
q := url.Query()
for k, v := range page.Params {
q.Add(k, v)
}
url.RawQuery = q.Encode()
}
// Send off our request
res, err := c.HttpGetJson(ctx, url.String(), page)
if err != nil {
return res, err
}
page.Client = c
return res, nil
}
// Next returns the next page of results from a previous MessageEventsSearch call
func (page *EventsPage) Next() (*EventsPage, *Response, error) {
return page.NextContext(context.Background())
}
// NextContext is the same as Next, and it accepts a context.Context
func (page *EventsPage) NextContext(ctx context.Context) (*EventsPage, *Response, error) {
var nextPage EventsPage
if page.NextPage == "" {
return nil, nil, nil
}
// Send off our request
res, err := page.Client.HttpGetJson(ctx, page.Client.Config.BaseUrl+page.NextPage, &nextPage)
if err != nil {
return nil, res, err
}
nextPage.Client = page.Client
return &nextPage, res, nil
}
func (page *EventsPage) UnmarshalJSON(data []byte) error {
// Clear object.
*page = EventsPage{}
// Object with array of events and cursors is being sent on Message Events.
var resultsWrapper struct {
RawEvents []json.RawMessage `json:"results"`
TotalCount int `json:"total_count,omitempty"`
Links []struct {
Href string `json:"href"`
Rel string `json:"rel"`
} `json:"links,omitempty"`
Errors []interface{} `json:"errors,omitempty"`
}
err := json.Unmarshal(data, &resultsWrapper)
if err != nil {
return err
}
page.Events, err = events.ParseRawJSONEvents(resultsWrapper.RawEvents)
if err != nil {
return err
}
page.Errors = resultsWrapper.Errors
page.TotalCount = resultsWrapper.TotalCount
for _, link := range resultsWrapper.Links {
switch link.Rel {
case "next":
page.NextPage = link.Href
case "previous":
page.PrevPage = link.Href
case "first":
page.FirstPage = link.Href
case "last":
page.LastPage = link.Href
}
}
return nil
}
// EventSamples requests a list of example event data.
func (c *Client) EventSamples(types []string) (*events.Events, *Response, error) {
return c.EventSamplesContext(context.Background(), types)
}
// EventSamplesContext is the same as EventSamples, and it accepts a context.Context
func (c *Client) EventSamplesContext(ctx context.Context, types []string) (*events.Events, *Response, error) {
path := fmt.Sprintf(MessageEventsSamplesPathFormat, c.Config.ApiVersion)
url, err := url.Parse(c.Config.BaseUrl + path)
if err != nil {
return nil, nil, errors.Wrap(err, "parsing url")
}
// Filter out types.
if types != nil {
// validate types
for _, etype := range types {
if !events.ValidEventType(etype) {
return nil, nil, fmt.Errorf("Invalid event type [%s]", etype)
}
}
// get the query string object so we can modify it
q := url.Query()
// add the requested events and re-encode
q.Set("events", strings.Join(types, ","))
url.RawQuery = q.Encode()
}
// Send off our request
var events events.Events
res, err := c.HttpGetJson(ctx, url.String(), &events)
if err != nil {
return nil, res, err
}
return &events, res, nil
}