-
Notifications
You must be signed in to change notification settings - Fork 1
/
members.go
executable file
·332 lines (275 loc) · 11 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
// Code generated by `gogenitor`. DO NOT EDIT.
package sumup
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
)
// Attributes is Object attributes that modifiable only by SumUp applications.
type Attributes struct {
}
// Invite is Pending invitation for membership.
type Invite struct {
// Email address of the invited user.
Email string `json:"email"`
ExpiresAt time.Time `json:"expires_at"`
}
// Member is 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"`
}
type MembershipStatus string
const (
MembershipStatusAccepted MembershipStatus = "accepted"
MembershipStatusDisabled MembershipStatus = "disabled"
MembershipStatusExpired MembershipStatus = "expired"
MembershipStatusPending MembershipStatus = "pending"
MembershipStatusUnknown MembershipStatus = "unknown"
)
// MembershipUser is 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.
Picture *string `json:"picture,omitempty"`
// True if the user is a virtual user (operator).
VirtualUser bool `json:"virtual_user"`
}
// MembershipUserClassic is Classic identifiers of the user.
type MembershipUserClassic struct {
UserId int `json:"user_id"`
}
// Metadata is Set of user-defined key-value pairs attached to the object. Partial updates are not supported. When updating, always submit whole metadata.
type Metadata struct {
}
// ListMerchantMembersParams are query parameters for ListMerchantMembers
type ListMerchantMembersParams struct {
Email *string `json:"email,omitempty"`
Limit *int `json:"limit,omitempty"`
Offset *int `json:"offset,omitempty"`
Roles *[]string `json:"roles,omitempty"`
Scroll *bool `json:"scroll,omitempty"`
Status *MembershipStatus `json:"status,omitempty"`
}
// ListMerchantMembersResponse is the type definition for a ListMerchantMembersResponse.
type ListMerchantMembersResponse struct {
Items []Member `json:"items"`
TotalCount *int `json:"total_count,omitempty"`
}
// AddMerchantMember request body.
type AddMerchantMemberBody struct {
// Object attributes that modifiable only by SumUp applications.
Attributes *Attributes `json:"attributes,omitempty"`
// Email address of the member to add.
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.
Password *string `json:"password,omitempty"`
// List of roles to assign to the new member.
Roles []string `json:"roles"`
}
// UpdateMerchantMember request body.
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 is 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.
Password *string `json:"password,omitempty"`
}
type MembersService service
// ListMerchantMembers: List members
// Lists merchant members with their roles and permissions.
func (s *MembersService) ListMerchantMembers(ctx context.Context, merchantCode string, params ListMerchantMembersParams) (*ListMerchantMembersResponse, 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)
}
resp, err := s.client.Do(req)
if err != nil {
return nil, fmt.Errorf("error sending request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v ListMerchantMembersResponse
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// AddMerchantMember: Add member to merchant.
func (s *MembersService) AddMerchantMember(ctx context.Context, merchantCode string, body AddMerchantMemberBody) (*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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Member
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// DeleteMerchantMember: Delete member
// Deletes member by ID.
func (s *MembersService) DeleteMerchantMember(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()
if resp.StatusCode >= 500 {
return fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return fmt.Errorf("read error response: %s", err.Error())
}
return &apiErr
}
return nil
}
// GetMerchantMember: Get merchant member
// Returns merchant member details.
func (s *MembersService) GetMerchantMember(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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Member
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}
// UpdateMerchantMember: Update merchant member
// Update assigned roles of the member.
func (s *MembersService) UpdateMerchantMember(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()
if resp.StatusCode >= 500 {
return nil, fmt.Errorf("invalid response: %d - %s", resp.StatusCode, http.StatusText(resp.StatusCode))
}
dec := json.NewDecoder(resp.Body)
if resp.StatusCode >= 400 {
var apiErr APIError
if err := dec.Decode(&apiErr); err != nil {
return nil, fmt.Errorf("read error response: %s", err.Error())
}
return nil, &apiErr
}
var v Member
if err := dec.Decode(&v); err != nil {
return nil, fmt.Errorf("decode response: %s", err.Error())
}
return &v, nil
}