ru_openai/
configuration.rs1use std::time::Duration;
2
3
4const API_BASE_PATH: &str = "https://api.openai.com/v1";
5
6const USER_AGENT_VALUE: &str = "openai-rust/0.1";
7
8#[derive(Debug)]
9pub struct Configuration {
10 pub api_key: Option<String>,
11 pub organization: Option<String>,
12 pub base_path: String,
13 pub proxy: Option<String>,
14 pub timeout: Option<u64>,
15 pub ignore_ssl: Option<bool>,
16}
17
18impl Configuration {
19 pub fn new(
20 api_key: Option<String>,
21 organization: Option<String>,
22 ) -> Self {
23
24 Self {
25 api_key,
26 organization,
27 base_path: API_BASE_PATH.to_string(),
28 proxy: None,
29 timeout: None,
30 ignore_ssl: Some(false),
31 }
32 }
33
34 pub fn new_personal(api_key: String) -> Self {
35 Self::new(Some(api_key), None)
36 }
37
38 pub fn new_organization(api_key: String, organization: String) -> Self {
39 Self::new(Some(api_key), Some(organization))
40 }
41
42 pub fn base_path(mut self, base_path: String) -> Self {
43 self.base_path = base_path;
44 self
45 }
46
47 pub fn proxy(mut self, proxy: String) -> Self {
48 self.proxy = Some(proxy);
49 self
50 }
51
52 pub fn timeout(mut self, timeout: u64) -> Self {
53 self.timeout = Some(timeout);
54 self
55 }
56
57 pub fn ignore_ssl(mut self, ignore_ssl: bool) -> Self {
58 self.ignore_ssl = Some(ignore_ssl);
59 self
60 }
61
62 pub fn apply_to_request(self, builder: reqwest::ClientBuilder, path: String, method: reqwest::Method) -> reqwest::RequestBuilder{
63 let mut client_builder = match self.proxy.clone() {
64 Some(proxy) => {
65 let proxy = reqwest::Proxy::https(proxy).unwrap();
66 builder.proxy(proxy)
67 },
68 None => builder,
69 };
70 client_builder = match self.timeout.clone() {
71 Some(timeout) => client_builder.timeout(Duration::from_secs(timeout)),
72 None => client_builder,
73 };
74 client_builder = match self.ignore_ssl.clone() {
75 Some(ignore_ssl) => client_builder.danger_accept_invalid_certs(ignore_ssl),
76 None => client_builder,
77 };
78
79 let client = client_builder.user_agent(USER_AGENT_VALUE).build().unwrap();
80
81 let url = format!("{}{}", self.base_path, path);
82 let mut builder = client.request(method, url);
83 builder = match self.api_key.clone() {
84 Some(key) => builder.bearer_auth(key.clone()),
85 None => builder,
86 };
87 match self.organization.clone() {
89 Some(org) => builder.header("OpenAI-Organization", org),
90 None => builder,
91 }
92 }
93
94}