-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmembers.go
executable file
·363 lines (314 loc) · 12 KB
/
members.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"time"
)
// Attributes: Object attributes that modifiable only by SumUp applications.
type Attributes map[string]any
// Invite: Pending invitation for membership.
type Invite struct {
// Email address of the invited user.
// Format: email
Email string `json:"email"`
ExpiresAt time.Time `json:"expires_at"`
}
// Member: A member is user within specific resource identified by resource id, resource type, and associated roles.
type Member struct {
// Object attributes that modifiable only by SumUp applications.
Attributes *Attributes `json:"attributes,omitempty"`
CreatedAt time.Time `json:"created_at"`
// ID of the member.
Id string `json:"id"`
// Pending invitation for membership.
Invite *Invite `json:"invite,omitempty"`
// Set of user-defined key-value pairs attached to the object. Partial updates are not supported. When updating, always
// submit whole metadata.
Metadata *Metadata `json:"metadata,omitempty"`
// User's permissions.
Permissions []string `json:"permissions"`
// User's roles.
Roles []string `json:"roles"`
Status MembershipStatus `json:"status"`
UpdatedAt time.Time `json:"updated_at"`
// User information.
User *MembershipUser `json:"user,omitempty"`
}
// MembershipStatus is a schema definition.
type MembershipStatus string
const (
MembershipStatusAccepted MembershipStatus = "accepted"
MembershipStatusDisabled MembershipStatus = "disabled"
MembershipStatusExpired MembershipStatus = "expired"
MembershipStatusPending MembershipStatus = "pending"
MembershipStatusUnknown MembershipStatus = "unknown"
)
// MembershipUser: User information.
type MembershipUser struct {
// Classic identifiers of the user.
Classic *MembershipUserClassic `json:"classic,omitempty"`
// Time when the user has been disabled. Applies only to virtual users (`virtual_user: true`).
DisabledAt *time.Time `json:"disabled_at,omitempty"`
// End-User's preferred e-mail address. Its value MUST conform to the RFC 5322 [RFC5322] addr-spec syntax. The
// RP MUST NOT rely upon this value being unique, for unique identification use ID instead.
Email string `json:"email"`
// Identifier for the End-User (also called Subject).
Id string `json:"id"`
// True if the user has enabled MFA on login.
MfaOnLoginEnabled bool `json:"mfa_on_login_enabled"`
// User's preferred name. Used for display purposes only.
Nickname *string `json:"nickname,omitempty"`
// URL of the End-User's profile picture. This URL refers to an image file (for example, a PNG, JPEG, or GIF
// image file), rather than to a Web page containing an image.
// Format: uri
Picture *string `json:"picture,omitempty"`
// True if the user is a virtual user (operator).
VirtualUser bool `json:"virtual_user"`
}
// MembershipUserClassic: Classic identifiers of the user.
type MembershipUserClassic struct {
// Format: int32
UserId int `json:"user_id"`
}
// Metadata: Set of user-defined key-value pairs attached to the object. Partial updates are not supported. When
// updating, always submit whole metadata.
type Metadata map[string]any
// CreateMerchantMemberBody is a schema definition.
type CreateMerchantMemberBody struct {
// Object attributes that modifiable only by SumUp applications.
Attributes *Attributes `json:"attributes,omitempty"`
// Email address of the member to add.
// Format: email
Email string `json:"email"`
// True if the user is managed by the merchant. In this case, we'll created a virtual user with the provided password
// and nickname.
IsManagedUser *bool `json:"is_managed_user,omitempty"`
// Set of user-defined key-value pairs attached to the object. Partial updates are not supported. When updating, always
// submit whole metadata.
Metadata *Metadata `json:"metadata,omitempty"`
// Nickname of the member to add. Only used if `is_managed_user` is true. Used for display purposes only.
Nickname *string `json:"nickname,omitempty"`
// Password of the member to add. Only used if `is_managed_user` is true.
// Format: password
// Min length: 8
Password *string `json:"password,omitempty"`
// List of roles to assign to the new member.
Roles []string `json:"roles"`
}
// UpdateMerchantMemberBody is a schema definition.
type UpdateMerchantMemberBody struct {
// Object attributes that modifiable only by SumUp applications.
Attributes *Attributes `json:"attributes,omitempty"`
// Set of user-defined key-value pairs attached to the object. Partial updates are not supported. When updating, always
// submit whole metadata.
Metadata *Metadata `json:"metadata,omitempty"`
Roles *[]string `json:"roles,omitempty"`
// Allows you to update user data of managed users.
User *UpdateMerchantMemberBodyUser `json:"user,omitempty"`
}
// UpdateMerchantMemberBodyUser: Allows you to update user data of managed users.
type UpdateMerchantMemberBodyUser struct {
// User's preferred name. Used for display purposes only.
Nickname *string `json:"nickname,omitempty"`
// Password of the member to add. Only used if `is_managed_user` is true.
// Format: password
// Min length: 8
Password *string `json:"password,omitempty"`
}
// ListMerchantMembersParams: query parameters for ListMerchantMembers
type ListMerchantMembersParams struct {
// Filter the returned users by email address prefix.
Email *string
// Maximum number of member to return.
Limit *int
// Offset of the first member to return.
Offset *int
// Filter the returned users by role.
Roles *[]string
// Indicates to skip count query.
Scroll *bool
// Filter the returned members by the membership status.
Status *MembershipStatus
}
// QueryValues converts [ListMerchantMembersParams] into [url.Values].
func (p *ListMerchantMembersParams) QueryValues() url.Values {
q := make(url.Values)
if p.Email != nil {
q.Set("email", *p.Email)
}
if p.Limit != nil {
q.Set("limit", strconv.Itoa(*p.Limit))
}
if p.Offset != nil {
q.Set("offset", strconv.Itoa(*p.Offset))
}
if p.Roles != nil {
for _, v := range *p.Roles {
q.Add("roles", v)
}
}
if p.Scroll != nil {
q.Set("scroll", strconv.FormatBool(*p.Scroll))
}
if p.Status != nil {
q.Set("status", string(*p.Status))
}
return q
}
// ListMerchantMembers200Response is a schema definition.
type ListMerchantMembers200Response struct {
Items []Member `json:"items"`
TotalCount *int `json:"total_count,omitempty"`
}
type MembersService service
// List: List members
// Lists merchant members with their roles and permissions.
func (s *MembersService) List(ctx context.Context, merchantCode string, params ListMerchantMembersParams) (*ListMerchantMembers200Response, error) {
path := fmt.Sprintf("/v0.1/merchants/%v/members", merchantCode)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
req.URL.RawQuery = params.QueryValues().Encode()
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var v ListMerchantMembers200Response
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusNotFound:
return nil, errors.New("Merchant not found.")
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Create: Create a merchant member.
func (s *MembersService) Create(ctx context.Context, merchantCode string, body CreateMerchantMemberBody) (*Member, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/v0.1/merchants/%v/members", merchantCode)
req, err := s.client.NewRequest(ctx, http.MethodPost, path, buf)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusCreated:
var v Member
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusBadRequest:
return nil, errors.New("Invalid request.")
case http.StatusNotFound:
return nil, errors.New("Merchant not found.")
case http.StatusTooManyRequests:
return nil, errors.New("Too many invitations sent to that user. The limit is 10 requests per 5 minutes and the Retry-After header is set to the number of minutes until the reset of the limit.")
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Delete: Delete member
// Deletes member by ID.
func (s *MembersService) Delete(ctx context.Context, merchantCode string, memberId string) error {
path := fmt.Sprintf("/v0.1/merchants/%v/members/%v", merchantCode, memberId)
req, err := s.client.NewRequest(ctx, http.MethodDelete, path, http.NoBody)
if err != nil {
return fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
return nil
case http.StatusNotFound:
return errors.New("Merchant or member not found.")
default:
return fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Get: Get merchant member
// Returns merchant member details.
func (s *MembersService) Get(ctx context.Context, merchantCode string, memberId string) (*Member, error) {
path := fmt.Sprintf("/v0.1/merchants/%v/members/%v", merchantCode, memberId)
req, err := s.client.NewRequest(ctx, http.MethodGet, path, http.NoBody)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var v Member
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusNotFound:
return nil, errors.New("Merchant or member not found.")
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}
// Update: Update merchant member
// Update assigned roles of the member.
func (s *MembersService) Update(ctx context.Context, merchantCode string, memberId string, body UpdateMerchantMemberBody) (*Member, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(body); err != nil {
return nil, fmt.Errorf("encoding json body request failed: %v", err)
}
path := fmt.Sprintf("/v0.1/merchants/%v/members/%v", merchantCode, memberId)
req, err := s.client.NewRequest(ctx, http.MethodPut, path, buf)
if err != nil {
return nil, fmt.Errorf("error building request: %v", err)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
switch resp.StatusCode {
case http.StatusOK:
var v Member
if err := json.NewDecoder(resp.Body).Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
case http.StatusBadRequest:
return nil, errors.New("Cannot set password or nickname for an invited user.")
case http.StatusForbidden:
return nil, errors.New("Cannot change password for managed user. Password was already used before.")
case http.StatusNotFound:
return nil, errors.New("Merchant or member not found.")
case http.StatusConflict:
return nil, errors.New("Cannot update member as some data conflict with existing members.")
default:
return nil, fmt.Errorf("unexpected response %d: %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
}