-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
rate_limit.go
55 lines (46 loc) · 1.3 KB
/
rate_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
package backlog
import (
"context"
"time"
)
// LimitStatus : limit status
type LimitStatus struct {
Limit *int `json:"limit,omitempty"`
Remaining *int `json:"remaining,omitempty"`
Reset *int `json:"reset,omitempty"`
}
// ResetAsTime returns reset as time
func (ls *LimitStatus) ResetAsTime() time.Time {
if ls.Reset == nil {
return time.Time{}
}
return time.Unix(int64(*ls.Reset), 0)
}
// RateLimit : rate limit
type RateLimit struct {
Read *LimitStatus `json:"read,omitempty"`
Update *LimitStatus `json:"update,omitempty"`
Search *LimitStatus `json:"search,omitempty"`
Icon *LimitStatus `json:"icon,omitempty"`
}
// ResponseRateLimit : rate limit API response
type ResponseRateLimit struct {
RateLimit *RateLimit `json:"rateLimit,omitempty"`
}
// GetRateLimit returns the rate limit
func (c *Client) GetRateLimit() (*RateLimit, error) {
return c.GetRateLimitContext(context.Background())
}
// GetRateLimitContext returns the rate limit
func (c *Client) GetRateLimitContext(ctx context.Context) (*RateLimit, error) {
u := "/api/v2/rateLimit"
req, err := c.NewRequest("GET", u, nil)
if err != nil {
return nil, err
}
responseRateLimit := new(ResponseRateLimit)
if err := c.Do(ctx, req, &responseRateLimit); err != nil {
return nil, err
}
return responseRateLimit.RateLimit, nil
}