-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature/issue 102 rate limit handling (#104)
* added rate limits * added rate limit to callouts * add rate limiting to hide replies * rate limit error helper * added response decode error * updated the error message and name * updated unit tests * add unit test for rate limit helper * added the rate limit example * updated the readme * updated readme * update for release migration * fix per golint
- Loading branch information
1 parent
3f09833
commit 7783042
Showing
41 changed files
with
1,700 additions
and
206 deletions.
There are no files selected for viewing
This file contains 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 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 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,63 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"time" | ||
|
||
twitter "github.com/g8rswimmer/go-twitter/v2" | ||
) | ||
|
||
type authorize struct { | ||
Token string | ||
} | ||
|
||
func (a authorize) Add(req *http.Request) { | ||
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", a.Token)) | ||
} | ||
|
||
/** | ||
In order to run, the user will need to provide the bearer token and the list of tweet ids. | ||
**/ | ||
func main() { | ||
token := flag.String("token", "", "twitter API token") | ||
flag.Parse() | ||
|
||
client := &twitter.Client{ | ||
Authorizer: authorize{ | ||
Token: *token, | ||
}, | ||
Client: http.DefaultClient, | ||
Host: "https://api.twitter.com", | ||
} | ||
|
||
fmt.Println("Rate Limiting Example\n\n") | ||
|
||
var rateLimit *twitter.RateLimit | ||
|
||
rateLimit = callout(client) | ||
fmt.Printf("Rate Limits: %v\n", *rateLimit) | ||
rateLimit = callout(client) | ||
fmt.Printf("Rate Limits: %v\n\n", *rateLimit) | ||
|
||
fmt.Printf("Wait for reset %v\n", rateLimit.Reset.Time()) | ||
time.Sleep(time.Until(rateLimit.Reset.Time())) | ||
fmt.Println("Rate Limits are reset") | ||
|
||
rateLimit = callout(client) | ||
fmt.Printf("Rate Limits: %v\n", *rateLimit) | ||
} | ||
|
||
func callout(client *twitter.Client) *twitter.RateLimit { | ||
opts := twitter.ListUserMembersOpts{ | ||
MaxResults: 1, | ||
} | ||
listResponse, err := client.ListUserMembers(context.Background(), "84839422", opts) | ||
if err != nil { | ||
log.Panicf("list members error: %v", err) | ||
} | ||
return listResponse.RateLimit | ||
} |
This file contains 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
Oops, something went wrong.