-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathlimit.go
57 lines (49 loc) · 1.28 KB
/
limit.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
package pushover
import (
"net/http"
"strconv"
"time"
)
// Limit represents the limitation of the application. This information is
// fetched when posting a new message.
// Headers example:
// X-Limit-App-Limit: 7500
// X-Limit-App-Remaining: 7496
// X-Limit-App-Reset: 1393653600
type Limit struct {
// Total number of messages you can send during a month.
Total int
// Remaining number of messages you can send until the next reset.
Remaining int
// NextReset is the time when all the app counters will be reseted.
NextReset time.Time
}
func newLimit(headers http.Header) (*Limit, error) {
headersStrings := []string{
"X-Limit-App-Limit",
"X-Limit-App-Remaining",
"X-Limit-App-Reset",
}
headersValues := map[string]int{}
for _, header := range headersStrings {
// Check if the header is present
h, ok := headers[header]
if !ok {
return nil, ErrInvalidHeaders
}
// The header must have only one element
if len(h) != 1 {
return nil, ErrInvalidHeaders
}
i, err := strconv.Atoi(h[0])
if err != nil {
return nil, err
}
headersValues[header] = i
}
return &Limit{
Total: headersValues["X-Limit-App-Limit"],
Remaining: headersValues["X-Limit-App-Remaining"],
NextReset: time.Unix(int64(headersValues["X-Limit-App-Reset"]), 0),
}, nil
}