-
Notifications
You must be signed in to change notification settings - Fork 1
/
billablemetric.go
359 lines (314 loc) · 14.5 KB
/
billablemetric.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
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
package metronome
import (
"context"
"errors"
"fmt"
"net/http"
"net/url"
"github.com/Metronome-Industries/metronome-go/internal/apijson"
"github.com/Metronome-Industries/metronome-go/internal/apiquery"
"github.com/Metronome-Industries/metronome-go/internal/pagination"
"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/shared"
)
// BillableMetricService 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 [NewBillableMetricService] method instead.
type BillableMetricService struct {
Options []option.RequestOption
}
// NewBillableMetricService 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 NewBillableMetricService(opts ...option.RequestOption) (r *BillableMetricService) {
r = &BillableMetricService{}
r.Options = opts
return
}
// Creates a new Billable Metric.
func (r *BillableMetricService) New(ctx context.Context, body BillableMetricNewParams, opts ...option.RequestOption) (res *BillableMetricNewResponse, err error) {
opts = append(r.Options[:], opts...)
path := "billable-metrics/create"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
// Get a billable metric.
func (r *BillableMetricService) Get(ctx context.Context, billableMetricID string, opts ...option.RequestOption) (res *BillableMetricGetResponse, err error) {
opts = append(r.Options[:], opts...)
if billableMetricID == "" {
err = errors.New("missing required billable_metric_id parameter")
return
}
path := fmt.Sprintf("billable-metrics/%s", billableMetricID)
err = requestconfig.ExecuteNewRequest(ctx, http.MethodGet, path, nil, &res, opts...)
return
}
// List all billable metrics.
func (r *BillableMetricService) List(ctx context.Context, query BillableMetricListParams, opts ...option.RequestOption) (res *pagination.CursorPage[BillableMetricListResponse], err error) {
var raw *http.Response
opts = append(r.Options[:], opts...)
opts = append([]option.RequestOption{option.WithResponseInto(&raw)}, opts...)
path := "billable-metrics"
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 billable metrics.
func (r *BillableMetricService) ListAutoPaging(ctx context.Context, query BillableMetricListParams, opts ...option.RequestOption) *pagination.CursorPageAutoPager[BillableMetricListResponse] {
return pagination.NewCursorPageAutoPager(r.List(ctx, query, opts...))
}
// Archive an existing billable metric.
func (r *BillableMetricService) Archive(ctx context.Context, body BillableMetricArchiveParams, opts ...option.RequestOption) (res *BillableMetricArchiveResponse, err error) {
opts = append(r.Options[:], opts...)
path := "billable-metrics/archive"
err = requestconfig.ExecuteNewRequest(ctx, http.MethodPost, path, body, &res, opts...)
return
}
type BillableMetricNewResponse struct {
Data shared.ID `json:"data,required"`
JSON billableMetricNewResponseJSON `json:"-"`
}
// billableMetricNewResponseJSON contains the JSON metadata for the struct
// [BillableMetricNewResponse]
type billableMetricNewResponseJSON struct {
Data apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BillableMetricNewResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r billableMetricNewResponseJSON) RawJSON() string {
return r.raw
}
type BillableMetricGetResponse struct {
Data BillableMetricGetResponseData `json:"data,required"`
JSON billableMetricGetResponseJSON `json:"-"`
}
// billableMetricGetResponseJSON contains the JSON metadata for the struct
// [BillableMetricGetResponse]
type billableMetricGetResponseJSON struct {
Data apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BillableMetricGetResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r billableMetricGetResponseJSON) RawJSON() string {
return r.raw
}
type BillableMetricGetResponseData struct {
// ID of the billable metric
ID string `json:"id,required" format:"uuid"`
// The display name of the billable metric.
Name string `json:"name,required"`
// A key that specifies which property of the event is used to aggregate data. This
// key must be one of the property filter names and is not applicable when the
// aggregation type is 'count'.
AggregationKey string `json:"aggregation_key"`
// Specifies the type of aggregation performed on matching events.
AggregationType BillableMetricGetResponseDataAggregationType `json:"aggregation_type"`
CustomFields map[string]string `json:"custom_fields"`
// An optional filtering rule to match the 'event_type' property of an event.
EventTypeFilter shared.EventTypeFilter `json:"event_type_filter"`
// Property names that are used to group usage costs on an invoice. Each entry
// represents a set of properties used to slice events into distinct buckets.
GroupKeys [][]string `json:"group_keys"`
// A list of filters to match events to this billable metric. Each filter defines a
// rule on an event property. All rules must pass for the event to match the
// billable metric.
PropertyFilters []shared.PropertyFilter `json:"property_filters"`
// The SQL query associated with the billable metric
Sql string `json:"sql"`
JSON billableMetricGetResponseDataJSON `json:"-"`
}
// billableMetricGetResponseDataJSON contains the JSON metadata for the struct
// [BillableMetricGetResponseData]
type billableMetricGetResponseDataJSON struct {
ID apijson.Field
Name apijson.Field
AggregationKey apijson.Field
AggregationType apijson.Field
CustomFields apijson.Field
EventTypeFilter apijson.Field
GroupKeys apijson.Field
PropertyFilters apijson.Field
Sql apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BillableMetricGetResponseData) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r billableMetricGetResponseDataJSON) RawJSON() string {
return r.raw
}
// Specifies the type of aggregation performed on matching events.
type BillableMetricGetResponseDataAggregationType string
const (
BillableMetricGetResponseDataAggregationTypeCount BillableMetricGetResponseDataAggregationType = "COUNT"
BillableMetricGetResponseDataAggregationTypeLatest BillableMetricGetResponseDataAggregationType = "LATEST"
BillableMetricGetResponseDataAggregationTypeMax BillableMetricGetResponseDataAggregationType = "MAX"
BillableMetricGetResponseDataAggregationTypeSum BillableMetricGetResponseDataAggregationType = "SUM"
BillableMetricGetResponseDataAggregationTypeUnique BillableMetricGetResponseDataAggregationType = "UNIQUE"
)
func (r BillableMetricGetResponseDataAggregationType) IsKnown() bool {
switch r {
case BillableMetricGetResponseDataAggregationTypeCount, BillableMetricGetResponseDataAggregationTypeLatest, BillableMetricGetResponseDataAggregationTypeMax, BillableMetricGetResponseDataAggregationTypeSum, BillableMetricGetResponseDataAggregationTypeUnique:
return true
}
return false
}
type BillableMetricListResponse struct {
// ID of the billable metric
ID string `json:"id,required" format:"uuid"`
// The display name of the billable metric.
Name string `json:"name,required"`
// A key that specifies which property of the event is used to aggregate data. This
// key must be one of the property filter names and is not applicable when the
// aggregation type is 'count'.
AggregationKey string `json:"aggregation_key"`
// Specifies the type of aggregation performed on matching events.
AggregationType BillableMetricListResponseAggregationType `json:"aggregation_type"`
CustomFields map[string]string `json:"custom_fields"`
// An optional filtering rule to match the 'event_type' property of an event.
EventTypeFilter shared.EventTypeFilter `json:"event_type_filter"`
// Property names that are used to group usage costs on an invoice. Each entry
// represents a set of properties used to slice events into distinct buckets.
GroupKeys [][]string `json:"group_keys"`
// A list of filters to match events to this billable metric. Each filter defines a
// rule on an event property. All rules must pass for the event to match the
// billable metric.
PropertyFilters []shared.PropertyFilter `json:"property_filters"`
// The SQL query associated with the billable metric
Sql string `json:"sql"`
JSON billableMetricListResponseJSON `json:"-"`
}
// billableMetricListResponseJSON contains the JSON metadata for the struct
// [BillableMetricListResponse]
type billableMetricListResponseJSON struct {
ID apijson.Field
Name apijson.Field
AggregationKey apijson.Field
AggregationType apijson.Field
CustomFields apijson.Field
EventTypeFilter apijson.Field
GroupKeys apijson.Field
PropertyFilters apijson.Field
Sql apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BillableMetricListResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r billableMetricListResponseJSON) RawJSON() string {
return r.raw
}
// Specifies the type of aggregation performed on matching events.
type BillableMetricListResponseAggregationType string
const (
BillableMetricListResponseAggregationTypeCount BillableMetricListResponseAggregationType = "COUNT"
BillableMetricListResponseAggregationTypeLatest BillableMetricListResponseAggregationType = "LATEST"
BillableMetricListResponseAggregationTypeMax BillableMetricListResponseAggregationType = "MAX"
BillableMetricListResponseAggregationTypeSum BillableMetricListResponseAggregationType = "SUM"
BillableMetricListResponseAggregationTypeUnique BillableMetricListResponseAggregationType = "UNIQUE"
)
func (r BillableMetricListResponseAggregationType) IsKnown() bool {
switch r {
case BillableMetricListResponseAggregationTypeCount, BillableMetricListResponseAggregationTypeLatest, BillableMetricListResponseAggregationTypeMax, BillableMetricListResponseAggregationTypeSum, BillableMetricListResponseAggregationTypeUnique:
return true
}
return false
}
type BillableMetricArchiveResponse struct {
Data shared.ID `json:"data,required"`
JSON billableMetricArchiveResponseJSON `json:"-"`
}
// billableMetricArchiveResponseJSON contains the JSON metadata for the struct
// [BillableMetricArchiveResponse]
type billableMetricArchiveResponseJSON struct {
Data apijson.Field
raw string
ExtraFields map[string]apijson.Field
}
func (r *BillableMetricArchiveResponse) UnmarshalJSON(data []byte) (err error) {
return apijson.UnmarshalRoot(data, r)
}
func (r billableMetricArchiveResponseJSON) RawJSON() string {
return r.raw
}
type BillableMetricNewParams struct {
// Specifies the type of aggregation performed on matching events.
AggregationType param.Field[BillableMetricNewParamsAggregationType] `json:"aggregation_type,required"`
// The display name of the billable metric.
Name param.Field[string] `json:"name,required"`
// A key that specifies which property of the event is used to aggregate data. This
// key must be one of the property filter names and is not applicable when the
// aggregation type is 'count'.
AggregationKey param.Field[string] `json:"aggregation_key"`
// Custom fields to attach to the billable metric.
CustomFields param.Field[map[string]string] `json:"custom_fields"`
// An optional filtering rule to match the 'event_type' property of an event.
EventTypeFilter param.Field[shared.EventTypeFilterParam] `json:"event_type_filter"`
// Property names that are used to group usage costs on an invoice. Each entry
// represents a set of properties used to slice events into distinct buckets.
GroupKeys param.Field[[][]string] `json:"group_keys"`
// A list of filters to match events to this billable metric. Each filter defines a
// rule on an event property. All rules must pass for the event to match the
// billable metric.
PropertyFilters param.Field[[]shared.PropertyFilterParam] `json:"property_filters"`
}
func (r BillableMetricNewParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r)
}
// Specifies the type of aggregation performed on matching events.
type BillableMetricNewParamsAggregationType string
const (
BillableMetricNewParamsAggregationTypeCount BillableMetricNewParamsAggregationType = "COUNT"
BillableMetricNewParamsAggregationTypeLatest BillableMetricNewParamsAggregationType = "LATEST"
BillableMetricNewParamsAggregationTypeMax BillableMetricNewParamsAggregationType = "MAX"
BillableMetricNewParamsAggregationTypeSum BillableMetricNewParamsAggregationType = "SUM"
BillableMetricNewParamsAggregationTypeUnique BillableMetricNewParamsAggregationType = "UNIQUE"
)
func (r BillableMetricNewParamsAggregationType) IsKnown() bool {
switch r {
case BillableMetricNewParamsAggregationTypeCount, BillableMetricNewParamsAggregationTypeLatest, BillableMetricNewParamsAggregationTypeMax, BillableMetricNewParamsAggregationTypeSum, BillableMetricNewParamsAggregationTypeUnique:
return true
}
return false
}
type BillableMetricListParams 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 [BillableMetricListParams]'s query parameters as
// `url.Values`.
func (r BillableMetricListParams) URLQuery() (v url.Values) {
return apiquery.MarshalWithSettings(r, apiquery.QuerySettings{
ArrayFormat: apiquery.ArrayQueryFormatComma,
NestedFormat: apiquery.NestedQueryFormatBrackets,
})
}
type BillableMetricArchiveParams struct {
ID shared.IDParam `json:"id,required"`
}
func (r BillableMetricArchiveParams) MarshalJSON() (data []byte, err error) {
return apijson.MarshalRoot(r.ID)
}