This repository was archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 310
Add support for rate_limit_status endpoint #124
Merged
dghubble
merged 1 commit into
dghubble:master
from
ltoussaint:feature/rate_limit_status
Nov 30, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package twitter | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestRateLimitService_Status(t *testing.T) { | ||
| httpClient, mux, server := testServer() | ||
| defer server.Close() | ||
|
|
||
| mux.HandleFunc("/1.1/application/rate_limit_status.json", func(w http.ResponseWriter, r *http.Request) { | ||
| assertMethod(t, "GET", r) | ||
| assertQuery(t, map[string]string{"resources": "statuses,users"}, r) | ||
| w.Header().Set("Content-Type", "application/json") | ||
| fmt.Fprintf(w, `{"rate_limit_context":{"access_token":"a_fake_access_token"},"resources":{"statuses":{"/statuses/mentions_timeline":{"limit":75,"remaining":75,"reset":1403602426},"/statuses/lookup":{"limit":900,"remaining":900,"reset":1403602426}}}}`) | ||
| }) | ||
|
|
||
| client := NewClient(httpClient) | ||
| rateLimits, _, err := client.RateLimits.Status(&RateLimitParams{Resources: []string{"statuses", "users"}}) | ||
| expected := &RateLimit{ | ||
| RateLimitContext: &RateLimitContext{AccessToken: "a_fake_access_token"}, | ||
| Resources: &RateLimitResources{ | ||
| Statuses: map[string]*RateLimitResource{ | ||
| "/statuses/mentions_timeline": &RateLimitResource{ | ||
| Limit: 75, | ||
| Remaining: 75, | ||
| Reset: 1403602426}, | ||
| "/statuses/lookup": &RateLimitResource{ | ||
| Limit: 900, | ||
| Remaining: 900, | ||
| Reset: 1403602426}}}} | ||
|
|
||
| assert.Nil(t, err) | ||
| assert.Equal(t, expected, rateLimits) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add to README please