// 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 }