-
Notifications
You must be signed in to change notification settings - Fork 7
/
user.go
139 lines (106 loc) · 3.01 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
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
package zammad
import (
"fmt"
"net/http"
"net/url"
"time"
)
// User is a Zammad user. See https://docs.zammad.org/en/latest/api/user.html.
type User struct {
ID int `json:"id"`
OrganizationID int `json:"organization_id"`
Login string `json:"login"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Email string `json:"email"`
Web string `json:"web"`
LastLogin time.Time `json:"last_login"`
}
// UserMe returns the current authenticated user.
func (c *Client) UserMe() (User, error) {
var user User
req, err := c.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", c.Url, "/api/v1/users/me"), nil)
if err != nil {
return user, err
}
if err = c.sendWithAuth(req, &user); err != nil {
return user, err
}
return user, nil
}
func (c *Client) UserListResult(opts ...Option) *Result[User] {
return &Result[User]{
res: nil,
resFunc: c.UserListWithOptions,
opts: NewRequestOptions(opts...),
}
}
func (c *Client) UserList() ([]User, error) {
return c.UserListResult().FetchAll()
}
func (c *Client) UserListWithOptions(ro RequestOptions) ([]User, error) {
var users []User
req, err := c.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", c.Url, "/api/v1/users"), nil)
if err != nil {
return nil, err
}
req.URL.RawQuery = ro.URLParams()
if err = c.sendWithAuth(req, &users); err != nil {
return nil, err
}
return users, nil
}
func (c *Client) UserSearch(query string, limit int) ([]User, error) {
var users []User
req, err := c.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", c.Url, fmt.Sprintf("/api/v1/users/search?query=%s&limit=%d", url.QueryEscape(query), limit)), nil)
if err != nil {
return nil, err
}
if err = c.sendWithAuth(req, &users); err != nil {
return nil, err
}
return users, nil
}
func (c *Client) UserShow(userID int) (User, error) {
var user User
req, err := c.NewRequest(http.MethodGet, fmt.Sprintf("%s%s", c.Url, fmt.Sprintf("/api/v1/users/%d", userID)), nil)
if err != nil {
return user, err
}
if err = c.sendWithAuth(req, &user); err != nil {
return user, err
}
return user, nil
}
func (c *Client) UserCreate(u User) (User, error) {
var user User
req, err := c.NewRequest(http.MethodPost, fmt.Sprintf("%s%s", c.Url, "/api/v1/users"), u)
if err != nil {
return user, err
}
if err = c.sendWithAuth(req, &user); err != nil {
return user, err
}
return user, nil
}
func (c *Client) UserUpdate(userID int, u User) (User, error) {
var user User
req, err := c.NewRequest(http.MethodPut, fmt.Sprintf("%s%s", c.Url, fmt.Sprintf("/api/v1/users/%d", userID)), u)
if err != nil {
return user, err
}
if err = c.sendWithAuth(req, &user); err != nil {
return user, err
}
return user, nil
}
func (c *Client) UserDelete(userID int) error {
req, err := c.NewRequest(http.MethodDelete, fmt.Sprintf("%s%s", c.Url, fmt.Sprintf("/api/v1/users/%d", userID)), nil)
if err != nil {
return err
}
if err = c.sendWithAuth(req, nil); err != nil {
return err
}
return nil
}