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