-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
322 lines (259 loc) · 7.8 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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package twitter
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/metrics"
)
const (
baseUrl = "https://api.twitter.com/2/"
bearerTokenMaxAge = 15 * time.Minute
metricsStructName = "twitter.client"
)
type Client struct {
httpClient *http.Client
clientId string
clientSecret string
bearerTokenMu sync.RWMutex
bearerToken string
lastBearerTokenRefresh time.Time
}
// NewClient returns a new Twitter client
func NewClient(clientId, clientSecret string) *Client {
return &Client{
httpClient: http.DefaultClient,
clientId: clientId,
clientSecret: clientSecret,
}
}
// User represents the structure for a user in the Twitter API response
type User struct {
ID string `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
VerifiedType string `json:"verified_type"`
ProfileImageUrl string `json:"profile_image_url"`
PublicMetrics PublicMetrics `json:"public_metrics"`
}
// PublicMetrics represents the structure for public metrics in the Twitter API response
type PublicMetrics struct {
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
TweetCount int `json:"tweet_count"`
LikeCount int `json:"like_count"`
}
// Tweet represents the structure for a tweet in the Twitter API response
type Tweet struct {
ID string `json:"id"`
Text string `json:"text"`
AuthorID *string `json:"author_id"`
AdditionalMetadata AdditionalTweetMetadata
}
// AdditionalTweetMetadata adds additinal metadata to a tweet that isn't directly
// represented in the Twitter API response
type AdditionalTweetMetadata struct {
Author *User
}
// GetUserById makes a request to the Twitter API and returns the user's information
// by ID
func (c *Client) GetUserById(ctx context.Context, id string) (*User, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "GetUserById")
defer tracer.End()
user, err := c.getUser(ctx, baseUrl+"users/"+id)
if err != nil {
tracer.OnError(err)
}
return user, err
}
// GetUserByUsername makes a request to the Twitter API and returns the user's information
// by username
func (c *Client) GetUserByUsername(ctx context.Context, username string) (*User, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "GetUserByUsername")
defer tracer.End()
user, err := c.getUser(ctx, baseUrl+"users/by/username/"+username)
if err != nil {
tracer.OnError(err)
}
return user, err
}
// GetUserTweets gets tweets for a given user
func (c *Client) GetUserTweets(ctx context.Context, userId string, maxResults int, nextToken *string) ([]*Tweet, *string, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "GetUserTweets")
defer tracer.End()
url := fmt.Sprintf(baseUrl+"users/"+userId+"/tweets?max_results=%d", maxResults)
if nextToken != nil {
url = fmt.Sprintf("%s&next_token=%s", url, *nextToken)
}
tweets, nextToken, err := c.getTweets(ctx, url)
if err != nil {
tracer.OnError(err)
}
return tweets, nextToken, err
}
// SearchRecentTweets searches for recent tweets within the last 7 days matching
// a search string.
func (c *Client) SearchRecentTweets(ctx context.Context, searchString string, maxResults int, nextToken *string) ([]*Tweet, *string, error) {
tracer := metrics.TraceMethodCall(ctx, metricsStructName, "SearchUserTweets")
defer tracer.End()
url := fmt.Sprintf(
baseUrl+"tweets/search/recent?query=%s&expansions=author_id&user.fields=username,profile_image_url,public_metrics,verified_type&max_results=%d",
url.QueryEscape(searchString),
maxResults,
)
if nextToken != nil {
url = fmt.Sprintf("%s&next_token=%s", url, *nextToken)
}
tweets, nextToken, err := c.getTweets(ctx, url)
if err != nil {
tracer.OnError(err)
}
return tweets, nextToken, err
}
func (c *Client) getUser(ctx context.Context, fromUrl string) (*User, error) {
bearerToken, err := c.getBearerToken(c.clientId, c.clientSecret)
if err != nil {
return nil, err
}
req, err := http.NewRequest("GET", fromUrl+"?user.fields=profile_image_url,public_metrics,verified_type", nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Add("Authorization", "Bearer "+bearerToken)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected http status code: %d", resp.StatusCode)
}
var result struct {
Data *User `json:"data"`
Errors []*twitterError `json:"errors"`
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, err
}
if len(result.Errors) > 0 {
return nil, result.Errors[0].toError()
}
return result.Data, nil
}
func (c *Client) getTweets(ctx context.Context, fromUrl string) ([]*Tweet, *string, error) {
bearerToken, err := c.getBearerToken(c.clientId, c.clientSecret)
if err != nil {
return nil, nil, err
}
req, err := http.NewRequest("GET", fromUrl, nil)
if err != nil {
return nil, nil, err
}
req = req.WithContext(ctx)
req.Header.Add("Authorization", "Bearer "+bearerToken)
resp, err := c.httpClient.Do(req)
if err != nil {
return nil, nil, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return nil, nil, fmt.Errorf("unexpected http status code: %d", resp.StatusCode)
}
var result struct {
Data []*Tweet `json:"data"`
Errors []*twitterError `json:"errors"`
Meta struct {
NextToken *string `json:"next_token"`
} `json:"meta"`
Includes struct {
Users []User `json:"users"`
} `json:"includes"`
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, nil, err
}
if err := json.Unmarshal(body, &result); err != nil {
return nil, nil, err
}
if len(result.Errors) > 0 {
return nil, nil, result.Errors[0].toError()
}
for _, tweet := range result.Data {
if tweet.AuthorID == nil {
continue
}
for _, user := range result.Includes.Users {
if user.ID == *tweet.AuthorID {
tweet.AdditionalMetadata.Author = &user
break
}
}
}
return result.Data, result.Meta.NextToken, nil
}
func (c *Client) getBearerToken(clientId, clientSecret string) (string, error) {
c.bearerTokenMu.RLock()
if time.Since(c.lastBearerTokenRefresh) < bearerTokenMaxAge {
c.bearerTokenMu.RUnlock()
return c.bearerToken, nil
}
c.bearerTokenMu.RUnlock()
c.bearerTokenMu.Lock()
defer c.bearerTokenMu.Unlock()
if time.Since(c.lastBearerTokenRefresh) < bearerTokenMaxAge {
return c.bearerToken, nil
}
requestData := []byte("grant_type=client_credentials")
req, err := http.NewRequest("POST", "https://api.twitter.com/oauth2/token", bytes.NewBuffer(requestData))
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.SetBasicAuth(clientId, clientSecret)
resp, err := c.httpClient.Do(req)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
var result struct {
TokenType string `json:"token_type"`
AccessToken string `json:"access_token"`
}
if err := json.Unmarshal(body, &result); err != nil {
return "", err
}
if len(result.AccessToken) == 0 {
return "", fmt.Errorf("could not get access token")
}
c.bearerToken = result.AccessToken
c.lastBearerTokenRefresh = time.Now()
return result.AccessToken, nil
}
// IsRetweet returns whether the tweet is a retweet
func (t *Tweet) IsRetweet() bool {
return strings.HasPrefix(t.Text, "RT @")
}
type twitterError struct {
Title string `json:"title"`
Detail string `json:"detail"`
}
func (e *twitterError) toError() error {
return errors.Errorf("%s: %s", e.Title, e.Detail)
}