forked from chatwork/sendgrid-stats-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendgrid.go
89 lines (75 loc) · 2.25 KB
/
sendgrid.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"os"
"time"
)
const (
endpoint = "https://api.sendgrid.com/v3/stats"
)
type Metrics struct {
Blocks int64 `json:"blocks,omitempty"`
BounceDrops int64 `json:"bounce_drops,omitempty"`
Bounces int64 `json:"bounces,omitempty"`
Clicks int64 `json:"clicks,omitempty"`
Deferred int64 `json:"deferred,omitempty"`
Delivered int64 `json:"delivered,omitempty"`
InvalidEmails int64 `json:"invalid_emails,omitempty"`
Opens int64 `json:"opens,omitempty"`
Processed int64 `json:"processed,omitempty"`
Requests int64 `json:"requests,omitempty"`
SpamReportDrops int64 `json:"spam_report_drops,omitempty"`
SpamReports int64 `json:"spam_reports,omitempty"`
UniqueClicks int64 `json:"unique_clicks,omitempty"`
UniqueOpens int64 `json:"unique_opens,omitempty"`
UnsubscribeDrops int64 `json:"unsubscribe_drops,omitempty"`
Unsubscribes int64 `json:"unsubscribes,omitempty"`
}
type Stat struct {
Metrics *Metrics `json:"metrics,omitempty"`
}
type Statistics struct {
Date string `json:"date,omitempty"`
Stats []*Stat `json:"stats,omitempty"`
}
func collectByDate(time time.Time) ([]*Statistics, error) {
parsedURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}
layout := "2006-01-02"
date := time.Format(layout)
query := url.Values{}
query.Set("start_date", date)
query.Set("end_date", date)
query.Set("aggregated_by", "day")
parsedURL.RawQuery = query.Encode()
req, err := http.NewRequest(http.MethodGet, parsedURL.String(), nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", *sendGridAPIKey))
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
var reader io.Reader = res.Body
reader = io.TeeReader(reader, os.Stdout)
switch res.StatusCode {
case http.StatusTooManyRequests:
return nil, fmt.Errorf("API rate limit exceeded")
case http.StatusOK:
var stats []*Statistics
if err := json.NewDecoder(reader).Decode(&stats); err != nil {
return nil, err
}
return stats, nil
default:
return nil, fmt.Errorf("status code = %d, response = %s", res.StatusCode, res.Body)
}
}