-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathclient.go
126 lines (102 loc) · 2.97 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
package twitter
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/metrics"
)
const (
baseUrl = "https://api.twitter.com/2/"
getUserByIdUrl = baseUrl + "users/"
getUserByUsernameUrl = baseUrl + "users/by/username/"
metricsStructName = "twitter.client"
)
type Client struct {
client *http.Client
apiToken string
}
// NewClient returns a new Twitter client
func NewClient(apiToken string) *Client {
return &Client{
client: http.DefaultClient,
apiToken: apiToken,
}
}
// 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"`
ProfileImageUrl string `json:"profile_image_url"`
PublicMetrics PublicMetrics `json:"public_metrics"`
}
// PublicMetrics are public metrics for a Twitter user
type PublicMetrics struct {
FollowersCount int `json:"followers_count"`
FollowingCount int `json:"following_count"`
TweetCount int `json:"tweet_count"`
LikeCount int `json:"like_count"`
}
// 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, getUserByIdUrl+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, getUserByUsernameUrl+username)
if err != nil {
tracer.OnError(err)
}
return user, err
}
func (c *Client) getUser(ctx context.Context, fromUrl string) (*User, error) {
req, err := http.NewRequest("GET", fromUrl+"?user.fields=profile_image_url,public_metrics", nil)
if err != nil {
return nil, err
}
req = req.WithContext(ctx)
req.Header.Add("Authorization", "Bearer "+c.apiToken)
resp, err := c.client.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
}
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)
}