File size: 8,601 Bytes
1477a90
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Code generated by @openmeter/typespec-typescript. DO NOT EDIT.

import { type Client, http } from '../core.js'
import { type Result, type RequestOptions } from '../lib/types.js'
import { request } from '../lib/request.js'
import { toURLSearchParams, encodeSort } from '../lib/encodings.js'
import {
  toWire,
  toPathWire,
  fromWire,
  assertValid,
  toSnakeCase,
} from '../lib/wire.js'
import * as schemas from '../models/schemas.js'
import type {
  CreateMeterRequest,
  CreateMeterResponse,
  GetMeterRequest,
  GetMeterResponse,
  ListMetersRequest,
  ListMetersResponse,
  UpdateMeterRequest,
  UpdateMeterResponse,
  DeleteMeterRequest,
  DeleteMeterResponse,
  QueryMeterRequest,
  QueryMeterResponse,
  QueryMeterCsvRequest,
  QueryMeterCsvResponse,
} from '../models/operations/meters.js'

/**
 * Create meter
 *
 * Create a meter.
 *
 * POST /openmeter/meters
 */
export function createMeter(
  client: Client,
  req: CreateMeterRequest,
  options?: RequestOptions,
): Promise<Result<CreateMeterResponse>> {
  return request(() => {
    const body = toWire(req, schemas.createMeterBody)
    if (client._options.validate) {
      assertValid(schemas.createMeterBodyWire, body)
    }
    return http(client)
      .post('openmeter/meters', { ...options, json: body })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.createMeterResponseWire, data)
        }
        return fromWire(data, schemas.createMeterResponse)
      })
  })
}

/**
 * Get meter
 *
 * Get a meter by ID.
 *
 * GET /openmeter/meters/{meterId}
 */
export function getMeter(
  client: Client,
  req: GetMeterRequest,
  options?: RequestOptions,
): Promise<Result<GetMeterResponse>> {
  return request(() => {
    const pathParamsInput = {
      meterId: req.meterId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.getMeterPathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.getMeterPathParamsWire, pathParams)
    }
    const path = `openmeter/meters/${(() => {
      if (pathParams.meterId === undefined) {
        throw new Error('missing path parameter: meterId')
      }
      return encodeURIComponent(String(pathParams.meterId))
    })()}`
    return http(client)
      .get(path, options)
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.getMeterResponseWire, data)
        }
        return fromWire(data, schemas.getMeterResponse)
      })
  })
}

/**
 * List meters
 *
 * List meters.
 *
 * GET /openmeter/meters
 */
export function listMeters(
  client: Client,
  req: ListMetersRequest = {},
  options?: RequestOptions,
): Promise<Result<ListMetersResponse>> {
  return request(() => {
    if (client._options.validate && req.sort !== undefined) {
      assertValid(schemas.listMetersQueryParams.shape.sort, req.sort)
    }
    const query = toWire(
      {
        page: req.page,
        sort: encodeSort(req.sort, toSnakeCase),
        filter: req.filter,
      },
      schemas.listMetersQueryParams,
    )
    if (client._options.validate) {
      assertValid(schemas.listMetersQueryParamsWire, query)
    }
    const searchParams = toURLSearchParams(query)
    return http(client)
      .get('openmeter/meters', { ...options, searchParams })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.listMetersResponseWire, data)
        }
        return fromWire(data, schemas.listMetersResponse)
      })
  })
}

/**
 * Update meter
 *
 * Update a meter.
 *
 * PUT /openmeter/meters/{meterId}
 */
export function updateMeter(
  client: Client,
  req: UpdateMeterRequest,
  options?: RequestOptions,
): Promise<Result<UpdateMeterResponse>> {
  return request(() => {
    const pathParamsInput = {
      meterId: req.meterId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.updateMeterPathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.updateMeterPathParamsWire, pathParams)
    }
    const path = `openmeter/meters/${(() => {
      if (pathParams.meterId === undefined) {
        throw new Error('missing path parameter: meterId')
      }
      return encodeURIComponent(String(pathParams.meterId))
    })()}`
    const body = toWire(req.body, schemas.updateMeterBody)
    if (client._options.validate) {
      assertValid(schemas.updateMeterBodyWire, body)
    }
    return http(client)
      .put(path, { ...options, json: body })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.updateMeterResponseWire, data)
        }
        return fromWire(data, schemas.updateMeterResponse)
      })
  })
}

/**
 * Delete meter
 *
 * Delete a meter.
 *
 * DELETE /openmeter/meters/{meterId}
 */
export function deleteMeter(
  client: Client,
  req: DeleteMeterRequest,
  options?: RequestOptions,
): Promise<Result<DeleteMeterResponse>> {
  return request(async () => {
    const pathParamsInput = {
      meterId: req.meterId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.deleteMeterPathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.deleteMeterPathParamsWire, pathParams)
    }
    const path = `openmeter/meters/${(() => {
      if (pathParams.meterId === undefined) {
        throw new Error('missing path parameter: meterId')
      }
      return encodeURIComponent(String(pathParams.meterId))
    })()}`
    await http(client).delete(path, options)
  })
}

/**
 * Query meter
 *
 * Query a meter for usage.
 *
 * Set `Accept: application/json` (the default) to get a structured JSON response.
 * Set `Accept: text/csv` to download the same data as a CSV file suitable for
 * spreadsheets. The CSV columns, in order, are:
 *
 * `from, to, [subject,] [customer_id, customer_key, customer_name,] <dimensions...>, value`
 *
 * The `subject` column is emitted only when `subject` is in the query's
 * `group_by_dimensions`. The three `customer_*` columns are emitted together only
 * when `customer_id` is in the query's `group_by_dimensions`.
 *
 * POST /openmeter/meters/{meterId}/query
 */
export function queryMeter(
  client: Client,
  req: QueryMeterRequest,
  options?: RequestOptions,
): Promise<Result<QueryMeterResponse>> {
  return request(() => {
    const pathParamsInput = {
      meterId: req.meterId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.queryMeterPathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.queryMeterPathParamsWire, pathParams)
    }
    const path = `openmeter/meters/${(() => {
      if (pathParams.meterId === undefined) {
        throw new Error('missing path parameter: meterId')
      }
      return encodeURIComponent(String(pathParams.meterId))
    })()}/query`
    const body = toWire(req.body, schemas.queryMeterBody)
    if (client._options.validate) {
      assertValid(schemas.queryMeterBodyWire, body)
    }
    return http(client)
      .post(path, { ...options, json: body })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.queryMeterResponseWire, data)
        }
        return fromWire(data, schemas.queryMeterResponse)
      })
  })
}

/** POST /openmeter/meters/{meterId}/query */
export function queryMeterCsv(
  client: Client,
  req: QueryMeterCsvRequest,
  options?: RequestOptions,
): Promise<Result<QueryMeterCsvResponse>> {
  const headers = new Headers(options?.headers as HeadersInit | undefined)
  headers.set('accept', 'text/csv')
  return request(() => {
    const pathParamsInput = {
      meterId: req.meterId,
    }
    const pathParams = client._options.validate
      ? toPathWire(pathParamsInput, schemas.queryMeterCsvPathParams)
      : pathParamsInput
    if (client._options.validate) {
      assertValid(schemas.queryMeterCsvPathParamsWire, pathParams)
    }
    const path = `openmeter/meters/${(() => {
      if (pathParams.meterId === undefined) {
        throw new Error('missing path parameter: meterId')
      }
      return encodeURIComponent(String(pathParams.meterId))
    })()}/query`
    const body = toWire(req.body, schemas.queryMeterCsvBody)
    if (client._options.validate) {
      assertValid(schemas.queryMeterCsvBodyWire, body)
    }
    return http(client)
      .post(path, { ...options, json: body, headers })
      .text()
  })
}