-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusers.go
95 lines (84 loc) · 2.59 KB
/
users.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
package twitter
import (
"context"
"net/url"
"strconv"
"strings"
)
// SearchUsersParams represents parameters to send to /users/search.json Twitter endpoint
type SearchUsersParams struct {
Q string
Page int
Count int
ExcludeEntities bool
}
// SearchUsers calls Twitter endpoint /users/search.json
func (c *Client) SearchUsers(ctx context.Context, params SearchUsersParams) (*UsersResponse, error) {
values := searchUsersToQuery(params)
urlStr := "https://api.twitter.com/1.1/users/search.json"
return c.handleUsersResponse(ctx, "GET", urlStr, values)
}
func searchUsersToQuery(params SearchUsersParams) url.Values {
values := url.Values{}
values.Set("q", params.Q)
if params.Page != 0 {
values.Set("page", strconv.Itoa(params.Page))
}
if params.Count != 0 {
values.Set("count", strconv.Itoa(params.Count))
}
if params.ExcludeEntities {
values.Set("include_entities", "false")
}
return values
}
// ShowUserParams reperesents parameters to send to /users/show.json Twitter endpoint
type ShowUserParams struct {
UserID string
ScreenName string
ExcludeEntities bool
}
// ShowUser calls Twitter endpoint /users/show.json
func (c *Client) ShowUser(ctx context.Context, params ShowUserParams) (*UserResponse, error) {
values := showUserToQuery(params)
urlStr := "https://api.twitter.com/1.1/users/show.json"
return c.handleUserResponse(ctx, "GET", urlStr, values)
}
func showUserToQuery(params ShowUserParams) url.Values {
values := url.Values{}
if params.UserID != "" {
values.Set("user_id", params.UserID)
}
if params.ScreenName != "" {
values.Set("screen_name", params.ScreenName)
}
if params.ExcludeEntities {
values.Set("include_entities", "false")
}
return values
}
// LookupUsersParams represents parameters to send to /users/lookup.json Twitter endpoint
type LookupUsersParams struct {
ScreenName []string
UserID []string
ExcludeEntities bool
}
// LookupUsers calls Twitter endpoint /users/lookup.json
func (c *Client) LookupUsers(ctx context.Context, params LookupUsersParams) (*UsersResponse, error) {
values := lookupUsersToQuery(params)
urlStr := "https://api.twitter.com/1.1/users/lookup.json"
return c.handleUsersResponse(ctx, "POST", urlStr, values)
}
func lookupUsersToQuery(params LookupUsersParams) url.Values {
values := url.Values{}
if len(params.ScreenName) > 0 {
values.Set("screen_name", strings.Join(params.ScreenName, ","))
}
if len(params.UserID) > 0 {
values.Set("user_id", strings.Join(params.UserID, ","))
}
if params.ExcludeEntities {
values.Set("include_entities", "false")
}
return values
}