-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathgenerated_user.go
138 lines (126 loc) · 3.43 KB
/
generated_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
// Code generated by go-uaa/generator; DO NOT EDIT.
package uaa
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
)
// GetUser with the given userID.
func (a *API) GetUser(userID string) (*User, error) {
u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", UsersEndpoint, userID))
user := &User{}
err := a.doJSON(http.MethodGet, &u, nil, user, true)
if err != nil {
return nil, err
}
return user, nil
}
// CreateUser creates the given user.
func (a *API) CreateUser(user User) (*User, error) {
u := urlWithPath(*a.TargetURL, UsersEndpoint)
created := &User{}
j, err := json.Marshal(user)
if err != nil {
return nil, err
}
err = a.doJSON(http.MethodPost, &u, bytes.NewBuffer([]byte(j)), created, true)
if err != nil {
return nil, err
}
return created, nil
}
// UpdateUser updates the given user.
func (a *API) UpdateUser(user User) (*User, error) {
u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", UsersEndpoint, user.Identifier()))
created := &User{}
j, err := json.Marshal(user)
if err != nil {
return nil, err
}
err = a.doJSON(http.MethodPut, &u, bytes.NewBuffer([]byte(j)), created, true)
if err != nil {
return nil, err
}
return created, nil
}
// DeleteUser deletes the user with the given user ID.
func (a *API) DeleteUser(userID string) (*User, error) {
if userID == "" {
return nil, errors.New("userID cannot be blank")
}
u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", UsersEndpoint, userID))
deleted := &User{}
err := a.doJSON(http.MethodDelete, &u, nil, deleted, true)
if err != nil {
return nil, err
}
return deleted, nil
}
// ListUsers with the given filter, sortBy, attributes, sortOrder, startIndex
// (1-based), and count (default 100).
// If successful, ListUsers returns the users and the total itemsPerPage of users for
// all pages. If unsuccessful, ListUsers returns the error.
func (a *API) ListUsers(filter string, sortBy string, attributes string, sortOrder SortOrder, startIndex int, itemsPerPage int) ([]User, Page, error) {
u := urlWithPath(*a.TargetURL, UsersEndpoint)
query := url.Values{}
if filter != "" {
query.Set("filter", filter)
}
if attributes != "" {
query.Set("attributes", attributes)
}
if sortBy != "" {
query.Set("sortBy", sortBy)
}
if sortOrder != "" {
query.Set("sortOrder", string(sortOrder))
}
if startIndex == 0 {
startIndex = 1
}
query.Set("startIndex", strconv.Itoa(startIndex))
if itemsPerPage == 0 {
itemsPerPage = 100
}
query.Set("count", strconv.Itoa(itemsPerPage))
u.RawQuery = query.Encode()
users := &paginatedUserList{}
err := a.doJSON(http.MethodGet, &u, nil, users, true)
if err != nil {
return nil, Page{}, err
}
page := Page{
StartIndex: users.StartIndex,
ItemsPerPage: users.ItemsPerPage,
TotalResults: users.TotalResults,
}
return users.Resources, page, err
}
// ListAllUsers retrieves UAA users
func (a *API) ListAllUsers(filter string, sortBy string, attributes string, sortOrder SortOrder) ([]User, error) {
page := Page{
StartIndex: 1,
ItemsPerPage: 100,
}
var (
results []User
currentPage []User
err error
)
for {
currentPage, page, err = a.ListUsers(filter, sortBy, attributes, sortOrder, page.StartIndex, page.ItemsPerPage)
if err != nil {
return nil, err
}
results = append(results, currentPage...)
if (page.StartIndex + page.ItemsPerPage) > page.TotalResults {
break
}
page.StartIndex = page.StartIndex + page.ItemsPerPage
}
return results, nil
}