forked from cloudfoundry/go-uaa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generated_client.go
135 lines (123 loc) · 3.44 KB
/
generated_client.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
// Code generated by go-uaa/generator; DO NOT EDIT.
package uaa
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
)
// GetClient with the given clientID.
func (a *API) GetClient(clientID string) (*Client, error) {
u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", ClientsEndpoint, clientID))
client := &Client{}
err := a.doJSON(http.MethodGet, &u, nil, client, true)
if err != nil {
return nil, err
}
return client, nil
}
// CreateClient creates the given client.
func (a *API) CreateClient(client Client) (*Client, error) {
u := urlWithPath(*a.TargetURL, ClientsEndpoint)
created := &Client{}
j, err := json.Marshal(client)
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
}
// UpdateClient updates the given client.
func (a *API) UpdateClient(client Client) (*Client, error) {
u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", ClientsEndpoint, client.Identifier()))
created := &Client{}
j, err := json.Marshal(client)
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
}
// DeleteClient deletes the client with the given client ID.
func (a *API) DeleteClient(clientID string) (*Client, error) {
if clientID == "" {
return nil, errors.New("clientID cannot be blank")
}
u := urlWithPath(*a.TargetURL, fmt.Sprintf("%s/%s", ClientsEndpoint, clientID))
deleted := &Client{}
err := a.doJSON(http.MethodDelete, &u, nil, deleted, true)
if err != nil {
return nil, err
}
return deleted, nil
}
// ListClients with the given filter, sortBy, attributes, sortOrder, startIndex
// (1-based), and count (default 100).
// If successful, ListClients returns the clients and the total itemsPerPage of clients for
// all pages. If unsuccessful, ListClients returns the error.
func (a *API) ListClients(filter string, sortBy string, sortOrder SortOrder, startIndex int, itemsPerPage int) ([]Client, Page, error) {
u := urlWithPath(*a.TargetURL, ClientsEndpoint)
query := url.Values{}
if filter != "" {
query.Set("filter", filter)
}
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()
clients := &paginatedClientList{}
err := a.doJSON(http.MethodGet, &u, nil, clients, true)
if err != nil {
return nil, Page{}, err
}
page := Page{
StartIndex: clients.StartIndex,
ItemsPerPage: clients.ItemsPerPage,
TotalResults: clients.TotalResults,
}
return clients.Resources, page, err
}
// ListAllClients retrieves UAA clients
func (a *API) ListAllClients(filter string, sortBy string, sortOrder SortOrder) ([]Client, error) {
page := Page{
StartIndex: 1,
ItemsPerPage: 100,
}
var (
results []Client
currentPage []Client
err error
)
for {
currentPage, page, err = a.ListClients(filter, sortBy, 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
}