forked from mattn/go-redmine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.go
76 lines (68 loc) · 1.62 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
package redmine
import (
"encoding/json"
"errors"
"strconv"
"strings"
)
type userResult struct {
User User `json:"user"`
}
type usersResult struct {
Users []User `json:"users"`
}
type User struct {
Id int `json:"id"`
Login string `json:"login"`
Firstname string `json:"firstname"`
Lastname string `json:"lastname"`
Mail string `json:"mail"`
CreatedOn string `json:"created_on"`
LatLoginOn string `json:"last_login_on"`
Memberships []Membership `json:"memberships"`
CustomFields []*CustomField `json:"custom_fields,omitempty"`
}
func (c *Client) Users() ([]User, error) {
res, err := c.Get(c.endpoint + "/users.json?key=" + c.apikey + c.getPaginationClause())
if err != nil {
return nil, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var r usersResult
if res.StatusCode != 200 {
var er errorsResult
err = decoder.Decode(&er)
if err == nil {
err = errors.New(strings.Join(er.Errors, "\n"))
}
} else {
err = decoder.Decode(&r)
}
if err != nil {
return nil, err
}
return r.Users, nil
}
func (c *Client) User(id int) (*User, error) {
res, err := c.Get(c.endpoint + "/users/" + strconv.Itoa(id) + ".json?key=" + c.apikey)
if err != nil {
return nil, err
}
defer res.Body.Close()
decoder := json.NewDecoder(res.Body)
var r userResult
if res.StatusCode != 200 {
var er errorsResult
err = decoder.Decode(&er)
if err == nil {
err = errors.New(strings.Join(er.Errors, "\n"))
}
} else {
err = decoder.Decode(&r)
}
if err != nil {
return nil, err
}
return &r.User, nil
}