forked from dghubble/go-twitter
-
Notifications
You must be signed in to change notification settings - Fork 17
/
rate_limits.go
68 lines (58 loc) · 2.4 KB
/
rate_limits.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
package twitter
import (
"net/http"
"github.com/dghubble/sling"
)
// RateLimitService provides methods for accessing Twitter rate limits
// API endpoints.
type RateLimitService struct {
sling *sling.Sling
}
// newRateLimitService returns a new RateLimitService.
func newRateLimitService(sling *sling.Sling) *RateLimitService {
return &RateLimitService{
sling: sling.Path("application/"),
}
}
// RateLimit summarizes current rate limits of resource families.
type RateLimit struct {
RateLimitContext *RateLimitContext `json:"rate_limit_context"`
Resources *RateLimitResources `json:"resources"`
}
// RateLimitContext contains auth context
type RateLimitContext struct {
AccessToken string `json:"access_token"`
}
// RateLimitResources contains all limit status data for endpoints group by resources
type RateLimitResources struct {
Application map[string]*RateLimitResource `json:"application"`
Favorites map[string]*RateLimitResource `json:"favorites"`
Followers map[string]*RateLimitResource `json:"followers"`
Friends map[string]*RateLimitResource `json:"friends"`
Friendships map[string]*RateLimitResource `json:"friendships"`
Geo map[string]*RateLimitResource `json:"geo"`
Help map[string]*RateLimitResource `json:"help"`
Lists map[string]*RateLimitResource `json:"lists"`
Search map[string]*RateLimitResource `json:"search"`
Statuses map[string]*RateLimitResource `json:"statuses"`
Trends map[string]*RateLimitResource `json:"trends"`
Users map[string]*RateLimitResource `json:"users"`
}
// RateLimitResource contains limit status data for a single endpoint
type RateLimitResource struct {
Limit int `json:"limit"`
Remaining int `json:"remaining"`
Reset int `json:"reset"`
}
// RateLimitParams are the parameters for RateLimitService.Status.
type RateLimitParams struct {
Resources []string `url:"resources,omitempty,comma"`
}
// Status summarizes the current rate limits of specified resource families.
// https://developer.twitter.com/en/docs/developer-utilities/rate-limit-status/api-reference/get-application-rate_limit_status
func (s *RateLimitService) Status(params *RateLimitParams) (*RateLimit, *http.Response, error) {
rateLimit := new(RateLimit)
apiError := new(APIError)
resp, err := s.sling.New().Get("rate_limit_status.json").QueryStruct(params).Receive(rateLimit, apiError)
return rateLimit, resp, relevantError(err, *apiError)
}