lago-types 0.1.24

Types definitions for Lago API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
use crate::models::PaginationParams;
use serde::{Deserialize, Serialize};

use crate::filters::{billable_metric::BillableMetricFilter, common::ListFilters};
use crate::models::{
    BillableMetricAggregationType, BillableMetricFilter as BillableMetricFilterModel,
    BillableMetricRoundingFunction, BillableMetricWeightedInterval,
};

/// Request parameters for listing billable metrics.
///
/// This struct combines pagination parameters and billable metric-specific filters
/// to build a comprehensive request for retrieving billable metric lists.
#[derive(Debug, Clone)]
pub struct ListBillableMetricsRequest {
    pub pagination: PaginationParams,
    pub filters: BillableMetricFilter,
}

impl ListBillableMetricsRequest {
    /// Creates a new empty list billable metrics request.
    ///
    /// # Returns
    /// A new `ListBillableMetricsRequest` instance with default pagination and no filters.
    pub fn new() -> Self {
        Self {
            pagination: PaginationParams::default(),
            filters: BillableMetricFilter::default(),
        }
    }

    /// Sets the pagination parameters for the request.
    ///
    /// # Arguments
    /// * `pagination` - The pagination parameters to use
    ///
    /// # Returns
    /// The modified request instance for method chaining.
    pub fn with_pagination(mut self, pagination: PaginationParams) -> Self {
        self.pagination = pagination;
        self
    }

    /// Sets the billable metric filters for the request.
    ///
    /// # Arguments
    /// * `filters` - The billable metric filters to apply
    ///
    /// # Returns
    /// The modified request instance for method chaining.
    pub fn with_filters(mut self, filters: BillableMetricFilter) -> Self {
        self.filters = filters;
        self
    }

    /// Converts the request parameters into HTTP query parameters.
    ///
    /// # Returns
    /// A vector of query parameter tuples containing both pagination and filter criteria.
    pub fn to_query_params(&self) -> Vec<(&str, String)> {
        let mut params = self.pagination.to_query_params();
        params.extend(self.filters.to_query_params());
        params
    }
}

impl Default for ListBillableMetricsRequest {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug, Clone)]
pub struct GetBillableMetricRequest {
    pub code: String,
}

impl GetBillableMetricRequest {
    /// Creates a new get billable metric request.
    ///
    /// # Arguments
    /// * `code` - The unique code of the billable metric to retrieve
    ///
    /// # Returns
    /// A new `GetBillableMetricRequest` instance
    pub fn new(code: String) -> Self {
        Self { code }
    }
}

/// Input parameters for creating a billable metric.
///
/// This struct contains all the necessary information to create a new billable metric
/// in the Lago billing system.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateBillableMetricInput {
    pub name: String,
    pub code: String,
    pub description: Option<String>,
    pub aggregation_type: BillableMetricAggregationType,
    pub recurring: Option<bool>,
    pub rounding_function: Option<BillableMetricRoundingFunction>,
    pub rounding_precision: Option<i32>,
    pub expression: Option<String>,
    pub field_name: Option<String>,
    pub weighted_interval: Option<BillableMetricWeightedInterval>,
    pub filters: Option<Vec<BillableMetricFilterModel>>,
}

impl CreateBillableMetricInput {
    /// Creates a new billable metric input with required fields.
    ///
    /// # Arguments
    /// * `name` - The name of the billable metric
    /// * `code` - The unique code for the billable metric
    /// * `aggregation_type` - The aggregation method to use
    ///
    /// # Returns
    /// A new `CreateBillableMetricInput` instance
    pub fn new(
        name: String,
        code: String,
        aggregation_type: BillableMetricAggregationType,
    ) -> Self {
        Self {
            name,
            code,
            aggregation_type,
            description: None,
            recurring: None,
            rounding_function: None,
            rounding_precision: None,
            expression: None,
            field_name: None,
            weighted_interval: None,
            filters: None,
        }
    }

    /// Sets the description for the billable metric.
    ///
    /// # Arguments
    /// * `description` - The description of the billable metric
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    /// Sets whether the billable metric is recurring.
    ///
    /// # Arguments
    /// * `recurring` - Whether the metric persists across billing periods
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_recurring(mut self, recurring: bool) -> Self {
        self.recurring = Some(recurring);
        self
    }

    /// Sets the rounding function for the billable metric.
    ///
    /// # Arguments
    /// * `rounding_function` - The rounding function to apply
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_rounding_function(
        mut self,
        rounding_function: BillableMetricRoundingFunction,
    ) -> Self {
        self.rounding_function = Some(rounding_function);
        self
    }

    /// Sets the rounding precision for the billable metric.
    ///
    /// # Arguments
    /// * `precision` - The number of decimal places for rounding
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_rounding_precision(mut self, precision: i32) -> Self {
        self.rounding_precision = Some(precision);
        self
    }

    /// Sets the expression for the billable metric.
    ///
    /// # Arguments
    /// * `expression` - The expression used to calculate event units
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_expression(mut self, expression: String) -> Self {
        self.expression = Some(expression);
        self
    }

    /// Sets the field name for the billable metric.
    ///
    /// # Arguments
    /// * `field_name` - The property to aggregate on
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_field_name(mut self, field_name: String) -> Self {
        self.field_name = Some(field_name);
        self
    }

    /// Sets the weighted interval for the billable metric.
    ///
    /// # Arguments
    /// * `interval` - The interval for weighted sum aggregation
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_weighted_interval(mut self, interval: BillableMetricWeightedInterval) -> Self {
        self.weighted_interval = Some(interval);
        self
    }

    /// Sets the filters for the billable metric.
    ///
    /// # Arguments
    /// * `filters` - The filters for differentiated pricing
    ///
    /// # Returns
    /// The modified input instance for method chaining.
    pub fn with_filters(mut self, filters: Vec<BillableMetricFilterModel>) -> Self {
        self.filters = Some(filters);
        self
    }
}

/// Request parameters for creating a billable metric.
///
/// This struct wraps the billable metric input in the expected API format.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateBillableMetricRequest {
    pub billable_metric: CreateBillableMetricInput,
}

impl CreateBillableMetricRequest {
    /// Creates a new create billable metric request.
    ///
    /// # Arguments
    /// * `billable_metric` - The billable metric input data
    ///
    /// # Returns
    /// A new `CreateBillableMetricRequest` instance
    pub fn new(billable_metric: CreateBillableMetricInput) -> Self {
        Self { billable_metric }
    }
}

/// Input parameters for updating a billable metric.
///
/// All fields are optional — only the fields that are set will be sent in the
/// request body, allowing partial updates without clearing unset fields.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UpdateBillableMetricInput {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub code: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub aggregation_type: Option<BillableMetricAggregationType>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recurring: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rounding_function: Option<BillableMetricRoundingFunction>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rounding_precision: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expression: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub field_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub weighted_interval: Option<BillableMetricWeightedInterval>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub filters: Option<Vec<BillableMetricFilterModel>>,
}

impl UpdateBillableMetricInput {
    /// Creates a new empty update billable metric input.
    pub fn new() -> Self {
        Self {
            name: None,
            code: None,
            description: None,
            aggregation_type: None,
            recurring: None,
            rounding_function: None,
            rounding_precision: None,
            expression: None,
            field_name: None,
            weighted_interval: None,
            filters: None,
        }
    }

    /// Sets the name.
    pub fn with_name(mut self, name: String) -> Self {
        self.name = Some(name);
        self
    }

    /// Sets the code.
    pub fn with_code(mut self, code: String) -> Self {
        self.code = Some(code);
        self
    }

    /// Sets the description.
    pub fn with_description(mut self, description: String) -> Self {
        self.description = Some(description);
        self
    }

    /// Sets the aggregation type.
    pub fn with_aggregation_type(
        mut self,
        aggregation_type: BillableMetricAggregationType,
    ) -> Self {
        self.aggregation_type = Some(aggregation_type);
        self
    }

    /// Sets whether the billable metric is recurring.
    pub fn with_recurring(mut self, recurring: bool) -> Self {
        self.recurring = Some(recurring);
        self
    }

    /// Sets the rounding function.
    pub fn with_rounding_function(
        mut self,
        rounding_function: BillableMetricRoundingFunction,
    ) -> Self {
        self.rounding_function = Some(rounding_function);
        self
    }

    /// Sets the rounding precision.
    pub fn with_rounding_precision(mut self, precision: i32) -> Self {
        self.rounding_precision = Some(precision);
        self
    }

    /// Sets the expression.
    pub fn with_expression(mut self, expression: String) -> Self {
        self.expression = Some(expression);
        self
    }

    /// Sets the field name.
    pub fn with_field_name(mut self, field_name: String) -> Self {
        self.field_name = Some(field_name);
        self
    }

    /// Sets the weighted interval.
    pub fn with_weighted_interval(mut self, interval: BillableMetricWeightedInterval) -> Self {
        self.weighted_interval = Some(interval);
        self
    }

    /// Sets the filters.
    pub fn with_filters(mut self, filters: Vec<BillableMetricFilterModel>) -> Self {
        self.filters = Some(filters);
        self
    }
}

impl Default for UpdateBillableMetricInput {
    fn default() -> Self {
        Self::new()
    }
}

/// Request parameters for updating a billable metric.
///
/// The `code` is used only to build the URL path and is never serialized into
/// the JSON body (enforced via `#[serde(skip)]`).
#[derive(Debug, Clone, Serialize)]
pub struct UpdateBillableMetricRequest {
    #[serde(skip)]
    pub code: String,
    pub billable_metric: UpdateBillableMetricInput,
}

impl UpdateBillableMetricRequest {
    /// Creates a new update billable metric request.
    ///
    /// # Arguments
    /// * `code` - The code of the billable metric to update
    /// * `input` - The billable metric update data
    pub fn new(code: String, input: UpdateBillableMetricInput) -> Self {
        Self {
            code,
            billable_metric: input,
        }
    }
}