-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathplan.go
593 lines (521 loc) · 22.1 KB
/
plan.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package metronome
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"time"
"github.com/Metronome-Industries/metronome-go/internal/apijson"
"github.com/Metronome-Industries/metronome-go/internal/apiquery"
"github.com/Metronome-Industries/metronome-go/internal/param"
"github.com/Metronome-Industries/metronome-go/internal/requestconfig"
"github.com/Metronome-Industries/metronome-go/option"
"github.com/Metronome-Industries/metronome-go/packages/pagination"
"github.com/Metronome-Industries/metronome-go/shared"
)
// PlanService contains methods and other services that help with interacting with
// the metronome 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 [NewPlanService] method instead.
type PlanService struct {
Options []option.RequestOption
}
// NewPlanService 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 NewPlanService(opts ...option.RequestOption) (r *PlanService) {
r = &PlanService{}
r.Options = opts
return
}
// List all available plans.
func (r *PlanService) List(ctx context.Context, query PlanListParams, opts ...option.RequestOption) (res *pagination.CursorPage[PlanListResponse], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "plans"
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
}
// List all available plans.
func (r *PlanService) ListAutoPaging(ctx context.Context, query PlanListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[PlanListResponse] {
return pagination.NewCursorPageAutoPager(r.List(ctx, query, opts...))
}
// Fetch high level details of a specific plan.
func (r *PlanService) GetDetails(ctx context.Context, query PlanGetDetailsParams, opts ...option.RequestOption) (res *PlanGetDetailsResponse, err error) {
opts = append(r.Options[:], opts...)
if query.PlanID.Value == "" {
err = errors.New("missing required plan_id parameter")
return
}
path := fmt.Sprintf("planDetails/%s", query.PlanID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// Fetches a list of charges of a specific plan.
func (r *PlanService) ListCharges(ctx context.Context, params PlanListChargesParams, opts ...option.RequestOption) (res *pagination.CursorPage[PlanListChargesResponse], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if params.PlanID.Value == "" {
err = errors.New("missing required plan_id parameter")
return
}
path := fmt.Sprintf("planDetails/%s/charges", params.PlanID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// Fetches a list of charges of a specific plan.
func (r *PlanService) ListChargesAutoPaging(ctx context.Context, params PlanListChargesParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[PlanListChargesResponse] {
return pagination.NewCursorPageAutoPager(r.ListCharges(ctx, params, opts...))
}
// Fetches a list of customers on a specific plan (by default, only currently
// active plans are included)
func (r *PlanService) ListCustomers(ctx context.Context, params PlanListCustomersParams, opts ...option.RequestOption) (res *pagination.CursorPage[PlanListCustomersResponse], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
if params.PlanID.Value == "" {
err = errors.New("missing required plan_id parameter")
return
}
path := fmt.Sprintf("planDetails/%s/customers", params.PlanID)
cfg, err := requestconfig.NewRequestConfig(ctx, http.MethodGet, path, params, &res, opts...)
if err != nil {
return nil, err
}
err = cfg.Execute()
if err != nil {
return nil, err
}
res.SetPageConfig(cfg, raw)
return res, nil
}
// Fetches a list of customers on a specific plan (by default, only currently
// active plans are included)
func (r *PlanService) ListCustomersAutoPaging(ctx context.Context, params PlanListCustomersParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[PlanListCustomersResponse] {
return pagination.NewCursorPageAutoPager(r.ListCustomers(ctx, params, opts...))
}
type PlanDetail struct {
ID string `json:"id,required" format:"uuid"`
CustomFields map[string]string `json:"custom_fields,required"`
Name string `json:"name,required"`
CreditGrants []PlanDetailCreditGrant `json:"credit_grants"`
Description string `json:"description"`
Minimums []PlanDetailMinimum `json:"minimums"`
OverageRates []PlanDetailOverageRate `json:"overage_rates"`
JSON planDetailJSON `json:"-"`
}
// planDetailJSON contains the JSON metadata for the struct [PlanDetail]
type planDetailJSON struct {
ID apijson.Field
CustomFields apijson.Field
Name apijson.Field
CreditGrants apijson.Field
Description apijson.Field
Minimums apijson.Field
OverageRates apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanDetail) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planDetailJSON) RawJSON() string {
return r.raw
}
type PlanDetailCreditGrant struct {
AmountGranted float64 `json:"amount_granted,required"`
AmountGrantedCreditType shared.CreditTypeData `json:"amount_granted_credit_type,required"`
AmountPaid float64 `json:"amount_paid,required"`
AmountPaidCreditType shared.CreditTypeData `json:"amount_paid_credit_type,required"`
EffectiveDuration float64 `json:"effective_duration,required"`
Name string `json:"name,required"`
Priority string `json:"priority,required"`
SendInvoice bool `json:"send_invoice,required"`
Reason string `json:"reason"`
RecurrenceDuration float64 `json:"recurrence_duration"`
RecurrenceInterval float64 `json:"recurrence_interval"`
JSON planDetailCreditGrantJSON `json:"-"`
}
// planDetailCreditGrantJSON contains the JSON metadata for the struct
// [PlanDetailCreditGrant]
type planDetailCreditGrantJSON struct {
AmountGranted apijson.Field
AmountGrantedCreditType apijson.Field
AmountPaid apijson.Field
AmountPaidCreditType apijson.Field
EffectiveDuration apijson.Field
Name apijson.Field
Priority apijson.Field
SendInvoice apijson.Field
Reason apijson.Field
RecurrenceDuration apijson.Field
RecurrenceInterval apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanDetailCreditGrant) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planDetailCreditGrantJSON) RawJSON() string {
return r.raw
}
type PlanDetailMinimum struct {
CreditType shared.CreditTypeData `json:"credit_type,required"`
Name string `json:"name,required"`
// Used in price ramps. Indicates how many billing periods pass before the charge
// applies.
StartPeriod float64 `json:"start_period,required"`
Value float64 `json:"value,required"`
JSON planDetailMinimumJSON `json:"-"`
}
// planDetailMinimumJSON contains the JSON metadata for the struct
// [PlanDetailMinimum]
type planDetailMinimumJSON struct {
CreditType apijson.Field
Name apijson.Field
StartPeriod apijson.Field
Value apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanDetailMinimum) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planDetailMinimumJSON) RawJSON() string {
return r.raw
}
type PlanDetailOverageRate struct {
CreditType shared.CreditTypeData `json:"credit_type,required"`
FiatCreditType shared.CreditTypeData `json:"fiat_credit_type,required"`
// Used in price ramps. Indicates how many billing periods pass before the charge
// applies.
StartPeriod float64 `json:"start_period,required"`
ToFiatConversionFactor float64 `json:"to_fiat_conversion_factor,required"`
JSON planDetailOverageRateJSON `json:"-"`
}
// planDetailOverageRateJSON contains the JSON metadata for the struct
// [PlanDetailOverageRate]
type planDetailOverageRateJSON struct {
CreditType apijson.Field
FiatCreditType apijson.Field
StartPeriod apijson.Field
ToFiatConversionFactor apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanDetailOverageRate) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planDetailOverageRateJSON) RawJSON() string {
return r.raw
}
type PlanListResponse struct {
ID string `json:"id,required" format:"uuid"`
Description string `json:"description,required"`
Name string `json:"name,required"`
CustomFields map[string]string `json:"custom_fields"`
JSON planListResponseJSON `json:"-"`
}
// planListResponseJSON contains the JSON metadata for the struct
// [PlanListResponse]
type planListResponseJSON struct {
ID apijson.Field
Description apijson.Field
Name apijson.Field
CustomFields apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanListResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planListResponseJSON) RawJSON() string {
return r.raw
}
type PlanGetDetailsResponse struct {
Data PlanDetail `json:"data,required"`
JSON planGetDetailsResponseJSON `json:"-"`
}
// planGetDetailsResponseJSON contains the JSON metadata for the struct
// [PlanGetDetailsResponse]
type planGetDetailsResponseJSON struct {
Data apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanGetDetailsResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planGetDetailsResponseJSON) RawJSON() string {
return r.raw
}
type PlanListChargesResponse struct {
ID string `json:"id,required" format:"uuid"`
ChargeType PlanListChargesResponseChargeType `json:"charge_type,required"`
CreditType shared.CreditTypeData `json:"credit_type,required"`
CustomFields map[string]string `json:"custom_fields,required"`
Name string `json:"name,required"`
Prices []PlanListChargesResponsePrice `json:"prices,required"`
ProductID string `json:"product_id,required"`
ProductName string `json:"product_name,required"`
Quantity float64 `json:"quantity"`
// Used in price ramps. Indicates how many billing periods pass before the charge
// applies.
StartPeriod float64 `json:"start_period"`
// Used in pricing tiers. Indicates how often the tier resets. Default is 1 - the
// tier count resets every billing period.
TierResetFrequency float64 `json:"tier_reset_frequency"`
// Specifies how quantities for usage based charges will be converted.
UnitConversion PlanListChargesResponseUnitConversion `json:"unit_conversion"`
JSON planListChargesResponseJSON `json:"-"`
}
// planListChargesResponseJSON contains the JSON metadata for the struct
// [PlanListChargesResponse]
type planListChargesResponseJSON struct {
ID apijson.Field
ChargeType apijson.Field
CreditType apijson.Field
CustomFields apijson.Field
Name apijson.Field
Prices apijson.Field
ProductID apijson.Field
ProductName apijson.Field
Quantity apijson.Field
StartPeriod apijson.Field
TierResetFrequency apijson.Field
UnitConversion apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanListChargesResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planListChargesResponseJSON) RawJSON() string {
return r.raw
}
type PlanListChargesResponseChargeType string
const (
PlanListChargesResponseChargeTypeUsage PlanListChargesResponseChargeType = "usage"
PlanListChargesResponseChargeTypeFixed PlanListChargesResponseChargeType = "fixed"
PlanListChargesResponseChargeTypeComposite PlanListChargesResponseChargeType = "composite"
PlanListChargesResponseChargeTypeMinimum PlanListChargesResponseChargeType = "minimum"
PlanListChargesResponseChargeTypeSeat PlanListChargesResponseChargeType = "seat"
)
func (r PlanListChargesResponseChargeType) IsKnown() bool {
switch r {
case PlanListChargesResponseChargeTypeUsage, PlanListChargesResponseChargeTypeFixed, PlanListChargesResponseChargeTypeComposite, PlanListChargesResponseChargeTypeMinimum, PlanListChargesResponseChargeTypeSeat:
return true
}
return false
}
type PlanListChargesResponsePrice struct {
// Used in pricing tiers. Indicates at what metric value the price applies.
Tier float64 `json:"tier,required"`
Value float64 `json:"value,required"`
CollectionInterval float64 `json:"collection_interval"`
CollectionSchedule string `json:"collection_schedule"`
Quantity float64 `json:"quantity"`
JSON planListChargesResponsePriceJSON `json:"-"`
}
// planListChargesResponsePriceJSON contains the JSON metadata for the struct
// [PlanListChargesResponsePrice]
type planListChargesResponsePriceJSON struct {
Tier apijson.Field
Value apijson.Field
CollectionInterval apijson.Field
CollectionSchedule apijson.Field
Quantity apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanListChargesResponsePrice) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planListChargesResponsePriceJSON) RawJSON() string {
return r.raw
}
// Specifies how quantities for usage based charges will be converted.
type PlanListChargesResponseUnitConversion struct {
// The conversion factor
DivisionFactor float64 `json:"division_factor,required"`
// Whether usage should be rounded down or up to the nearest whole number. If null,
// quantity will be rounded to 20 decimal places.
RoundingBehavior PlanListChargesResponseUnitConversionRoundingBehavior `json:"rounding_behavior"`
JSON planListChargesResponseUnitConversionJSON `json:"-"`
}
// planListChargesResponseUnitConversionJSON contains the JSON metadata for the
// struct [PlanListChargesResponseUnitConversion]
type planListChargesResponseUnitConversionJSON struct {
DivisionFactor apijson.Field
RoundingBehavior apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanListChargesResponseUnitConversion) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planListChargesResponseUnitConversionJSON) RawJSON() string {
return r.raw
}
// Whether usage should be rounded down or up to the nearest whole number. If null,
// quantity will be rounded to 20 decimal places.
type PlanListChargesResponseUnitConversionRoundingBehavior string
const (
PlanListChargesResponseUnitConversionRoundingBehaviorFloor PlanListChargesResponseUnitConversionRoundingBehavior = "floor"
PlanListChargesResponseUnitConversionRoundingBehaviorCeiling PlanListChargesResponseUnitConversionRoundingBehavior = "ceiling"
)
func (r PlanListChargesResponseUnitConversionRoundingBehavior) IsKnown() bool {
switch r {
case PlanListChargesResponseUnitConversionRoundingBehaviorFloor, PlanListChargesResponseUnitConversionRoundingBehaviorCeiling:
return true
}
return false
}
type PlanListCustomersResponse struct {
CustomerDetails CustomerDetail `json:"customer_details,required"`
PlanDetails PlanListCustomersResponsePlanDetails `json:"plan_details,required"`
JSON planListCustomersResponseJSON `json:"-"`
}
// planListCustomersResponseJSON contains the JSON metadata for the struct
// [PlanListCustomersResponse]
type planListCustomersResponseJSON struct {
CustomerDetails apijson.Field
PlanDetails apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanListCustomersResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planListCustomersResponseJSON) RawJSON() string {
return r.raw
}
type PlanListCustomersResponsePlanDetails struct {
ID string `json:"id,required" format:"uuid"`
CustomFields map[string]string `json:"custom_fields,required"`
CustomerPlanID string `json:"customer_plan_id,required" format:"uuid"`
Name string `json:"name,required"`
// The start date of the plan
StartingOn time.Time `json:"starting_on,required" format:"date-time"`
// The end date of the plan
EndingBefore time.Time `json:"ending_before,nullable" format:"date-time"`
JSON planListCustomersResponsePlanDetailsJSON `json:"-"`
}
// planListCustomersResponsePlanDetailsJSON contains the JSON metadata for the
// struct [PlanListCustomersResponsePlanDetails]
type planListCustomersResponsePlanDetailsJSON struct {
ID apijson.Field
CustomFields apijson.Field
CustomerPlanID apijson.Field
Name apijson.Field
StartingOn apijson.Field
EndingBefore apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *PlanListCustomersResponsePlanDetails) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r planListCustomersResponsePlanDetailsJSON) RawJSON() string {
return r.raw
}
type PlanListParams struct {
// Max number of results that should be returned
Limit param.Field[int64] `query:"limit"`
// Cursor that indicates where the next page of results should start.
NextPage param.Field[string] `query:"next_page"`
}
// URLQuery serializes [PlanListParams]'s query parameters as `url.Values`.
func (r PlanListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type PlanGetDetailsParams struct {
PlanID param.Field[string] `path:"plan_id,required" format:"uuid"`
}
type PlanListChargesParams struct {
PlanID param.Field[string] `path:"plan_id,required" format:"uuid"`
// Max number of results that should be returned
Limit param.Field[int64] `query:"limit"`
// Cursor that indicates where the next page of results should start.
NextPage param.Field[string] `query:"next_page"`
}
// URLQuery serializes [PlanListChargesParams]'s query parameters as `url.Values`.
func (r PlanListChargesParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type PlanListCustomersParams struct {
PlanID param.Field[string] `path:"plan_id,required" format:"uuid"`
// Max number of results that should be returned
Limit param.Field[int64] `query:"limit"`
// Cursor that indicates where the next page of results should start.
NextPage param.Field[string] `query:"next_page"`
// Status of customers on a given plan. Defaults to `active`.
//
// - `all` - Return current, past, and upcoming customers of the plan.
// - `active` - Return current customers of the plan.
// - `ended` - Return past customers of the plan.
// - `upcoming` - Return upcoming customers of the plan.
//
// Multiple statuses can be OR'd together using commas, e.g. `active,ended`.
// **Note:** `ended,upcoming` combination is not yet supported.
Status param.Field[PlanListCustomersParamsStatus] `query:"status"`
}
// URLQuery serializes [PlanListCustomersParams]'s query parameters as
// `url.Values`.
func (r PlanListCustomersParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
// Status of customers on a given plan. Defaults to `active`.
//
// - `all` - Return current, past, and upcoming customers of the plan.
// - `active` - Return current customers of the plan.
// - `ended` - Return past customers of the plan.
// - `upcoming` - Return upcoming customers of the plan.
//
// Multiple statuses can be OR'd together using commas, e.g. `active,ended`.
// **Note:** `ended,upcoming` combination is not yet supported.
type PlanListCustomersParamsStatus string
const (
PlanListCustomersParamsStatusAll PlanListCustomersParamsStatus = "all"
PlanListCustomersParamsStatusActive PlanListCustomersParamsStatus = "active"
PlanListCustomersParamsStatusEnded PlanListCustomersParamsStatus = "ended"
PlanListCustomersParamsStatusUpcoming PlanListCustomersParamsStatus = "upcoming"
)
func (r PlanListCustomersParamsStatus) IsKnown() bool {
switch r {
case PlanListCustomersParamsStatusAll, PlanListCustomersParamsStatusActive, PlanListCustomersParamsStatusEnded, PlanListCustomersParamsStatusUpcoming:
return true
}
return false
}