| |
|
|
| package openmeter |
|
|
| import ( |
| "context" |
| "fmt" |
| "iter" |
| "net/http" |
| "net/url" |
| ) |
|
|
| type FeaturesService struct { |
| client *Client |
| } |
|
|
| type FeatureFilter struct { |
| MeterID *StringExactFilter |
| Key *StringFilter |
| Name *StringFilter |
| } |
|
|
| type FeatureListParams struct { |
| Page *PageParams |
| Sort *Sort |
| Filter *FeatureFilter |
| } |
|
|
| func (p FeatureListParams) values() url.Values { |
| q := url.Values{} |
|
|
| addPageParams(q, p.Page) |
|
|
| addSort(q, "sort", p.Sort) |
|
|
| if p.Filter != nil { |
| addStringExactFilter(q, "filter[meter_id]", p.Filter.MeterID) |
| addStringFilter(q, "filter[key]", p.Filter.Key) |
| addStringFilter(q, "filter[name]", p.Filter.Name) |
| } |
|
|
| return q |
| } |
|
|
| |
| func (s *FeaturesService) List(ctx context.Context, params FeatureListParams) (*FeaturePagePaginatedResponse, error) { |
| path := "/openmeter/features" |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, params.values(), nil, "", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out FeaturePagePaginatedResponse |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| |
| func (s *FeaturesService) ListAll(ctx context.Context, params FeatureListParams) iter.Seq2[Feature, error] { |
| return paginate(params.Page, func(page, size int) ([]Feature, int, error) { |
| pageParams := params |
| pageParams.Page = &PageParams{Size: Int(size), Number: Int(page)} |
|
|
| resp, err := s.List(ctx, pageParams) |
| if err != nil { |
| return nil, 0, err |
| } |
|
|
| return resp.Data, resp.Meta.Page.Total, nil |
| }) |
| } |
|
|
| |
| func (s *FeaturesService) Create(ctx context.Context, request CreateFeatureRequest) (*Feature, error) { |
| path := "/openmeter/features" |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodPost, path, nil, request, "application/json", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out Feature |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| |
| func (s *FeaturesService) Get(ctx context.Context, featureID string) (*Feature, error) { |
| if featureID == "" { |
| return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "featureID", ErrEmptyID) |
| } |
|
|
| path := "/openmeter/features/{featureId}" |
|
|
| path = replacePathParam(path, "featureId", featureID) |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodGet, path, nil, nil, "", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out Feature |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| |
| func (s *FeaturesService) Update(ctx context.Context, featureID string, request UpdateFeatureRequest) (*Feature, error) { |
| if featureID == "" { |
| return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "featureID", ErrEmptyID) |
| } |
|
|
| path := "/openmeter/features/{featureId}" |
|
|
| path = replacePathParam(path, "featureId", featureID) |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodPatch, path, nil, request, "application/json", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out Feature |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|
| |
| func (s *FeaturesService) Delete(ctx context.Context, featureID string) error { |
| if featureID == "" { |
| return fmt.Errorf("openmeter: %s must not be empty: %w", "featureID", ErrEmptyID) |
| } |
|
|
| path := "/openmeter/features/{featureId}" |
|
|
| path = replacePathParam(path, "featureId", featureID) |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodDelete, path, nil, nil, "", "") |
| if err != nil { |
| return err |
| } |
|
|
| _, err = s.client.doRaw(req) |
| return err |
| } |
|
|
| |
| func (s *FeaturesService) QueryCost(ctx context.Context, featureID string, request *MeterQueryRequest) (*FeatureCostQueryResult, error) { |
| if featureID == "" { |
| return nil, fmt.Errorf("openmeter: %s must not be empty: %w", "featureID", ErrEmptyID) |
| } |
|
|
| path := "/openmeter/features/{featureId}/cost/query" |
|
|
| path = replacePathParam(path, "featureId", featureID) |
|
|
| req, err := s.client.newRequestWithContentType(ctx, http.MethodPost, path, nil, optionalBody(request), "application/json", "application/json") |
| if err != nil { |
| return nil, err |
| } |
|
|
| var out FeatureCostQueryResult |
| if err := s.client.doJSON(req, &out); err != nil { |
| return nil, err |
| } |
|
|
| return &out, nil |
| } |
|
|