example_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package anaconda_test
  2. import (
  3. "fmt"
  4. "time"
  5. "github.com/ChimeraCoder/anaconda"
  6. )
  7. // Initialize an client library for a given user.
  8. // This only needs to be done *once* per user
  9. func ExampleTwitterApi_InitializeClient() {
  10. api := anaconda.NewTwitterApiWithCredentials(ACCESS_TOKEN, ACCESS_TOKEN_SECRET, "your-consumer-key", "your-consumer-secret")
  11. fmt.Println(*api.Credentials)
  12. }
  13. func ExampleTwitterApi_GetSearch() {
  14. anaconda.SetConsumerKey("your-consumer-key")
  15. anaconda.SetConsumerSecret("your-consumer-secret")
  16. api := anaconda.NewTwitterApi("your-access-token", "your-access-token-secret")
  17. search_result, err := api.GetSearch("golang", nil)
  18. if err != nil {
  19. panic(err)
  20. }
  21. for _, tweet := range search_result.Statuses {
  22. fmt.Print(tweet.Text)
  23. }
  24. }
  25. // Throttling queries can easily be handled in the background, automatically
  26. func ExampleTwitterApi_Throttling() {
  27. api := anaconda.NewTwitterApi("your-access-token", "your-access-token-secret")
  28. api.EnableThrottling(10*time.Second, 5)
  29. // These queries will execute in order
  30. // with appropriate delays inserted only if necessary
  31. golangTweets, err := api.GetSearch("golang", nil)
  32. anacondaTweets, err2 := api.GetSearch("anaconda", nil)
  33. if err != nil {
  34. panic(err)
  35. }
  36. if err2 != nil {
  37. panic(err)
  38. }
  39. fmt.Println(golangTweets)
  40. fmt.Println(anacondaTweets)
  41. }
  42. // Fetch a list of all followers without any need for managing cursors
  43. // (Each page is automatically fetched when the previous one is read)
  44. func ExampleTwitterApi_GetFollowersListAll() {
  45. pages := api.GetFollowersListAll(nil)
  46. for page := range pages {
  47. //Print the current page of followers
  48. fmt.Println(page.Followers)
  49. }
  50. }