|
| 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 | +// RateLimitStatusResponse response of rate limit status |
| 23 | +type RateLimitStatusResponse struct { |
| 24 | + RateLimitContext *RateLimitContext `json:"rate_limit_context,omitempty"` |
| 25 | + Resources *RateLimitResources `json:"resources,omitempty"` |
| 26 | +} |
| 27 | + |
| 28 | +// RateLimitContext contains auth context |
| 29 | +type RateLimitContext struct { |
| 30 | + AccessToken string `json:"access_token,omitempty"` |
| 31 | +} |
| 32 | + |
| 33 | +// RateLimitResources contains all limit status data for endpoints group by resources |
| 34 | +type RateLimitResources struct { |
| 35 | + Users map[string]*RateLimitResource `json:"users,omitempty"` |
| 36 | + Statuses map[string]*RateLimitResource `json:"statuses,omitempty"` |
| 37 | + Help map[string]*RateLimitResource `json:"help,omitempty"` |
| 38 | + Search map[string]*RateLimitResource `json:"search,omitempty"` |
| 39 | +} |
| 40 | + |
| 41 | +// RateLimitResource contains limit status data for a single endpoint |
| 42 | +type RateLimitResource struct { |
| 43 | + Limit int `json:"limit,omitempty"` |
| 44 | + Remaining int `json:"remaining,omitempty"` |
| 45 | + Reset int `json:"reset,omitempty"` |
| 46 | +} |
| 47 | + |
| 48 | +// RateLimitStatusParams are the parameters for RateLimitService.Status. |
| 49 | +type RateLimitStatusParams struct { |
| 50 | + Resources string `url:"resources,omitempty"` |
| 51 | +} |
| 52 | + |
| 53 | +// Status returns rate limits. |
| 54 | +// https://developer.twitter.com/en/docs/developer-utilities/rate-limit-status/api-reference/get-application-rate_limit_status |
| 55 | +func (s *RateLimitService) Status(params *RateLimitStatusParams) (RateLimitStatusResponse, *http.Response, error) { |
| 56 | + rateLimitStatusResponse := new(RateLimitStatusResponse) |
| 57 | + apiError := new(APIError) |
| 58 | + resp, err := s.sling.New().Get("rate_limit_status.json").QueryStruct(params).Receive(rateLimitStatusResponse, apiError) |
| 59 | + return *rateLimitStatusResponse, resp, relevantError(err, *apiError) |
| 60 | +} |
0 commit comments