-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstats.go
More file actions
38 lines (31 loc) · 1.06 KB
/
Copy pathstats.go
File metadata and controls
38 lines (31 loc) · 1.06 KB
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
package listennotes
import (
"net/http"
"strconv"
"time"
)
// ResponseStatistics will contain the stats information provided by API response headers
type ResponseStatistics struct {
FreeQuota int
Usage int
LatencySeconds float64
NextBillingDate time.Time
}
func parseStats(resp *http.Response) ResponseStatistics {
// Do not allow this to fail, just return null values if we cannot parse the response.
// We do this so that a POST that succeeds does not return an unrelated error.
stats := ResponseStatistics{}
if freeQuota, err := strconv.Atoi(resp.Header.Get(ResponseHeaderKeyFreeQuota)); err == nil {
stats.FreeQuota = freeQuota
}
if usage, err := strconv.Atoi(resp.Header.Get(ResponseHeaderKeyUsage)); err == nil {
stats.Usage = usage
}
if latency, err := strconv.ParseFloat(resp.Header.Get(ResponseHeaderKeyLatencySeconds), 64); err == nil {
stats.LatencySeconds = latency
}
if nextBill, err := time.Parse(TimeFormat, resp.Header.Get(ResponseHeaderKeyNextBillingDate)); err == nil {
stats.NextBillingDate = nextBill
}
return stats
}