File size: 4,011 Bytes
04f1444
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Code generated by @openmeter/typespec-go. DO NOT EDIT.

package openmeter

import (
	"context"
	"fmt"
	"iter"
	"net/http"
	"net/url"
)

type BillingService struct {
	client *Client
}

type ProfileListParams struct {
	Page *PageParams
}

func (p ProfileListParams) values() url.Values {
	q := url.Values{}

	addPageParams(q, p.Page)

	return q
}

// List billing profiles.
func (s *BillingService) ListProfiles(ctx context.Context, params ProfileListParams) (*ProfilePagePaginatedResponse, error) {
	path := "/openmeter/profiles"

	req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, params.values(), nil, "", "application/json")
	if err != nil {
		return nil, err
	}

	var out ProfilePagePaginatedResponse
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// ListProfilesAll returns an iterator over all Profile results, fetching pages of ListProfiles transparently. Iteration stops at the first error, which is yielded as the second value.
func (s *BillingService) ListProfilesAll(ctx context.Context, params ProfileListParams) iter.Seq2[Profile, error] {
	return paginate(params.Page, func(page, size int) ([]Profile, int, error) {
		pageParams := params
		pageParams.Page = &PageParams{Size: Int(size), Number: Int(page)}

		resp, err := s.ListProfiles(ctx, pageParams)
		if err != nil {
			return nil, 0, err
		}

		return resp.Data, resp.Meta.Page.Total, nil
	})
}

// Create a new billing profile.
//
// Billing profiles contain the settings for billing and controls invoice
// generation. An organization can have multiple billing profiles defined. A
// billing profile is linked to a specific app. This association is established
// during the billing profile's creation and remains immutable.
func (s *BillingService) CreateProfile(ctx context.Context, request CreateBillingProfileRequest) (*Profile, error) {
	path := "/openmeter/profiles"

	req, err := s.client.newRequestWithContentType(ctx, http.MethodPost, path, nil, request, "application/json", "application/json")
	if err != nil {
		return nil, err
	}

	var out Profile
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// Get a billing profile.
func (s *BillingService) GetProfile(ctx context.Context, id string) (*Profile, error) {
	if id == "" {
		return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "id", ErrEmptyID)
	}

	path := "/openmeter/profiles/{id}"

	path = replacePathParam(path, "id", id)

	req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, nil, nil, "", "application/json")
	if err != nil {
		return nil, err
	}

	var out Profile
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// Update a billing profile.
func (s *BillingService) UpdateProfile(ctx context.Context, id string, request UpsertBillingProfileRequest) (*Profile, error) {
	if id == "" {
		return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "id", ErrEmptyID)
	}

	path := "/openmeter/profiles/{id}"

	path = replacePathParam(path, "id", id)

	req, err := s.client.newRequestWithContentType(ctx, http.MethodPut, path, nil, request, "application/json", "application/json")
	if err != nil {
		return nil, err
	}

	var out Profile
	if err := s.client.doJSON(req, &out); err != nil {
		return nil, err
	}

	return &out, nil
}

// Delete a billing profile.
//
// Only such billing profiles can be deleted that are:
//
// - not the default profile
// - not pinned to any customer using customer overrides
// - only have finalized invoices
func (s *BillingService) DeleteProfile(ctx context.Context, id string) error {
	if id == "" {
		return fmt.Errorf("openmeter: %s must not be empty: %w", "id", ErrEmptyID)
	}

	path := "/openmeter/profiles/{id}"

	path = replacePathParam(path, "id", id)

	req, err := s.client.newRequestWithContentType(ctx, http.MethodDelete, path, nil, nil, "", "")
	if err != nil {
		return err
	}

	_, err = s.client.doRaw(req)
	return err
}