File size: 2,300 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
// 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, fromWire, assertValid, toSnakeCase } from '../lib/wire.js'
import * as schemas from '../models/schemas.js'
import type {
  ListMeteringEventsRequest,
  ListMeteringEventsResponse,
  IngestMeteringEventsRequest,
  IngestMeteringEventsResponse,
} from '../models/operations/events.js'

/**
 * List metering events
 *
 * List ingested events.
 *
 * GET /openmeter/events
 */
export function listMeteringEvents(
  client: Client,
  req: ListMeteringEventsRequest = {},
  options?: RequestOptions,
): Promise<Result<ListMeteringEventsResponse>> {
  return request(() => {
    if (client._options.validate && req.sort !== undefined) {
      assertValid(schemas.listMeteringEventsQueryParams.shape.sort, req.sort)
    }
    const query = toWire(
      {
        page: req.page,
        filter: req.filter,
        sort: encodeSort(req.sort, toSnakeCase),
      },
      schemas.listMeteringEventsQueryParams,
    )
    if (client._options.validate) {
      assertValid(schemas.listMeteringEventsQueryParamsWire, query)
    }
    const searchParams = toURLSearchParams(query)
    return http(client)
      .get('openmeter/events', { ...options, searchParams })
      .json()
      .then((data) => {
        if (client._options.validate) {
          assertValid(schemas.listMeteringEventsResponseWire, data)
        }
        return fromWire(data, schemas.listMeteringEventsResponse)
      })
  })
}

/**
 * Ingest metering events
 *
 * Ingests an event or batch of events following the CloudEvents specification.
 *
 * POST /openmeter/events
 */
export function ingestMeteringEvents(
  client: Client,
  req: IngestMeteringEventsRequest,
  options?: RequestOptions,
): Promise<Result<IngestMeteringEventsResponse>> {
  return request(async () => {
    const body = toWire(req, schemas.ingestMeteringEventsBody)
    if (client._options.validate) {
      assertValid(schemas.ingestMeteringEventsBodyWire, body)
    }
    await http(client).post('openmeter/events', { ...options, json: body })
  })
}