forked from SparkPost/gosparkpost
-
Notifications
You must be signed in to change notification settings - Fork 1
/
del_metrics.go
121 lines (103 loc) · 4.55 KB
/
del_metrics.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
package gosparkpost
import (
"context"
"encoding/json"
"fmt"
"net/url"
"github.com/pkg/errors"
)
var MetricsPathFormat = "/api/v%d/metrics/deliverability"
type MetricItem struct {
CountInjected int `json:"count_injected"`
CountBounce int `json:"count_bounce,omitempty"`
CountRejected int `json:"count_rejected,omitempty"`
CountDelivered int `json:"count_delivered,omitempty"`
CountDeliveredFirst int `json:"count_delivered_first,omitempty"`
CountDeliveredSubsequent int `json:"count_delivered_subsequent,omitempty"`
TotalDeliveryTimeFirst int `json:"total_delivery_time_first,omitempty"`
TotalDeliveryTimeSubsequent int `json:"total_delivery_time_subsequent,omitempty"`
TotalMsgVolume int `json:"total_msg_volume,omitempty"`
CountPolicyRejection int `json:"count_policy_rejection,omitempty"`
CountGenerationRejection int `json:"count_generation_rejection,omitempty"`
CountGenerationFailed int `json:"count_generation_failed,omitempty"`
CountInbandBounce int `json:"count_inband_bounce,omitempty"`
CountOutofbandBounce int `json:"count_outofband_bounce,omitempty"`
CountSoftBounce int `json:"count_soft_bounce,omitempty"`
CountHardBounce int `json:"count_hard_bounce,omitempty"`
CountBlockBounce int `json:"count_block_bounce,omitempty"`
CountAdminBounce int `json:"count_admin_bounce,omitempty"`
CountUndeterminedBounce int `json:"count_undetermined_bounce,omitempty"`
CountDelayed int `json:"count_delayed,omitempty"`
CountDelayedFirst int `json:"count_delayed_first,omitempty"`
CountRendered int `json:"count_rendered,omitempty"`
CountUniqueRendered int `json:"count_unique_rendered,omitempty"`
CountUniqueConfirmedOpened int `json:"count_unique_confirmed_opened,omitempty"`
CountClicked int `json:"count_clicked,omitempty"`
CountUniqueClicked int `json:"count_unique_clicked,omitempty"`
CountTargeted int `json:"count_targeted,omitempty"`
CountSent int `json:"count_sent,omitempty"`
CountAccepted int `json:"count_accepted,omitempty"`
CountSpamComplaint int `json:"count_spam_complaint,omitempty"`
Domain string `json:"domain,omitempty"`
CampaignId string `json:"campaign_id,omitempty"`
TemplateId string `json:"template_id,omitempty"`
TimeStamp string `json:"ts,omitempty"`
WatchedDomain string `json:"watched_domain,omitempty"`
Binding string `json:"binding,omitempty"`
BindingGroup string `json:"binding_group,omitempty"`
}
type Metrics struct {
Results []MetricItem `json:"results,omitempty"`
TotalCount int `json:"total_count,omitempty"`
Links []map[string]string `json:"links,omitempty"`
Errors []interface{} `json:"errors,omitempty"`
ExtraPath string `json:"-"`
Params map[string]string `json:"-"`
}
// https://developers.sparkpost.com/api/#/reference/metrics/deliverability-metrics-by-domain
func (c *Client) QueryMetrics(m *Metrics) (*Response, error) {
return c.QueryMetricsContext(context.Background(), m)
}
func (c *Client) QueryMetricsContext(ctx context.Context, m *Metrics) (*Response, error) {
var finalUrl string
path := fmt.Sprintf(MetricsPathFormat, c.Config.ApiVersion)
if m.ExtraPath != "" {
path = fmt.Sprintf("%s/%s", path, m.ExtraPath)
}
if m.Params == nil || len(m.Params) == 0 {
finalUrl = fmt.Sprintf("%s%s", c.Config.BaseUrl, path)
} else {
params := url.Values{}
for k, v := range m.Params {
params.Add(k, v)
}
finalUrl = fmt.Sprintf("%s%s?%s", c.Config.BaseUrl, path, params.Encode())
}
return m.doMetricsRequest(ctx, c, finalUrl)
}
func (m *Metrics) doMetricsRequest(ctx context.Context, c *Client, finalUrl string) (*Response, error) {
// Send off our request
res, err := c.HttpGet(ctx, finalUrl)
if err != nil {
return res, err
}
// Assert that we got a JSON Content-Type back
if err = res.AssertJson(); err != nil {
return res, err
}
err = res.ParseResponse()
if err != nil {
return res, err
}
// Get the Content
bodyBytes, err := res.ReadBody()
if err != nil {
return res, err
}
// Parse expected response structure
err = json.Unmarshal(bodyBytes, m)
if err != nil {
return res, errors.Wrap(err, "unmarshaling response")
}
return res, nil
}