Skip to content

Commit

Permalink
feat(felixible aggregation): Expose expression in API (#213)
Browse files Browse the repository at this point in the history
* feat(felixible aggregation): Expose expression in API

* feat(felixible aggregation): Add documentation for expression evaluation endpoint
  • Loading branch information
vincent-pochet authored Nov 4, 2024
1 parent 441c71d commit 03b758b
Showing 1 changed file with 41 additions and 0 deletions.
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
}

0 comments on commit 03b758b

Please sign in to comment.