forked from sivchari/gotwtr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_lookup.go
200 lines (169 loc) · 5.58 KB
/
user_lookup.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
package gotwtr
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)
func retrieveMultipleUsersWithIDs(ctx context.Context, c *client, userIDs []string, opt ...*RetrieveUserOption) (*UsersResponse, error) {
switch {
case len(userIDs) == 0:
return nil, errors.New("retrieve multiple users with ids: ids parameter is required")
case len(userIDs) > userLookUpMaxIDs:
return nil, errors.New("retrieve multiple users with ids: ids parameter must be less than or equal to 100")
default:
}
ep := retrieveMultipleUsersWithIDsURL + strings.Join(userIDs, ",")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
if err != nil {
return nil, fmt.Errorf("retrieve multiple users with ids new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var ropt RetrieveUserOption
switch len(opt) {
case 0:
// do nothing
case 1:
ropt = *opt[0]
default:
return nil, errors.New("retrieve multiple users with ids: only one option is allowed")
}
ropt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("retrieve multiple users with ids response: %w", err)
}
defer resp.Body.Close()
var ur UsersResponse
if err := json.NewDecoder(resp.Body).Decode(&ur); err != nil {
return nil, fmt.Errorf("retrieve multiple users with ids decode: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &ur, &HTTPError{
APIName: "user lookup",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &ur, nil
}
func retrieveSingleUserWithID(ctx context.Context, c *client, userID string, opt ...*RetrieveUserOption) (*UserResponse, error) {
if userID == "" {
return nil, errors.New("retrieve single user with id: user id is required")
}
ep := fmt.Sprintf(retrieveSingleUserWithIDURL, userID)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
if err != nil {
return nil, fmt.Errorf("retrieve single user with id new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var ropt RetrieveUserOption
switch len(opt) {
case 0:
// do nothing
case 1:
ropt = *opt[0]
default:
return nil, errors.New("retrieve single user with id: only one option is allowed")
}
ropt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("retrieve single user with id response: %w", err)
}
defer resp.Body.Close()
var ur UserResponse
if err := json.NewDecoder(resp.Body).Decode(&ur); err != nil {
return nil, fmt.Errorf("retrieve single user with id decode: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &ur, &HTTPError{
APIName: "retrieve single user with id",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &ur, nil
}
func retrieveMultipleUsersWithUserNames(ctx context.Context, c *client, userNames []string, opt ...*RetrieveUserOption) (*UsersResponse, error) {
switch {
case len(userNames) == 0:
return nil, errors.New("retrieve multiple users with user names: user names parameter is required")
case len(userNames) > userLookUpMaxIDs:
return nil, errors.New("retrieve multiple users with user names: user names parameter must be less than or equal to 100")
default:
}
ep := retrieveMultipleUsersWithUserNamesURL + strings.Join(userNames, ",")
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
if err != nil {
return nil, fmt.Errorf("retrieve multiple users with user names new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var ropt RetrieveUserOption
switch len(opt) {
case 0:
// do nothing
case 1:
ropt = *opt[0]
default:
return nil, errors.New("retrieve multiple users with user names: only one option is allowed")
}
ropt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("retrieve multiple users with user names response: %w", err)
}
defer resp.Body.Close()
var ur UsersResponse
if err := json.NewDecoder(resp.Body).Decode(&ur); err != nil {
return nil, fmt.Errorf("retrieve multiple users with user names decode: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &ur, &HTTPError{
APIName: "users lookup by usernames",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &ur, nil
}
func retrieveSingleUserWithUserName(ctx context.Context, c *client, userName string, opt ...*RetrieveUserOption) (*UserResponse, error) {
if userName == "" {
return nil, errors.New("retrieve single user with user name: user name is required")
}
ep := fmt.Sprintf(retrieveSingleUserWithUserNameURL, userName)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, ep, nil)
if err != nil {
return nil, fmt.Errorf("retrieve single user with user name new request with ctx: %w", err)
}
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", c.bearerToken))
var ropt RetrieveUserOption
switch len(opt) {
case 0:
// do nothing
case 1:
ropt = *opt[0]
default:
return nil, errors.New("retrieve single user with user name: only one option is allowed")
}
ropt.addQuery(req)
resp, err := c.client.Do(req)
if err != nil {
return nil, fmt.Errorf("retrieve single user with user name response: %w", err)
}
defer resp.Body.Close()
var ur UserResponse
if err := json.NewDecoder(resp.Body).Decode(&ur); err != nil {
return nil, fmt.Errorf("retrieve single user with user name decode: %w", err)
}
if resp.StatusCode != http.StatusOK {
return &ur, &HTTPError{
APIName: "retrieve single user with user name",
Status: resp.Status,
URL: req.URL.String(),
}
}
return &ur, nil
}