-
Notifications
You must be signed in to change notification settings - Fork 247
/
README
140 lines (88 loc) · 5.32 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
Anaconda
========
[![Build Status](https://travis-ci.org/ChimeraCoder/anaconda.svg?branch=master)](https://travis-ci.org/ChimeraCoder/anaconda) [![Build Status](https://ci.appveyor.com/api/projects/status/63pi6csod8bps80i/branch/master?svg=true)](https://ci.appveyor.com/project/ChimeraCoder/anaconda/branch/master) [![GoDoc](https://godoc.org/github.com/ChimeraCoder/anaconda?status.svg)](https://godoc.org/github.com/ChimeraCoder/anaconda)
Anaconda is a simple, transparent Go package for accessing version 1.1 of the Twitter API.
Successful API queries return native Go structs that can be used immediately, with no need for type assertions.
Examples
--------
### Authentication
If you already have the access token (and secret) for your user (Twitter provides this for your own account on the developer portal), creating the client is simple:
```go
api := anaconda.NewTwitterApiWithCredentials("your-access-token", "your-access-token-secret", "your-consumer-key", "your-consumer-secret")
```
### Queries
Queries are conducted using a pointer to an authenticated `TwitterApi` struct. In v1.1 of Twitter's API, all requests should be authenticated.
```go
searchResult, _ := api.GetSearch("golang", nil)
for _ , tweet := range searchResult.Statuses {
fmt.Println(tweet.Text)
}
```
Certain endpoints allow separate optional parameter; if desired, these can be passed as the final parameter.
```go
//Perhaps we want 30 values instead of the default 15
v := url.Values{}
v.Set("count", "30")
result, err := api.GetSearch("golang", v)
```
(Remember that `url.Values` is equivalent to a `map[string][]string`, if you find that more convenient notation when specifying values). Otherwise, `nil` suffices.
### Streaming
Anaconda supports the Streaming APIs. You can use `PublicStream*` or `UserStream` API methods.
A go loop is started an gives you an stream that sends `interface{}` objects through it's `chan` `C`
Objects which you can cast into a tweet, event and more.
````go
v := url.Values{}
s := api.UserStream(v)
for t := range s.C {
switch v := t.(type) {
case anaconda.Tweet:
fmt.Printf("%-15s: %s\n", v.User.ScreenName, v.Text)
case anaconda.EventTweet:
switch v.Event.Event {
case "favorite":
sn := v.Source.ScreenName
tw := v.TargetObject.Text
fmt.Printf("Favorited by %-15s: %s\n", sn, tw)
case "unfavorite":
sn := v.Source.ScreenName
tw := v.TargetObject.Text
fmt.Printf("UnFavorited by %-15s: %s\n", sn, tw)
}
}
}
````
Endpoints
---------
Anaconda implements most of the endpoints defined in the [Twitter API documentation](https://developer.twitter.com/en/docs). For clarity, in most cases, the function name is simply the name of the HTTP method and the endpoint (e.g., the endpoint `GET /friendships/incoming` is provided by the function `GetFriendshipsIncoming`).
In a few cases, a shortened form has been chosen to make life easier (for example, retweeting is simply the function `Retweet`)
Error Handling, Rate Limiting, and Throttling
---------------------------------------------
### Error Handling
Twitter errors are returned as an `ApiError`, which satisfies the `error` interface and can be treated as a vanilla `error`. However, it also contains the additional information returned by the Twitter API that may be useful in deciding how to proceed after encountering an error.
If you make queries too quickly, you may bump against Twitter's [rate limits](https://developer.twitter.com/en/docs/basics/rate-limits). If this happens, `anaconda` automatically retries the query when the rate limit resets, using the `X-Rate-Limit-Reset` header that Twitter provides to determine how long to wait.
In other words, users of the `anaconda` library should not need to handle rate limiting errors themselves; this is handled seamlessly behind-the-scenes. If an error is returned by a function, another form of error must have occurred (which can be checked by using the fields provided by the `ApiError` struct).
(If desired, this feature can be turned off by calling `ReturnRateLimitError(true)`.)
### Throttling
Anaconda now supports automatic client-side throttling of queries to avoid hitting the Twitter rate-limit.
This is currently *off* by default; however, it may be turned on by default in future versions of the library, as the implementation is improved.
To set a delay between queries, use the `SetDelay` method:
```go
api.SetDelay(10 * time.Second)
```
Delays are set specific to each `TwitterApi` struct, so queries that use different users' access credentials are completely independent.
To turn off automatic throttling, set the delay to `0`:
```go
api.SetDelay(0 * time.Second)
```
### Query Queue Persistence
If your code creates a NewTwitterApi in a regularly called function, you'll need to call `.Close()` on the API struct to clear the queryQueue and allow the goroutine to exit. Otherwise you could see goroutine and therefor heap memory leaks in long-running applications.
### Google App Engine
Since Google App Engine doesn't make the standard `http.Transport` available, it's necessary to tell Anaconda to use a different client context.
```go
api = anaconda.NewTwitterApi("", "")
c := appengine.NewContext(r)
api.HttpClient.Transport = &urlfetch.Transport{Context: c}
```
License
-------
Anaconda is free software licensed under the MIT/X11 license. Details provided in the LICENSE file.