-
-
Notifications
You must be signed in to change notification settings - Fork 802
/
client.go
63 lines (51 loc) · 1.33 KB
/
client.go
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
package twitter
import (
"encoding/json"
"fmt"
"strconv"
)
/* NOTE: Currently single application ONLY
* bearer tokens are only supported for applications, not single-users
*/
// Client represents the data required to connect to the Twitter API
type Client struct {
apiBase string
bearerToken string
count int
screenName string
}
// NewClient creates and returns a new Twitter client
func NewClient(settings *Settings) *Client {
client := Client{
apiBase: "https://api.twitter.com/1.1/",
count: settings.count,
screenName: "",
bearerToken: settings.bearerToken,
}
return &client
}
/* -------------------- Public Functions -------------------- */
// Tweets returns a list of tweets of a user
func (client *Client) Tweets() []Tweet {
tweets, err := client.tweets()
if err != nil {
return []Tweet{}
}
return tweets
}
/* -------------------- Private Functions -------------------- */
// tweets is the private interface for retrieving the list of user tweets
func (client *Client) tweets() (tweets []Tweet, err error) {
apiURL := fmt.Sprintf(
"%s/statuses/user_timeline.json?screen_name=%s&count=%s",
client.apiBase,
client.screenName,
strconv.Itoa(client.count),
)
data, err := Request(client.bearerToken, apiURL)
if err != nil {
return tweets, err
}
err = json.Unmarshal(data, &tweets)
return
}