-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlicense.go
More file actions
436 lines (379 loc) · 16.3 KB
/
license.go
File metadata and controls
436 lines (379 loc) · 16.3 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
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package orb
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"slices"
"time"
"github.com/orbcorp/orb-go/internal/apijson"
"github.com/orbcorp/orb-go/internal/apiquery"
"github.com/orbcorp/orb-go/internal/param"
"github.com/orbcorp/orb-go/internal/requestconfig"
"github.com/orbcorp/orb-go/option"
"github.com/orbcorp/orb-go/packages/pagination"
)
// LicenseService contains methods and other services that help with interacting
// with the orb 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 [NewLicenseService] method instead.
type LicenseService struct {
Options []option.RequestOption
ExternalLicenses *LicenseExternalLicenseService
Usage *LicenseUsageService
}
// NewLicenseService 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 NewLicenseService(opts ...option.RequestOption) (r *LicenseService) {
r = &LicenseService{}
r.Options = opts
r.ExternalLicenses = NewLicenseExternalLicenseService(opts...)
r.Usage = NewLicenseUsageService(opts...)
return
}
// This endpoint is used to create a new license for a user.
//
// If a start date is provided, the license will be activated at the **start** of
// the specified date in the customer's timezone. Otherwise, the activation time
// will default to the **start** of the current day in the customer's timezone.
func (r *LicenseService) New(ctx context.Context, body LicenseNewParams, opts ...option.RequestOption) (res *LicenseNewResponse, err error) {
opts = slices.Concat(r.Options, opts)
path := "licenses"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// This endpoint is used to fetch a license given an identifier.
func (r *LicenseService) Get(ctx context.Context, licenseID string, opts ...option.RequestOption) (res *LicenseGetResponse, err error) {
opts = slices.Concat(r.Options, opts)
if licenseID == "" {
err = errors.New("missing required license_id parameter")
return nil, err
}
path := fmt.Sprintf("licenses/%s", licenseID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return res, err
}
// This endpoint returns a list of all licenses for a subscription.
func (r *LicenseService) List(ctx context.Context, query LicenseListParams, opts ...option.RequestOption) (res *pagination.Page[LicenseListResponse], err error) {
var raw *http.Response
opts = slices.Concat(r.Options, opts)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "licenses"
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, query, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// This endpoint returns a list of all licenses for a subscription.
func (r *LicenseService) ListAutoPaging(ctx context.Context, query LicenseListParams, opts ...option.RequestOption) *pagination.PageAutoPager[LicenseListResponse] {
return pagination.NewPageAutoPager(r.List(ctx, query, opts...))
}
// This endpoint is used to deactivate an existing license.
//
// If an end date is provided, the license will be deactivated at the **start** of
// the specified date in the customer's timezone. Otherwise, the deactivation time
// will default to the **end** of the current day in the customer's timezone.
func (r *LicenseService) Deactivate(ctx context.Context, licenseID string, body LicenseDeactivateParams, opts ...option.RequestOption) (res *LicenseDeactivateResponse, err error) {
opts = slices.Concat(r.Options, opts)
if licenseID == "" {
err = errors.New("missing required license_id parameter")
return nil, err
}
path := fmt.Sprintf("licenses/%s/deactivate", licenseID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return res, err
}
// This endpoint is used to fetch a license given an external license identifier.
func (r *LicenseService) GetByExternalID(ctx context.Context, externalLicenseID string, query LicenseGetByExternalIDParams, opts ...option.RequestOption) (res *LicenseGetByExternalIDResponse, err error) {
opts = slices.Concat(r.Options, opts)
if externalLicenseID == "" {
err = errors.New("missing required external_license_id parameter")
return nil, err
}
path := fmt.Sprintf("licenses/external_license_id/%s", externalLicenseID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, query, &res, opts...)
return res, err
}
type LicenseNewResponse struct {
ID string `json:"id" api:"required"`
EndDate time.Time `json:"end_date" api:"required,nullable" format:"date-time"`
ExternalLicenseID string `json:"external_license_id" api:"required"`
LicenseTypeID string `json:"license_type_id" api:"required"`
StartDate time.Time `json:"start_date" api:"required" format:"date-time"`
Status LicenseNewResponseStatus `json:"status" api:"required"`
SubscriptionID string `json:"subscription_id" api:"required"`
JSON licenseNewResponseJSON `json:"-"`
}
// licenseNewResponseJSON contains the JSON metadata for the struct
// [LicenseNewResponse]
type licenseNewResponseJSON struct {
ID apijson.Field
EndDate apijson.Field
ExternalLicenseID apijson.Field
LicenseTypeID apijson.Field
StartDate apijson.Field
Status apijson.Field
SubscriptionID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *LicenseNewResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r licenseNewResponseJSON) RawJSON() string {
return r.raw
}
type LicenseNewResponseStatus string
const (
LicenseNewResponseStatusActive LicenseNewResponseStatus = "active"
LicenseNewResponseStatusInactive LicenseNewResponseStatus = "inactive"
)
func (r LicenseNewResponseStatus) IsKnown() bool {
switch r {
case LicenseNewResponseStatusActive, LicenseNewResponseStatusInactive:
return true
}
return false
}
type LicenseGetResponse struct {
ID string `json:"id" api:"required"`
EndDate time.Time `json:"end_date" api:"required,nullable" format:"date-time"`
ExternalLicenseID string `json:"external_license_id" api:"required"`
LicenseTypeID string `json:"license_type_id" api:"required"`
StartDate time.Time `json:"start_date" api:"required" format:"date-time"`
Status LicenseGetResponseStatus `json:"status" api:"required"`
SubscriptionID string `json:"subscription_id" api:"required"`
JSON licenseGetResponseJSON `json:"-"`
}
// licenseGetResponseJSON contains the JSON metadata for the struct
// [LicenseGetResponse]
type licenseGetResponseJSON struct {
ID apijson.Field
EndDate apijson.Field
ExternalLicenseID apijson.Field
LicenseTypeID apijson.Field
StartDate apijson.Field
Status apijson.Field
SubscriptionID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *LicenseGetResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r licenseGetResponseJSON) RawJSON() string {
return r.raw
}
type LicenseGetResponseStatus string
const (
LicenseGetResponseStatusActive LicenseGetResponseStatus = "active"
LicenseGetResponseStatusInactive LicenseGetResponseStatus = "inactive"
)
func (r LicenseGetResponseStatus) IsKnown() bool {
switch r {
case LicenseGetResponseStatusActive, LicenseGetResponseStatusInactive:
return true
}
return false
}
type LicenseListResponse struct {
ID string `json:"id" api:"required"`
EndDate time.Time `json:"end_date" api:"required,nullable" format:"date-time"`
ExternalLicenseID string `json:"external_license_id" api:"required"`
LicenseTypeID string `json:"license_type_id" api:"required"`
StartDate time.Time `json:"start_date" api:"required" format:"date-time"`
Status LicenseListResponseStatus `json:"status" api:"required"`
SubscriptionID string `json:"subscription_id" api:"required"`
JSON licenseListResponseJSON `json:"-"`
}
// licenseListResponseJSON contains the JSON metadata for the struct
// [LicenseListResponse]
type licenseListResponseJSON struct {
ID apijson.Field
EndDate apijson.Field
ExternalLicenseID apijson.Field
LicenseTypeID apijson.Field
StartDate apijson.Field
Status apijson.Field
SubscriptionID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *LicenseListResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r licenseListResponseJSON) RawJSON() string {
return r.raw
}
type LicenseListResponseStatus string
const (
LicenseListResponseStatusActive LicenseListResponseStatus = "active"
LicenseListResponseStatusInactive LicenseListResponseStatus = "inactive"
)
func (r LicenseListResponseStatus) IsKnown() bool {
switch r {
case LicenseListResponseStatusActive, LicenseListResponseStatusInactive:
return true
}
return false
}
type LicenseDeactivateResponse struct {
ID string `json:"id" api:"required"`
EndDate time.Time `json:"end_date" api:"required,nullable" format:"date-time"`
ExternalLicenseID string `json:"external_license_id" api:"required"`
LicenseTypeID string `json:"license_type_id" api:"required"`
StartDate time.Time `json:"start_date" api:"required" format:"date-time"`
Status LicenseDeactivateResponseStatus `json:"status" api:"required"`
SubscriptionID string `json:"subscription_id" api:"required"`
JSON licenseDeactivateResponseJSON `json:"-"`
}
// licenseDeactivateResponseJSON contains the JSON metadata for the struct
// [LicenseDeactivateResponse]
type licenseDeactivateResponseJSON struct {
ID apijson.Field
EndDate apijson.Field
ExternalLicenseID apijson.Field
LicenseTypeID apijson.Field
StartDate apijson.Field
Status apijson.Field
SubscriptionID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *LicenseDeactivateResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r licenseDeactivateResponseJSON) RawJSON() string {
return r.raw
}
type LicenseDeactivateResponseStatus string
const (
LicenseDeactivateResponseStatusActive LicenseDeactivateResponseStatus = "active"
LicenseDeactivateResponseStatusInactive LicenseDeactivateResponseStatus = "inactive"
)
func (r LicenseDeactivateResponseStatus) IsKnown() bool {
switch r {
case LicenseDeactivateResponseStatusActive, LicenseDeactivateResponseStatusInactive:
return true
}
return false
}
type LicenseGetByExternalIDResponse struct {
ID string `json:"id" api:"required"`
EndDate time.Time `json:"end_date" api:"required,nullable" format:"date-time"`
ExternalLicenseID string `json:"external_license_id" api:"required"`
LicenseTypeID string `json:"license_type_id" api:"required"`
StartDate time.Time `json:"start_date" api:"required" format:"date-time"`
Status LicenseGetByExternalIDResponseStatus `json:"status" api:"required"`
SubscriptionID string `json:"subscription_id" api:"required"`
JSON licenseGetByExternalIDResponseJSON `json:"-"`
}
// licenseGetByExternalIDResponseJSON contains the JSON metadata for the struct
// [LicenseGetByExternalIDResponse]
type licenseGetByExternalIDResponseJSON struct {
ID apijson.Field
EndDate apijson.Field
ExternalLicenseID apijson.Field
LicenseTypeID apijson.Field
StartDate apijson.Field
Status apijson.Field
SubscriptionID apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *LicenseGetByExternalIDResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r licenseGetByExternalIDResponseJSON) RawJSON() string {
return r.raw
}
type LicenseGetByExternalIDResponseStatus string
const (
LicenseGetByExternalIDResponseStatusActive LicenseGetByExternalIDResponseStatus = "active"
LicenseGetByExternalIDResponseStatusInactive LicenseGetByExternalIDResponseStatus = "inactive"
)
func (r LicenseGetByExternalIDResponseStatus) IsKnown() bool {
switch r {
case LicenseGetByExternalIDResponseStatusActive, LicenseGetByExternalIDResponseStatusInactive:
return true
}
return false
}
type LicenseNewParams struct {
// The external identifier for the license.
ExternalLicenseID param.Field[string] `json:"external_license_id" api:"required"`
LicenseTypeID param.Field[string] `json:"license_type_id" api:"required"`
SubscriptionID param.Field[string] `json:"subscription_id" api:"required"`
// The end date of the license. If not provided, the license will remain active
// until deactivated.
EndDate param.Field[time.Time] `json:"end_date" format:"date"`
// The start date of the license. If not provided, defaults to start of day today
// in the customer's timezone.
StartDate param.Field[time.Time] `json:"start_date" format:"date"`
}
func (r LicenseNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type LicenseListParams struct {
SubscriptionID param.Field[string] `query:"subscription_id" api:"required"`
// Cursor for pagination. This can be populated by the `next_cursor` value returned
// from the initial request.
Cursor param.Field[string] `query:"cursor"`
ExternalLicenseID param.Field[string] `query:"external_license_id"`
LicenseTypeID param.Field[string] `query:"license_type_id"`
// The number of items to fetch. Defaults to 20.
Limit param.Field[int64] `query:"limit"`
Status param.Field[LicenseListParamsStatus] `query:"status"`
}
// URLQuery serializes [LicenseListParams]'s query parameters as `url.Values`.
func (r LicenseListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type LicenseListParamsStatus string
const (
LicenseListParamsStatusActive LicenseListParamsStatus = "active"
LicenseListParamsStatusInactive LicenseListParamsStatus = "inactive"
)
func (r LicenseListParamsStatus) IsKnown() bool {
switch r {
case LicenseListParamsStatusActive, LicenseListParamsStatusInactive:
return true
}
return false
}
type LicenseDeactivateParams struct {
// The date to deactivate the license. If not provided, defaults to end of day
// today in the customer's timezone.
EndDate param.Field[time.Time] `json:"end_date" format:"date"`
}
func (r LicenseDeactivateParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
type LicenseGetByExternalIDParams struct {
// The ID of the license type to fetch the license for.
LicenseTypeID param.Field[string] `query:"license_type_id" api:"required"`
// The ID of the subscription to fetch the license for.
SubscriptionID param.Field[string] `query:"subscription_id" api:"required"`
}
// URLQuery serializes [LicenseGetByExternalIDParams]'s query parameters as
// `url.Values`.
func (r LicenseGetByExternalIDParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatBrackets,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}