Repository navigation
Expand file tree
/
Copy pathuser.go
More file actions
312 lines (286 loc) · 11.7 KB
/
Copy pathuser.go
File metadata and controls
312 lines (286 loc) · 11.7 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
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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package sentdm
import (
"context"
"errors"
"fmt"
"net/http"
"slices"
"time"
"github.com/sentdm/sent-dm-go/internal/apijson"
shimjson "github.com/sentdm/sent-dm-go/internal/encoding/json"
"github.com/sentdm/sent-dm-go/internal/requestconfig"
"github.com/sentdm/sent-dm-go/option"
"github.com/sentdm/sent-dm-go/packages/param"
"github.com/sentdm/sent-dm-go/packages/respjson"
)
// The people who can sign in to your organization, and what each may do.
//
// Users are dashboard access and nothing else — they do not send, and removing one
// does not affect traffic. An API key is not a user: it belongs to the
// organization or to a sender profile, so revoking a person's access leaves your
// integration running.
//
// UserService contains methods and other services that help with interacting with
// the Sent API.
//
// Note, unlike clients, this service does not read variables from the environment
// automatically. You should not instantiate this service directly, and instead use
// the [NewUserService] method instead.
type UserService struct {
Options []option.RequestOption
}
// NewUserService generates a new service that applies the given options to each
// request. These options are applied after the parent client's options (if there
// is one), and before any request-specific options.
func NewUserService(opts ...option.RequestOption) (r UserService) {
r = UserService{}
r.Options = opts
return
}
// Retrieves detailed information about a specific user in an organization or
// profile. Requires developer role or higher.
func (r *UserService) Get(ctx context.Context, userID string, query UserGetParams, opts ...option.RequestOption) (res *APIResponseOfUser, err error) {
if !param.IsOmitted(query.XProfileID) {
opts = append(opts, option.WithHeader("x-profile-id", fmt.Sprintf("%v", query.XProfileID.Value)))
}
opts = slices.Concat(r.Options, opts)
if userID == "" {
err = errors.New("missing required userId parameter")
return nil, err
}
path := fmt.Sprintf("v3/users/%s", userID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// Retrieves all users who have access to the organization or profile identified by
// the API key, including their roles and status. Shows invited users (pending
// acceptance) and active users. Requires developer role or higher.
func (r *UserService) List(ctx context.Context, query UserListParams, opts ...option.RequestOption) (res *UserListResponse, err error) {
if !param.IsOmitted(query.XProfileID) {
opts = append(opts, option.WithHeader("x-profile-id", fmt.Sprintf("%v", query.XProfileID.Value)))
}
opts = slices.Concat(r.Options, opts)
path := "v3/users"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// Sends an invitation to a user to join the organization or profile with a
// specific role. Requires admin role. The user will receive an invitation email
// with a token to accept. Invitation tokens expire after 7 days.
func (r *UserService) Invite(ctx context.Context, params UserInviteParams, opts ...option.RequestOption) (res *APIResponseOfUser, err error) {
if !param.IsOmitted(params.IdempotencyKey) {
opts = append(opts, option.WithHeader("Idempotency-Key", fmt.Sprintf("%v", params.IdempotencyKey.Value)))
}
if !param.IsOmitted(params.XProfileID) {
opts = append(opts, option.WithHeader("x-profile-id", fmt.Sprintf("%v", params.XProfileID.Value)))
}
opts = slices.Concat(r.Options, opts)
path := "v3/users"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, params, &res, opts...)
return res, err
}
// Removes a user's access to an organization or profile. Requires admin role. You
// cannot remove yourself or remove the last admin.
func (r *UserService) Remove(ctx context.Context, userID string, params UserRemoveParams, opts ...option.RequestOption) (err error) {
if !param.IsOmitted(params.XProfileID) {
opts = append(opts, option.WithHeader("x-profile-id", fmt.Sprintf("%v", params.XProfileID.Value)))
}
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithHeader("Accept", "*/*")}, opts...)
if userID == "" {
err = errors.New("missing required userId parameter")
return err
}
path := fmt.Sprintf("v3/users/%s", userID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodDelete, path, params, nil, opts...)
return err
}
// Updates a user's role in the organization or profile. Requires admin role. You
// cannot change your own role or demote the last admin.
func (r *UserService) UpdateRole(ctx context.Context, userID string, params UserUpdateRoleParams, opts ...option.RequestOption) (res *APIResponseOfUser, err error) {
if !param.IsOmitted(params.IdempotencyKey) {
opts = append(opts, option.WithHeader("Idempotency-Key", fmt.Sprintf("%v", params.IdempotencyKey.Value)))
}
if !param.IsOmitted(params.XProfileID) {
opts = append(opts, option.WithHeader("x-profile-id", fmt.Sprintf("%v", params.XProfileID.Value)))
}
opts = slices.Concat(r.Options, opts)
if userID == "" {
err = errors.New("missing required userId parameter")
return nil, err
}
path := fmt.Sprintf("v3/users/%s", userID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPatch, path, params, &res, opts...)
return res, err
}
// Standard API response envelope for all v3 endpoints
type APIResponseOfUser struct {
// User response for v3 API
Data UserResponse `json:"data" api:"nullable"`
// Error information
Error ErrorDetail `json:"error" api:"nullable"`
// Request and response metadata
Meta APIMeta `json:"meta"`
// Indicates whether the request was successful
Success bool `json:"success"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Error respjson.Field
Meta respjson.Field
Success respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r APIResponseOfUser) RawJSON() string { return r.JSON.raw }
func (r *APIResponseOfUser) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// User response for v3 API
type UserResponse struct {
// User unique identifier
ID string `json:"id" format:"uuid"`
// When the user was added to the organization
CreatedAt time.Time `json:"created_at" format:"date-time"`
// Which customer owns this — the key's own, or the profile named in x-profile-id.
// Says whose resource this is, which the resource's own id does not.
CustomerID string `json:"customer_id" format:"uuid"`
// User email address
Email string `json:"email"`
// When the user was invited
InvitedAt time.Time `json:"invited_at" api:"nullable" format:"date-time"`
// When the user last logged in
LastLoginAt time.Time `json:"last_login_at" api:"nullable" format:"date-time"`
// User full name
Name string `json:"name"`
// User role in the organization: admin, billing, developer
Role string `json:"role"`
// User status: active, invited, suspended, rejected
Status string `json:"status"`
// When the user record was last updated
UpdatedAt time.Time `json:"updated_at" api:"nullable" format:"date-time"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
ID respjson.Field
CreatedAt respjson.Field
CustomerID respjson.Field
Email respjson.Field
InvitedAt respjson.Field
LastLoginAt respjson.Field
Name respjson.Field
Role respjson.Field
Status respjson.Field
UpdatedAt respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r UserResponse) RawJSON() string { return r.JSON.raw }
func (r *UserResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// Standard API response envelope for all v3 endpoints
type UserListResponse struct {
// The users in the organization.
Data UserListResponseData `json:"data" api:"nullable"`
// Error information
Error ErrorDetail `json:"error" api:"nullable"`
// Request and response metadata
Meta APIMeta `json:"meta"`
// Indicates whether the request was successful
Success bool `json:"success"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Data respjson.Field
Error respjson.Field
Meta respjson.Field
Success respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r UserListResponse) RawJSON() string { return r.JSON.raw }
func (r *UserListResponse) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
// The users in the organization.
type UserListResponseData struct {
// Pagination metadata for list responses
Pagination PaginationMeta `json:"pagination"`
// The users on this page.
Users []UserResponse `json:"users"`
// JSON contains metadata for fields, check presence with [respjson.Field.Valid].
JSON struct {
Pagination respjson.Field
Users respjson.Field
ExtraFields map[string]respjson.Field
raw string
} `json:"-"`
}
// Returns the unmodified JSON received from the API
func (r UserListResponseData) RawJSON() string { return r.JSON.raw }
func (r *UserListResponseData) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type UserGetParams struct {
XProfileID param.Opt[string] `header:"x-profile-id,omitzero" format:"uuid" json:"-"`
paramObj
}
type UserListParams struct {
XProfileID param.Opt[string] `header:"x-profile-id,omitzero" format:"uuid" json:"-"`
paramObj
}
type UserInviteParams struct {
// User email address (required)
Email param.Opt[string] `json:"email,omitzero" format:"email"`
// User full name (required)
Name param.Opt[string] `json:"name,omitzero"`
// User role: admin, billing, or developer (required)
Role param.Opt[string] `json:"role,omitzero"`
// Sandbox flag - when true, the operation is simulated without side effects Useful
// for testing integrations without actual execution
Sandbox param.Opt[bool] `json:"sandbox,omitzero"`
IdempotencyKey param.Opt[string] `header:"Idempotency-Key,omitzero" json:"-"`
XProfileID param.Opt[string] `header:"x-profile-id,omitzero" format:"uuid" json:"-"`
paramObj
}
func (r UserInviteParams) MarshalJSON() (data []byte, err error) {
type shadow UserInviteParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *UserInviteParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type UserRemoveParams struct {
MutationRequest MutationRequestParam
XProfileID param.Opt[string] `header:"x-profile-id,omitzero" format:"uuid" json:"-"`
paramObj
}
func (r UserRemoveParams) MarshalJSON() (data []byte, err error) {
return shimjson.Marshal(r.MutationRequest)
}
func (r *UserRemoveParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}
type UserUpdateRoleParams struct {
// User role: admin, billing, or developer (required)
Role param.Opt[string] `json:"role,omitzero"`
// Sandbox flag - when true, the operation is simulated without side effects Useful
// for testing integrations without actual execution
Sandbox param.Opt[bool] `json:"sandbox,omitzero"`
IdempotencyKey param.Opt[string] `header:"Idempotency-Key,omitzero" json:"-"`
XProfileID param.Opt[string] `header:"x-profile-id,omitzero" format:"uuid" json:"-"`
paramObj
}
func (r UserUpdateRoleParams) MarshalJSON() (data []byte, err error) {
type shadow UserUpdateRoleParams
return param.MarshalObject(r, (*shadow)(&r))
}
func (r *UserUpdateRoleParams) UnmarshalJSON(data []byte) error {
return apijson.UnmarshalRoot(data, r)
}