Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(felixible aggregation): Expose expression in API #213

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions billable_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type BillableMetricInput struct {
Description string `json:"description,omitempty"`
AggregationType AggregationType `json:"aggregation_type,omitempty"`
Recurring bool `json:"recurring,omitempty"`
Expression string `json:"expression,omitempty"`
FieldName string `json:"field_name"`
WeightedInterval WeightedInterval `json:"weighted_interval,omitempty"`
Filters []BillableMetricFilter `json:"filters,omitempty"`
Expand Down Expand Up @@ -68,6 +69,7 @@ type BillableMetric struct {
Description string `json:"description,omitempty"`
Recurring bool `json:"recurring,omitempty"`
AggregationType AggregationType `json:"aggregation_type,omitempty"`
Expression string `json:"expression,omitempty"`
FieldName string `json:"field_name"`
CreatedAt time.Time `json:"created_at,omitempty"`
WeightedInterval *WeightedInterval `json:"weighted_interval,omitempty"`
Expand All @@ -77,6 +79,25 @@ type BillableMetric struct {
PlansCount int `json:"plans_count,omitempty"`
}

type BillableMetricEveluateExpressionEvent struct {
Code string `json:"code,omitempty"`
Timestamp string `json:"timestamp,omitempty"`
Properties map[string]interface{} `json:"properties,omitempty"`
}

type BillableMetricEvaluateExpressionInput struct {
Expression string `json:"expression"`
Event BillableMetricEveluateExpressionEvent `json:"event"`
}

type BillableMetricEvaluateExpressionResultValue struct {
Value string `json:"value,omitempty"`
}

type BillableMetricEvaluateExpressionResult struct {
ExpressionResult BillableMetricEvaluateExpressionResultValue `json:"expression_result,omitempty"`
}

func (c *Client) BillableMetric() *BillableMetricRequest {
return &BillableMetricRequest{
client: c,
Expand Down Expand Up @@ -198,3 +219,23 @@ func (bmr *BillableMetricRequest) Delete(ctx context.Context, billableMetricCode

return billableMetricResult.BillableMetric, nil
}

func (bmr *BillableMetricRequest) EvaluateExpression(ctx context.Context, evaluateExpressingInput *BillableMetricEvaluateExpressionInput) (*BillableMetricEvaluateExpressionResultValue, *Error) {
clientRequest := &ClientRequest{
Path: "billable_metrics/evaluate_expression",
Result: &BillableMetricEvaluateExpressionResult{},
Body: evaluateExpressingInput,
}

result, err := bmr.client.Post(ctx, clientRequest)
if err != nil {
return nil, err
}

evaluateExpressionResult, ok := result.(*BillableMetricEvaluateExpressionResult)
if !ok {
return nil, &ErrorTypeAssert
}

return &evaluateExpressionResult.ExpressionResult, nil
}
Loading