openai-ergonomic 0.5.2

Ergonomic Rust wrapper for OpenAI 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
//! Usage API builders.
//!
//! Provides high-level builders for querying usage and cost data from the `OpenAI` API.
//! Supports filtering by date range, aggregation buckets, projects, users, API keys, and models.

/// Bucket width for aggregating usage data.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BucketWidth {
    /// Aggregate data by day.
    Day,
    /// Aggregate data by hour.
    Hour,
}

impl BucketWidth {
    /// Convert to API string representation.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Day => "1d",
            Self::Hour => "1h",
        }
    }
}

impl std::fmt::Display for BucketWidth {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Group by field for usage aggregation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum GroupBy {
    /// Group by project ID.
    ProjectId,
    /// Group by user ID.
    UserId,
    /// Group by API key ID.
    ApiKeyId,
    /// Group by model.
    Model,
}

impl GroupBy {
    /// Convert to API string representation.
    #[must_use]
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::ProjectId => "project_id",
            Self::UserId => "user_id",
            Self::ApiKeyId => "api_key_id",
            Self::Model => "model",
        }
    }
}

impl std::fmt::Display for GroupBy {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

/// Builder for querying usage data from the `OpenAI` API.
///
/// # Examples
///
/// ```rust
/// use openai_ergonomic::builders::usage::{UsageBuilder, BucketWidth};
///
/// let builder = UsageBuilder::new(1704067200, None) // Start time (Unix timestamp)
///     .bucket_width(BucketWidth::Day)
///     .limit(100);
/// ```
#[derive(Debug, Clone)]
pub struct UsageBuilder {
    start_time: i32,
    end_time: Option<i32>,
    bucket_width: Option<BucketWidth>,
    project_ids: Vec<String>,
    user_ids: Vec<String>,
    api_key_ids: Vec<String>,
    models: Vec<String>,
    group_by: Vec<GroupBy>,
    limit: Option<i32>,
    page: Option<String>,
}

impl UsageBuilder {
    /// Create a new usage builder with the specified start time.
    ///
    /// # Arguments
    ///
    /// * `start_time` - Unix timestamp (in seconds) for the start of the query range
    /// * `end_time` - Optional Unix timestamp (in seconds) for the end of the query range
    #[must_use]
    pub fn new(start_time: i32, end_time: Option<i32>) -> Self {
        Self {
            start_time,
            end_time,
            bucket_width: None,
            project_ids: Vec::new(),
            user_ids: Vec::new(),
            api_key_ids: Vec::new(),
            models: Vec::new(),
            group_by: Vec::new(),
            limit: None,
            page: None,
        }
    }

    /// Set the bucket width for aggregation.
    #[must_use]
    pub fn bucket_width(mut self, width: BucketWidth) -> Self {
        self.bucket_width = Some(width);
        self
    }

    /// Filter by a single project ID.
    #[must_use]
    pub fn project_id(mut self, id: impl Into<String>) -> Self {
        self.project_ids.push(id.into());
        self
    }

    /// Filter by multiple project IDs.
    #[must_use]
    pub fn project_ids<I, S>(mut self, ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.project_ids.extend(ids.into_iter().map(Into::into));
        self
    }

    /// Filter by a single user ID.
    #[must_use]
    pub fn user_id(mut self, id: impl Into<String>) -> Self {
        self.user_ids.push(id.into());
        self
    }

    /// Filter by multiple user IDs.
    #[must_use]
    pub fn user_ids<I, S>(mut self, ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.user_ids.extend(ids.into_iter().map(Into::into));
        self
    }

    /// Filter by a single API key ID.
    #[must_use]
    pub fn api_key_id(mut self, id: impl Into<String>) -> Self {
        self.api_key_ids.push(id.into());
        self
    }

    /// Filter by multiple API key IDs.
    #[must_use]
    pub fn api_key_ids<I, S>(mut self, ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.api_key_ids.extend(ids.into_iter().map(Into::into));
        self
    }

    /// Filter by a single model.
    #[must_use]
    pub fn model(mut self, model: impl Into<String>) -> Self {
        self.models.push(model.into());
        self
    }

    /// Filter by multiple models.
    #[must_use]
    pub fn models<I, S>(mut self, models: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.models.extend(models.into_iter().map(Into::into));
        self
    }

    /// Add a group by field.
    #[must_use]
    pub fn group_by(mut self, field: GroupBy) -> Self {
        self.group_by.push(field);
        self
    }

    /// Add multiple group by fields.
    #[must_use]
    pub fn group_by_fields<I>(mut self, fields: I) -> Self
    where
        I: IntoIterator<Item = GroupBy>,
    {
        self.group_by.extend(fields);
        self
    }

    /// Set the maximum number of results to return.
    #[must_use]
    pub fn limit(mut self, limit: i32) -> Self {
        self.limit = Some(limit);
        self
    }

    /// Set the pagination cursor.
    #[must_use]
    pub fn page(mut self, page: impl Into<String>) -> Self {
        self.page = Some(page.into());
        self
    }

    /// Get the start time.
    #[must_use]
    pub fn start_time(&self) -> i32 {
        self.start_time
    }

    /// Get the end time.
    #[must_use]
    pub fn end_time(&self) -> Option<i32> {
        self.end_time
    }

    /// Get the bucket width.
    #[must_use]
    pub fn bucket_width_ref(&self) -> Option<BucketWidth> {
        self.bucket_width
    }

    /// Get the project IDs.
    #[must_use]
    pub fn project_ids_ref(&self) -> &[String] {
        &self.project_ids
    }

    /// Get the user IDs.
    #[must_use]
    pub fn user_ids_ref(&self) -> &[String] {
        &self.user_ids
    }

    /// Get the API key IDs.
    #[must_use]
    pub fn api_key_ids_ref(&self) -> &[String] {
        &self.api_key_ids
    }

    /// Get the models.
    #[must_use]
    pub fn models_ref(&self) -> &[String] {
        &self.models
    }

    /// Get the group by fields.
    #[must_use]
    pub fn group_by_ref(&self) -> &[GroupBy] {
        &self.group_by
    }

    /// Get the limit.
    #[must_use]
    pub fn limit_ref(&self) -> Option<i32> {
        self.limit
    }

    /// Get the page cursor.
    #[must_use]
    pub fn page_ref(&self) -> Option<&str> {
        self.page.as_deref()
    }

    /// Convert project IDs to `Option<Vec<String>>`.
    #[must_use]
    pub fn project_ids_option(&self) -> Option<Vec<String>> {
        if self.project_ids.is_empty() {
            None
        } else {
            Some(self.project_ids.clone())
        }
    }

    /// Convert user IDs to `Option<Vec<String>>`.
    #[must_use]
    pub fn user_ids_option(&self) -> Option<Vec<String>> {
        if self.user_ids.is_empty() {
            None
        } else {
            Some(self.user_ids.clone())
        }
    }

    /// Convert API key IDs to `Option<Vec<String>>`.
    #[must_use]
    pub fn api_key_ids_option(&self) -> Option<Vec<String>> {
        if self.api_key_ids.is_empty() {
            None
        } else {
            Some(self.api_key_ids.clone())
        }
    }

    /// Convert models to `Option<Vec<String>>`.
    #[must_use]
    pub fn models_option(&self) -> Option<Vec<String>> {
        if self.models.is_empty() {
            None
        } else {
            Some(self.models.clone())
        }
    }

    /// Convert group by fields to `Option<Vec<String>>`.
    #[must_use]
    pub fn group_by_option(&self) -> Option<Vec<String>> {
        if self.group_by.is_empty() {
            None
        } else {
            Some(self.group_by.iter().map(ToString::to_string).collect())
        }
    }

    /// Get bucket width as `Option<&str>`.
    #[must_use]
    pub fn bucket_width_str(&self) -> Option<&str> {
        self.bucket_width.as_ref().map(BucketWidth::as_str)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_usage_builder_basic() {
        let builder = UsageBuilder::new(1_704_067_200, None);
        assert_eq!(builder.start_time(), 1_704_067_200);
        assert_eq!(builder.end_time(), None);
    }

    #[test]
    fn test_usage_builder_with_end_time() {
        let builder = UsageBuilder::new(1_704_067_200, Some(1_704_153_600));
        assert_eq!(builder.start_time(), 1_704_067_200);
        assert_eq!(builder.end_time(), Some(1_704_153_600));
    }

    #[test]
    fn test_usage_builder_with_bucket_width() {
        let builder = UsageBuilder::new(1_704_067_200, None).bucket_width(BucketWidth::Day);
        assert_eq!(builder.bucket_width_ref(), Some(BucketWidth::Day));
        assert_eq!(builder.bucket_width_str(), Some("1d"));
    }

    #[test]
    fn test_usage_builder_with_filters() {
        let builder = UsageBuilder::new(1_704_067_200, None)
            .project_id("proj_123")
            .user_id("user_456")
            .model("gpt-4");

        assert_eq!(builder.project_ids_ref(), &["proj_123"]);
        assert_eq!(builder.user_ids_ref(), &["user_456"]);
        assert_eq!(builder.models_ref(), &["gpt-4"]);
    }

    #[test]
    fn test_usage_builder_with_multiple_filters() {
        let builder = UsageBuilder::new(1_704_067_200, None)
            .project_ids(vec!["proj_1", "proj_2"])
            .user_ids(vec!["user_1", "user_2"])
            .models(vec!["gpt-4", "gpt-3.5-turbo"]);

        assert_eq!(builder.project_ids_ref().len(), 2);
        assert_eq!(builder.user_ids_ref().len(), 2);
        assert_eq!(builder.models_ref().len(), 2);
    }

    #[test]
    fn test_usage_builder_with_group_by() {
        let builder = UsageBuilder::new(1_704_067_200, None)
            .group_by(GroupBy::ProjectId)
            .group_by(GroupBy::Model);

        assert_eq!(builder.group_by_ref().len(), 2);
        let group_by_strings = builder.group_by_option().unwrap();
        assert_eq!(group_by_strings, vec!["project_id", "model"]);
    }

    #[test]
    fn test_usage_builder_with_pagination() {
        let builder = UsageBuilder::new(1_704_067_200, None)
            .limit(50)
            .page("next_page_token");

        assert_eq!(builder.limit_ref(), Some(50));
        assert_eq!(builder.page_ref(), Some("next_page_token"));
    }

    #[test]
    fn test_bucket_width_display() {
        assert_eq!(BucketWidth::Day.to_string(), "1d");
        assert_eq!(BucketWidth::Hour.to_string(), "1h");
    }

    #[test]
    fn test_group_by_display() {
        assert_eq!(GroupBy::ProjectId.to_string(), "project_id");
        assert_eq!(GroupBy::UserId.to_string(), "user_id");
        assert_eq!(GroupBy::ApiKeyId.to_string(), "api_key_id");
        assert_eq!(GroupBy::Model.to_string(), "model");
    }

    #[test]
    fn test_empty_vectors_to_none() {
        let builder = UsageBuilder::new(1_704_067_200, None);
        assert!(builder.project_ids_option().is_none());
        assert!(builder.user_ids_option().is_none());
        assert!(builder.api_key_ids_option().is_none());
        assert!(builder.models_option().is_none());
        assert!(builder.group_by_option().is_none());
    }
}