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
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::models::PaginationParams;
/// Request parameters for listing events.
///
/// This struct combines pagination parameters and optional filters
/// to build a comprehensive request for retrieving event lists.
#[derive(Debug, Clone, Default)]
pub struct ListEventsRequest {
/// Pagination parameters.
pub pagination: PaginationParams,
/// Filter by external subscription ID.
pub external_subscription_id: Option<String>,
/// Filter by billable metric code.
pub code: Option<String>,
/// Requires `external_subscription_id` to be set.
/// Filter events by timestamp after the subscription started at datetime.
pub timestamp_from_started_at: Option<bool>,
/// Filter events by timestamp starting from a specific date (ISO 8601 format).
pub timestamp_from: Option<String>,
/// Filter events by timestamp up to a specific date (ISO 8601 format).
pub timestamp_to: Option<String>,
}
impl ListEventsRequest {
/// Creates a new empty list events request.
///
/// # Returns
/// A new `ListEventsRequest` instance with default pagination and no filters.
pub fn new() -> Self {
Self::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 external subscription ID filter.
///
/// # Arguments
/// * `external_subscription_id` - The external subscription ID to filter by
///
/// # Returns
/// The modified request instance for method chaining.
pub fn with_external_subscription_id(mut self, external_subscription_id: String) -> Self {
self.external_subscription_id = Some(external_subscription_id);
self
}
/// Sets the billable metric code filter.
///
/// # Arguments
/// * `code` - The billable metric code to filter by
///
/// # Returns
/// The modified request instance for method chaining.
pub fn with_code(mut self, code: String) -> Self {
self.code = Some(code);
self
}
/// Sets whether to filter events by timestamp after the subscription started at datetime.
/// Requires `external_subscription_id` to be set.
///
/// # Arguments
/// * `timestamp_from_started_at` - Whether to filter from subscription start
///
/// # Returns
/// The modified request instance for method chaining.
pub fn with_timestamp_from_started_at(mut self, timestamp_from_started_at: bool) -> Self {
self.timestamp_from_started_at = Some(timestamp_from_started_at);
self
}
/// Sets the timestamp from filter (events with timestamp >= this date).
///
/// # Arguments
/// * `timestamp_from` - The start date in ISO 8601 format
///
/// # Returns
/// The modified request instance for method chaining.
pub fn with_timestamp_from(mut self, timestamp_from: String) -> Self {
self.timestamp_from = Some(timestamp_from);
self
}
/// Sets the timestamp to filter (events with timestamp <= this date).
///
/// # Arguments
/// * `timestamp_to` - The end date in ISO 8601 format
///
/// # Returns
/// The modified request instance for method chaining.
pub fn with_timestamp_to(mut self, timestamp_to: String) -> Self {
self.timestamp_to = Some(timestamp_to);
self
}
/// Sets the timestamp range filter.
///
/// # Arguments
/// * `from` - The start date in ISO 8601 format
/// * `to` - The end date in ISO 8601 format
///
/// # Returns
/// The modified request instance for method chaining.
pub fn with_timestamp_range(mut self, from: String, to: String) -> Self {
self.timestamp_from = Some(from);
self.timestamp_to = Some(to);
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();
if let Some(external_subscription_id) = &self.external_subscription_id {
params.push(("external_subscription_id", external_subscription_id.clone()));
}
if let Some(code) = &self.code {
params.push(("code", code.clone()));
}
if let Some(timestamp_from_started_at) = &self.timestamp_from_started_at {
params.push((
"timestamp_from_started_at",
timestamp_from_started_at.to_string(),
));
}
if let Some(timestamp_from) = &self.timestamp_from {
params.push(("timestamp_from", timestamp_from.clone()));
}
if let Some(timestamp_to) = &self.timestamp_to {
params.push(("timestamp_to", timestamp_to.clone()));
}
params
}
}
/// Request to retrieve a specific event by transaction ID.
#[derive(Debug, Clone)]
pub struct GetEventRequest {
/// The transaction ID of the event to retrieve (must be URL encoded)
pub transaction_id: String,
}
impl GetEventRequest {
/// Creates a new get event request.
///
/// # Arguments
/// * `transaction_id` - The transaction ID of the event to retrieve
///
/// # Returns
/// A new `GetEventRequest` instance
pub fn new(transaction_id: String) -> Self {
Self { transaction_id }
}
}
/// Input data for creating a usage event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateEventInput {
/// Unique identifier for this event (used for idempotency and retrieval)
pub transaction_id: String,
/// External customer ID - required if external_subscription_id is not provided
#[serde(skip_serializing_if = "Option::is_none")]
pub external_customer_id: Option<String>,
/// External subscription ID - required if external_customer_id is not provided
#[serde(skip_serializing_if = "Option::is_none")]
pub external_subscription_id: Option<String>,
/// Billable metric code
pub code: String,
/// Event timestamp (Unix timestamp in seconds)
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<i64>,
/// Custom properties/metadata for the event
#[serde(skip_serializing_if = "Option::is_none")]
pub properties: Option<Value>,
/// Precise total amount in cents
#[serde(skip_serializing_if = "Option::is_none")]
pub precise_total_amount_cents: Option<i64>,
}
impl CreateEventInput {
/// Creates a new event input for a customer.
///
/// # Arguments
/// * `transaction_id` - Unique identifier for the event
/// * `external_customer_id` - The external ID of the customer
/// * `code` - The billable metric code
///
/// # Returns
/// A new `CreateEventInput` instance
pub fn for_customer(
transaction_id: String,
external_customer_id: String,
code: String,
) -> Self {
Self {
transaction_id,
external_customer_id: Some(external_customer_id),
external_subscription_id: None,
code,
timestamp: None,
properties: None,
precise_total_amount_cents: None,
}
}
/// Creates a new event input for a subscription.
///
/// # Arguments
/// * `transaction_id` - Unique identifier for the event
/// * `external_subscription_id` - The external ID of the subscription
/// * `code` - The billable metric code
///
/// # Returns
/// A new `CreateEventInput` instance
pub fn for_subscription(
transaction_id: String,
external_subscription_id: String,
code: String,
) -> Self {
Self {
transaction_id,
external_customer_id: None,
external_subscription_id: Some(external_subscription_id),
code,
timestamp: None,
properties: None,
precise_total_amount_cents: None,
}
}
/// Sets the timestamp for the event.
///
/// # Arguments
/// * `timestamp` - Unix timestamp in seconds
///
/// # Returns
/// The modified input instance for method chaining
pub fn with_timestamp(mut self, timestamp: i64) -> Self {
self.timestamp = Some(timestamp);
self
}
/// Sets custom properties for the event.
///
/// # Arguments
/// * `properties` - JSON object containing event properties
///
/// # Returns
/// The modified input instance for method chaining
pub fn with_properties(mut self, properties: Value) -> Self {
self.properties = Some(properties);
self
}
/// Sets the precise total amount in cents.
///
/// # Arguments
/// * `amount` - The precise amount in cents
///
/// # Returns
/// The modified input instance for method chaining
pub fn with_precise_total_amount_cents(mut self, amount: i64) -> Self {
self.precise_total_amount_cents = Some(amount);
self
}
}
/// Request wrapper for creating a usage event.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CreateEventRequest {
/// The event input data
pub event: CreateEventInput,
}
impl CreateEventRequest {
/// Creates a new create event request.
///
/// # Arguments
/// * `event` - The event input data
///
/// # Returns
/// A new `CreateEventRequest` instance
pub fn new(event: CreateEventInput) -> Self {
Self { event }
}
}