openai4rs/config/
client.rs1use super::http::{HttpConfig, HttpConfigBuilder};
2use super::{Credentials, CredentialsBuilder};
3use crate::OpenAI;
4use crate::common::types::Body;
5use crate::config::CredentialsBuilderError;
6use http::header::IntoHeaderName;
7use http::{HeaderMap, HeaderValue};
8use std::fmt;
9use std::time::Duration;
10
11#[derive(Debug)]
12pub enum ConfigBuildError {
13 RequiredFieldMissing(String),
15 ValidationError(String),
17}
18
19impl fmt::Display for ConfigBuildError {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 match self {
22 ConfigBuildError::RequiredFieldMissing(field) => {
23 write!(f, "Required field missing: {field}")
24 }
25 ConfigBuildError::ValidationError(msg) => {
26 write!(f, "Validation error: {msg}")
27 }
28 }
29 }
30}
31
32impl std::error::Error for ConfigBuildError {}
33
34impl From<super::http::HttpConfigBuilderError> for ConfigBuildError {
36 fn from(err: super::http::HttpConfigBuilderError) -> Self {
37 ConfigBuildError::RequiredFieldMissing(err.to_string())
38 }
39}
40
41impl From<CredentialsBuilderError> for ConfigBuildError {
42 fn from(err: CredentialsBuilderError) -> Self {
43 ConfigBuildError::RequiredFieldMissing(err.to_string())
44 }
45}
46
47pub struct Config {
49 credentials: Credentials,
51 http: HttpConfig,
53 retry_count: usize,
55}
56impl Config {
57 pub fn new(api_key: impl Into<String>, base_url: impl Into<String>) -> Self {
64 Self {
65 credentials: Credentials::new(api_key.into(), base_url.into()),
66 http: HttpConfig::default(),
67 retry_count: 5,
68 }
69 }
70
71 pub fn builder() -> ConfigBuilder {
73 ConfigBuilder {
74 retry_count: 5,
75 credentials_builder: CredentialsBuilder::default(),
76 http_builder: HttpConfigBuilder::default(),
77 }
78 }
79
80 #[inline]
82 pub fn api_key(&self) -> &str {
83 self.credentials.api_key()
84 }
85
86 #[inline]
88 pub fn base_url(&self) -> &str {
89 self.credentials.base_url()
90 }
91
92 #[inline]
94 pub fn retry_count(&self) -> usize {
95 self.retry_count
96 }
97
98 #[inline]
100 pub fn timeout(&self) -> Duration {
101 self.http.timeout()
102 }
103
104 #[inline]
106 pub fn proxy(&self) -> Option<&String> {
107 self.http.proxy()
108 }
109
110 #[inline]
112 pub fn user_agent(&self) -> Option<&HeaderValue> {
113 self.http.user_agent()
114 }
115
116 #[inline]
118 pub fn connect_timeout(&self) -> Duration {
119 self.http.connect_timeout()
120 }
121
122 #[inline]
124 pub fn http(&self) -> &HttpConfig {
125 &self.http
126 }
127
128 #[inline]
130 pub fn credentials(&self) -> &Credentials {
131 &self.credentials
132 }
133
134 pub fn with_base_url(&mut self, base_url: impl Into<String>) -> &mut Self {
144 self.credentials.with_base_url(base_url);
145 self
146 }
147
148 pub fn with_api_key(&mut self, api_key: impl Into<String>) -> &mut Self {
158 self.credentials.with_api_key(api_key);
159 self
160 }
161
162 pub fn with_retry_count(&mut self, retry_count: usize) -> &mut Self {
172 self.retry_count = retry_count;
173 self
174 }
175
176 pub fn with_timeout(&mut self, timeout: Duration) -> &mut Self {
186 self.http.with_timeout(timeout);
187 self
188 }
189
190 pub fn with_connect_timeout(&mut self, connect_timeout: Duration) -> &mut Self {
200 self.http.with_connect_timeout(connect_timeout);
201 self
202 }
203
204 pub fn with_proxy(&mut self, proxy: impl Into<String>) -> &mut Self {
214 self.http.with_proxy(proxy);
215 self
216 }
217
218 pub fn with_user_agent(&mut self, user_agent: HeaderValue) -> &mut Self {
228 self.http.with_user_agent(user_agent);
229 self
230 }
231}
232
233pub struct ConfigBuilder {
235 retry_count: usize,
237 credentials_builder: CredentialsBuilder,
239 http_builder: HttpConfigBuilder,
241}
242
243impl ConfigBuilder {
244 pub fn build(self) -> Result<Config, ConfigBuildError> {
250 Ok(Config {
251 credentials: self.credentials_builder.build()?,
252 http: self.http_builder.build()?,
253 retry_count: self.retry_count,
254 })
255 }
256
257 pub fn build_openai(self) -> Result<OpenAI, ConfigBuildError> {
263 Ok(OpenAI::with_config(self.build()?))
264 }
265
266 pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
276 self.credentials_builder = self.credentials_builder.api_key(api_key.into());
277 self
278 }
279
280 pub fn base_url(mut self, base_url: impl Into<String>) -> Self {
290 self.credentials_builder = self.credentials_builder.base_url(base_url.into());
291 self
292 }
293
294 pub fn retry_count(mut self, retry_count: usize) -> Self {
304 self.retry_count = retry_count;
305 self
306 }
307
308 pub fn timeout(mut self, timeout: Duration) -> Self {
318 self.http_builder = self.http_builder.timeout(timeout);
319 self
320 }
321
322 pub fn connect_timeout(mut self, connect_timeout: Duration) -> Self {
332 self.http_builder = self.http_builder.connect_timeout(connect_timeout);
333 self
334 }
335
336 pub fn proxy(mut self, proxy: impl Into<String>) -> Self {
346 self.http_builder = self.http_builder.proxy(proxy.into());
347 self
348 }
349
350 pub fn user_agent(mut self, user_agent: HeaderValue) -> Self {
360 self.http_builder = self.http_builder.user_agent(user_agent);
361 self
362 }
363
364 pub fn header<K: IntoHeaderName>(mut self, key: K, value: HeaderValue) -> Self {
375 self.http_builder = self.http_builder.header(key, value);
376 self
377 }
378
379 pub fn body(mut self, key: impl Into<String>, value: impl Into<serde_json::Value>) -> Self {
390 self.http_builder = self.http_builder.body(key.into(), value.into());
391 self
392 }
393
394 pub fn headers(mut self, headers: HeaderMap) -> Self {
404 self.http_builder = self.http_builder.headers(headers);
405 self
406 }
407
408 pub fn bodys(mut self, bodys: Body) -> Self {
418 self.http_builder = self.http_builder.bodys(bodys);
419 self
420 }
421}