openai_rust_sdk/api/
common.rs

1//! Common utilities for API clients to reduce code duplication
2
3use crate::api::base::HttpClient;
4use crate::error::Result;
5
6/// Common trait for API clients with standard constructors
7pub trait ApiClientConstructors: Sized {
8    /// Create a new instance with the HTTP client
9    fn from_http_client(http_client: HttpClient) -> Self;
10
11    /// Creates a new API client
12    ///
13    /// # Arguments
14    ///
15    /// * `api_key` - Your `OpenAI` API key
16    ///
17    /// # Errors
18    ///
19    /// Returns an error if the API key is empty or invalid
20    fn new<S: Into<String>>(api_key: S) -> Result<Self> {
21        Ok(Self::from_http_client(HttpClient::new(api_key)?))
22    }
23
24    /// Creates a new API client with custom base URL
25    ///
26    /// # Arguments
27    ///
28    /// * `api_key` - Your `OpenAI` API key
29    /// * `base_url` - Custom base URL for the API
30    ///
31    /// # Errors
32    ///
33    /// Returns an error if the API key is empty or invalid
34    fn new_with_base_url<S: Into<String>>(api_key: S, base_url: S) -> Result<Self> {
35        Ok(Self::from_http_client(HttpClient::new_with_base_url(
36            api_key, base_url,
37        )?))
38    }
39}
40
41/// Helper function to build query parameters for list operations
42pub fn build_list_query_params<T>(params: &T) -> Vec<(String, String)>
43where
44    T: ListQueryParams,
45{
46    let mut query_params = Vec::new();
47
48    if let Some(limit) = params.limit() {
49        query_params.push(("limit".to_string(), limit.to_string()));
50    }
51    if let Some(order) = params.order_str() {
52        query_params.push(("order".to_string(), order.to_string()));
53    }
54    if let Some(after) = params.after() {
55        query_params.push(("after".to_string(), after.clone()));
56    }
57    if let Some(before) = params.before() {
58        query_params.push(("before".to_string(), before.clone()));
59    }
60
61    query_params
62}
63
64/// Common trait for list query parameters
65pub trait ListQueryParams {
66    /// Get the limit parameter
67    fn limit(&self) -> Option<u32>;
68    /// Get the order parameter as string
69    fn order_str(&self) -> Option<&str>;
70    /// Get the after cursor parameter
71    fn after(&self) -> Option<&String>;
72    /// Get the before cursor parameter
73    fn before(&self) -> Option<&String>;
74}
75
76/// Standard implementation for common list parameters
77#[derive(Debug, Clone, Default)]
78pub struct StandardListParams {
79    /// Maximum number of items to return
80    pub limit: Option<u32>,
81    /// Sort order (asc or desc)
82    pub order: Option<String>,
83    /// Cursor for pagination (return items after this ID)
84    pub after: Option<String>,
85    /// Cursor for pagination (return items before this ID)
86    pub before: Option<String>,
87}
88
89impl ListQueryParams for StandardListParams {
90    fn limit(&self) -> Option<u32> {
91        self.limit
92    }
93
94    fn order_str(&self) -> Option<&str> {
95        self.order.as_deref()
96    }
97
98    fn after(&self) -> Option<&String> {
99        self.after.as_ref()
100    }
101
102    fn before(&self) -> Option<&String> {
103        self.before.as_ref()
104    }
105}