From a4b443dd17ddf1d88bddd259bc7d9e6c14d12b8b Mon Sep 17 00:00:00 2001 From: Vincent Pochet Date: Thu, 24 Oct 2024 14:24:23 +0200 Subject: [PATCH 1/2] feat(felixible aggregation): Expose expression in API --- billable_metric.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/billable_metric.go b/billable_metric.go index 2e8af68..943e6d9 100644 --- a/billable_metric.go +++ b/billable_metric.go @@ -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"` @@ -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"` From 4e2219614a79335fc15bd8d608761e1c7bfae082 Mon Sep 17 00:00:00 2001 From: Vincent Pochet Date: Thu, 24 Oct 2024 17:29:56 +0200 Subject: [PATCH 2/2] feat(felixible aggregation): Add documentation for expression evaluation endpoint --- billable_metric.go | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/billable_metric.go b/billable_metric.go index 943e6d9..7fbc0ab 100644 --- a/billable_metric.go +++ b/billable_metric.go @@ -79,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, @@ -200,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 +}