-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
78 lines (64 loc) · 2.04 KB
/
user.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
package docbase
import (
"net/http"
"net/url"
"strconv"
"time"
)
// UserService implements interface with API /posts endpoint.
// See https://help.docbase.io/posts/45703#%E3%83%81%E3%83%BC%E3%83%A0
type UserService interface {
List(opts *UserListOptions) (*UserListResponse, *Response, error)
}
// userService handles communication with API
type userService struct {
client *Client
}
// User represents a docbase User
type User struct {
ID int `json:"id"`
Name string `json:"name"`
Username string `json:"username"`
ProfileImageURL string `json:"profile_image_url"`
Role string `json:"role"`
PostsCount int `json:"posts_count"`
LastAccessTime time.Time `json:"last_access_time"`
TwoStepAuthentication bool `json:"two_step_authentication"`
Groups []SimpleGroup `json:"groups"`
}
type SimpleUser struct {
ID int `json:"id"`
Name string `json:"name"`
ProfileImageURL string `json:"profile_image_url"`
}
type UserListResponse []User
// UserListOptions identifies as query params of User List request
type UserListOptions struct {
Q string `url:"q,omitempty"`
Page int `url:"page,omitempty"`
PerPage int `url:"per_page,omitempty"`
IncludeUserGroups bool `url:"include_user_groups,omitempty"`
}
// List User
func (s *userService) List(opts *UserListOptions) (*UserListResponse, *Response, error) {
u, err := url.Parse("/users")
if err != nil {
return nil, nil, err
}
q := u.Query()
q.Set("per_page", strconv.Itoa(opts.PerPage))
q.Set("page", strconv.Itoa(opts.Page))
q.Set("q", opts.Q)
q.Set("include_user_groups", opts.Q)
u.RawQuery = q.Encode()
req, err := s.client.NewRequest(http.MethodGet, u.String(), nil)
if err != nil {
return nil, nil, err
}
userResp := &UserListResponse{}
resp, err := s.client.Do(req, userResp)
if err != nil {
return nil, resp, err
}
return userResp, resp, err
}