|
| 1 | +package twitter |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + |
| 6 | + "github.com/dghubble/sling" |
| 7 | +) |
| 8 | + |
| 9 | +// RateLimitService provides methods for accessing Twitter rate limits |
| 10 | +// API endpoints. |
| 11 | +type RateLimitService struct { |
| 12 | + sling *sling.Sling |
| 13 | +} |
| 14 | + |
| 15 | +// newRateLimitService returns a new RateLimitService. |
| 16 | +func newRateLimitService(sling *sling.Sling) *RateLimitService { |
| 17 | + return &RateLimitService{ |
| 18 | + sling: sling.Path("application/"), |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// RateLimit summarizes current rate limits of resource families. |
| 23 | +type RateLimit struct { |
| 24 | + RateLimitContext *RateLimitContext `json:"rate_limit_context"` |
| 25 | + Resources *RateLimitResources `json:"resources"` |
| 26 | +} |
| 27 | + |
| 28 | +// RateLimitContext contains auth context |
| 29 | +type RateLimitContext struct { |
| 30 | + AccessToken string `json:"access_token"` |
| 31 | +} |
| 32 | + |
| 33 | +// RateLimitResources contains all limit status data for endpoints group by resources |
| 34 | +type RateLimitResources struct { |
| 35 | + Application map[string]*RateLimitResource `json:"application"` |
| 36 | + Favorites map[string]*RateLimitResource `json:"favorites"` |
| 37 | + Followers map[string]*RateLimitResource `json:"followers"` |
| 38 | + Friends map[string]*RateLimitResource `json:"friends"` |
| 39 | + Friendships map[string]*RateLimitResource `json:"friendships"` |
| 40 | + Geo map[string]*RateLimitResource `json:"geo"` |
| 41 | + Help map[string]*RateLimitResource `json:"help"` |
| 42 | + Lists map[string]*RateLimitResource `json:"lists"` |
| 43 | + Search map[string]*RateLimitResource `json:"search"` |
| 44 | + Statuses map[string]*RateLimitResource `json:"statuses"` |
| 45 | + Trends map[string]*RateLimitResource `json:"trends"` |
| 46 | + Users map[string]*RateLimitResource `json:"users"` |
| 47 | +} |
| 48 | + |
| 49 | +// RateLimitResource contains limit status data for a single endpoint |
| 50 | +type RateLimitResource struct { |
| 51 | + Limit int `json:"limit"` |
| 52 | + Remaining int `json:"remaining"` |
| 53 | + Reset int `json:"reset"` |
| 54 | +} |
| 55 | + |
| 56 | +// RateLimitParams are the parameters for RateLimitService.Status. |
| 57 | +type RateLimitParams struct { |
| 58 | + Resources []string `url:"resources,omitempty,comma"` |
| 59 | +} |
| 60 | + |
| 61 | +// Status summarizes the current rate limits of specified resource families. |
| 62 | +// https://developer.twitter.com/en/docs/developer-utilities/rate-limit-status/api-reference/get-application-rate_limit_status |
| 63 | +func (s *RateLimitService) Status(params *RateLimitParams) (*RateLimit, *http.Response, error) { |
| 64 | + rateLimit := new(RateLimit) |
| 65 | + apiError := new(APIError) |
| 66 | + resp, err := s.sling.New().Get("rate_limit_status.json").QueryStruct(params).Receive(rateLimit, apiError) |
| 67 | + return rateLimit, resp, relevantError(err, *apiError) |
| 68 | +} |
0 commit comments