-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathuser.go
More file actions
121 lines (105 loc) · 4.95 KB
/
user.go
File metadata and controls
121 lines (105 loc) · 4.95 KB
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
package openproject
import (
"context"
"fmt"
"math"
"net/url"
)
// UserService handles users for the OpenProject instance / API.
type UserService struct {
client *Client
}
// User is the object representing OpenProject users.
type User struct {
Type string `json:"_type,omitempty" structs:"_type,omitempty"`
ID int `json:"id,omitempty" structs:"id,omitempty"`
Name string `json:"name,omitempty" structs:"name,omitempty"`
CreatedAt *Time `json:"createdAt,omitempty" structs:"createdAt,omitempty"`
UpdatedAt *Time `json:"updatedAt,omitempty" structs:"updatedAt,omitempty"`
Login string `json:"login,omitempty" structs:"login,omitempty"`
Admin bool `json:"admin,omitempty" structs:"admin,omitempty"`
FirstName string `json:"firstName,omitempty" structs:"firstName,omitempty"`
LastName string `json:"lastName,omitempty" structs:"lastName,omitempty"`
Email string `json:"email,omitempty" structs:"email,omitempty"`
Avatar string `json:"avatar,omitempty" structs:"avatar,omitempty"`
Status string `json:"status,omitempty" structs:"status,omitempty"`
IdentityURL interface{} `json:"identityUrl,omitempty"`
Language string `json:"language,omitempty" structs:"language,omitempty"`
Password string `json:"password,omitempty" structs:"password,omitempty"`
Links struct {
Self *OPGenericLink `json:"self,omitempty" structs:"self,omitempty"`
Memberships *OPGenericLink `json:"memberships,omitempty" structs:"memberships,omitempty"`
ShowUser *OPGenericLink `json:"showUser,omitempty" structs:"showUser,omitempty"`
UpdateImmediately *OPGenericLink `json:"updateImmediately,omitempty" structs:"updateImmediately,omitempty"`
Lock *OPGenericLink `json:"lock,omitempty" structs:"lock,omitempty"`
Delete *OPGenericLink `json:"delete,omitempty" structs:"delete,omitempty"`
AuthSource *OPGenericLink `json:"authSource,omitempty" structs:"authSource,omitempty"`
} `json:"_links,omitempty" structs:"_links,omitempty"`
}
// SearchResultUser is a small wrapper around the Search
type SearchResultUser struct {
Embedded searchEmbeddedUser `json:"_embedded" structs:"_embedded"`
PaginationParam
}
func (s *SearchResultUser) TotalPage() int {
return int(math.Ceil(float64(s.Total) / float64(s.PageSize)))
}
func (s *SearchResultUser) ConcatEmbed(users interface{}) {
s.Embedded.Elements = append(s.Embedded.Elements, users.(*SearchResultUser).Embedded.Elements...)
}
// searchEmbeddedUser wraps embedded fields of User object
type searchEmbeddedUser struct {
Elements []User `json:"elements" structs:"elements"`
}
// GetWithContext gets user info from OpenProject using its Account ID
// TODO: Implement GetList and adapt tests
func (s *UserService) GetWithContext(ctx context.Context, accountID string) (*User, *Response, error) {
apiEndpoint := fmt.Sprintf("api/v3/users?id=%s", accountID)
Obj, Resp, err := GetWithContext(ctx, s, apiEndpoint)
if err != nil {
return nil, Resp, err
}
return Obj.(*User), Resp, err
}
// Get wraps GetWithContext using the background context.
func (s *UserService) Get(accountID string) (*User, *Response, error) {
return s.GetWithContext(context.Background(), accountID)
}
// GetListWithContext will retrieve a list of users using filters
func (s *UserService) GetListWithContext(ctx context.Context, options *FilterOptions, offset int, pageSize int) (*SearchResultUser, *Response, error) {
u := url.URL{
Path: "api/v3/users",
}
objList, resp, err := GetListWithContext(ctx, s, u.String(), options, offset, pageSize)
if err != nil {
return nil, resp, err
}
return objList.(*SearchResultUser), resp, err
}
// GetList wraps GetListWithContext using the background context.
func (s *UserService) GetList(options *FilterOptions, offset int, pageSize int) (*SearchResultUser, *Response, error) {
return s.GetListWithContext(context.Background(), options, offset, pageSize)
}
// CreateWithContext creates a user from a JSON representation.
func (s *UserService) CreateWithContext(ctx context.Context, user *User) (*User, *Response, error) {
apiEndpoint := "api/v3/users"
userResponse, resp, err := CreateWithContext(ctx, user, s, apiEndpoint)
if err != nil {
return nil, resp, err
}
return userResponse.(*User), resp, err
}
// Create wraps CreateWithContext using the background context.
func (s *UserService) Create(user *User) (*User, *Response, error) {
return s.CreateWithContext(context.Background(), user)
}
// DeleteWithContext will delete a single user.
func (s *UserService) DeleteWithContext(ctx context.Context, userID string) (*Response, error) {
apiEndPoint := fmt.Sprintf("api/v3/users/%s", userID)
resp, err := DeleteWithContext(ctx, s, apiEndPoint)
return resp, err
}
// Delete wraps DeleteWithContext using the background context.
func (s *UserService) Delete(userID string) (*Response, error) {
return s.DeleteWithContext(context.Background(), userID)
}