openai_rust_sdk/api/
common.rs1use crate::api::base::HttpClient;
4use crate::error::Result;
5
6pub trait ApiClientConstructors: Sized {
8 fn from_http_client(http_client: HttpClient) -> Self;
10
11 fn new<S: Into<String>>(api_key: S) -> Result<Self> {
21 Ok(Self::from_http_client(HttpClient::new(api_key)?))
22 }
23
24 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
41pub 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
64pub trait ListQueryParams {
66 fn limit(&self) -> Option<u32>;
68 fn order_str(&self) -> Option<&str>;
70 fn after(&self) -> Option<&String>;
72 fn before(&self) -> Option<&String>;
74}
75
76#[derive(Debug, Clone, Default)]
78pub struct StandardListParams {
79 pub limit: Option<u32>,
81 pub order: Option<String>,
83 pub after: Option<String>,
85 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}