-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathratelimit.go
50 lines (40 loc) · 977 Bytes
/
ratelimit.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
// Copyright 2021-present Airheart, Inc. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.
package duffel
import (
"net/http"
"strconv"
"time"
)
type (
RateLimit struct {
Limit int
Remaining int
ResetAt time.Time
Period time.Duration
}
)
func parseRateLimit(resp *http.Response) (*RateLimit, error) {
rl := &RateLimit{}
var err error
rl.Limit, err = strconv.Atoi(resp.Header.Get("Ratelimit-Limit"))
if err != nil {
return nil, err
}
rl.Remaining, err = strconv.Atoi(resp.Header.Get("Ratelimit-Remaining"))
if err != nil {
return nil, err
}
resetAt, err := time.Parse(time.RFC1123, resp.Header.Get("Ratelimit-Reset"))
if err != nil {
return nil, err
}
date, err := time.Parse(time.RFC1123, resp.Header.Get("Date"))
if err != nil {
return nil, err
}
rl.ResetAt = resetAt
rl.Period = resetAt.Sub(date)
return rl, nil
}