api_openai 0.3.0

OpenAI's API for accessing large language models (LLMs).
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
//! Comprehensive tests for cURL generation functionality.
//!
//! This file implements comprehensive failing tests for cURL generation functionality
//! following TDD principles. Tests cover converting API requests to equivalent cURL
//! commands for debugging, documentation, and external integration purposes.

use api_openai::ClientApiAccessors;
use api_openai::
{
  Client,
  environment ::OpenaiEnvironmentImpl,
  secret ::Secret,
  components ::embeddings_request::CreateEmbeddingRequest,
  curl_generation ::{ CurlGeneration, CurlGenerator, CurlRequestBuilder, CurlRequest, CurlFormatOptions, CurlFormattingOptions, CurlConnectionOptions },
};

use std::collections::HashMap;

/// Helper function to create test client
fn create_test_client() -> Result< Client< OpenaiEnvironmentImpl >, Box< dyn std::error::Error > >
{
  let secret = Secret::load_from_env( "OPENAI_API_KEY" )
    .unwrap_or_else(|_| Secret::load_with_fallbacks( "OPENAI_API_KEY" )
    .unwrap_or_else(|_| panic!("No API key available for testing")));
  let env = OpenaiEnvironmentImpl::build( secret, None, None, api_openai::environment::OpenAIRecommended::base_url().to_string(), api_openai::environment::OpenAIRecommended::realtime_base_url().to_string() )?;
  Ok( Client::build( env )? )
}

/// Helper function to check if we should run integration tests
fn should_run_integration_tests() -> bool
{
  std ::env::var( "OPENAI_API_KEY" ).is_ok()
}

// === UNIT TESTS ===

#[ test ]
fn test_curl_generator_basic_structure()
{
  let generator = CurlGenerator::new();

  assert!(generator.can_generate_curl());
  assert_eq!(generator.get_supported_methods().len(), 4); // GET, POST, PUT, DELETE
  assert!(generator.get_supported_methods().contains(&"GET".to_string()));
  assert!(generator.get_supported_methods().contains(&"POST".to_string()));
}

#[ test ]
fn test_curl_request_builder_structure()
{
  let builder = CurlRequestBuilder::new();

  let request = builder
    .method("POST")
    .url("https://api.openai.com/v1/embeddings")
    .header("Authorization", "Bearer sk-test")
    .header("Content-Type", "application/json")
    .body(r#"{"input": "test", "model": "text-embedding-ada-002"}"#)
    .build();

  assert_eq!(request.method, "POST");
  assert_eq!(request.url, "https://api.openai.com/v1/embeddings");
  assert_eq!(request.headers.len(), 2);
  assert!(request.body.is_some());
}

#[ test ]
fn test_curl_command_generation_basic()
{
  let request = CurlRequest
  {
    method : "POST".to_string(),
    url : "https://api.openai.com/v1/embeddings".to_string(),
    headers : vec![
      ("Authorization".to_string(), "Bearer sk-test".to_string()),
      ("Content-Type".to_string(), "application/json".to_string()),
    ],
    body : Some(r#"{"input": "test", "model": "text-embedding-ada-002"}"#.to_string()),
  };

  let curl_command = request.to_curl_command();

  assert!(curl_command.contains("curl"));
  assert!(curl_command.contains("-X POST"));
  assert!(curl_command.contains("https://api.openai.com/v1/embeddings"));
  assert!(curl_command.contains("-H 'Authorization: Bearer sk-test'"));
  assert!(curl_command.contains("-H 'Content-Type: application/json'"));
  assert!(curl_command.contains("-d"));
  assert!(curl_command.contains("text-embedding-ada-002"));
}

#[ test ]
fn test_curl_command_escaping()
{
  let request = CurlRequest
  {
    method : "POST".to_string(),
    url : "https://api.openai.com/v1/chat/completions".to_string(),
    headers : vec![
      ("Authorization".to_string(), "Bearer sk-test".to_string()),
    ],
    body : Some(r#"{"messages": [{"role": "user", "content": "What's \"hello world\" in Rust?"}]}"#.to_string()),
  };

  let curl_command = request.to_curl_command();

  // Should properly escape quotes in JSON - checking the actual pattern
  assert!(curl_command.contains(r#"\\"hello world\\""#));
  assert!(!curl_command.contains("\"hello world\"")); // Raw quotes should be escaped
}

#[ test ]
fn test_curl_command_get_request()
{
  let request = CurlRequest
  {
    method : "GET".to_string(),
    url : "https://api.openai.com/v1/models".to_string(),
    headers : vec![
      ("Authorization".to_string(), "Bearer sk-test".to_string()),
    ],
    body : None,
  };

  let curl_command = request.to_curl_command();

  assert!(curl_command.contains("curl"));
  assert!(curl_command.contains("-X GET"));
  assert!(curl_command.contains("https://api.openai.com/v1/models"));
  assert!(curl_command.contains("-H 'Authorization: Bearer sk-test'"));
  assert!(!curl_command.contains("-d")); // No body for GET request
}

#[ test ]
fn test_curl_command_formatting_options()
{
  let request = CurlRequest
  {
    method : "POST".to_string(),
    url : "https://api.openai.com/v1/embeddings".to_string(),
    headers : vec![
      ("Authorization".to_string(), "Bearer sk-test".to_string()),
      ("Content-Type".to_string(), "application/json".to_string()),
    ],
    body : Some(r#"{"input": "test"}"#.to_string()),
  };

  let options = CurlFormatOptions
  {
    formatting : CurlFormattingOptions
    {
      pretty_print : true,
      include_verbose : true,
      include_silent : false,
    },
    connection : CurlConnectionOptions
    {
      include_insecure : false,
    },
    timeout : Some(30),
  };

  let curl_command = request.to_curl_command_with_options(&options);

  assert!(curl_command.contains("--verbose"));
  assert!(curl_command.contains("--max-time 30"));
  assert!(!curl_command.contains("--silent"));
  assert!(!curl_command.contains("--insecure"));

  // Should be formatted with line breaks for readability
  assert!(curl_command.contains("\\\n"));
}

#[ test ]
fn test_embeddings_request_to_curl()
{
  let client = create_test_client().expect("Failed to create test client");

  let request = CreateEmbeddingRequest::new_single(
    "The quick brown fox jumps over the lazy dog".to_string(),
    "text-embedding-ada-002".to_string()
  );

  let curl_command = client.embeddings().to_curl(&request).expect("Failed to generate cURL");

  assert!(curl_command.contains("curl"));
  assert!(curl_command.contains("-X POST"));
  assert!(curl_command.contains("https://api.openai.com/v1/embeddings"));
  assert!(curl_command.contains("text-embedding-ada-002"));
  assert!(curl_command.contains("The quick brown fox"));
}

// Note : Chat completion tests will be added once the request structures are properly exposed

#[ test ]
fn test_models_list_request_to_curl()
{
  let client = create_test_client().expect("Failed to create test client");

  let curl_command = client.models().list_to_curl().expect("Failed to generate cURL");

  assert!(curl_command.contains("curl"));
  assert!(curl_command.contains("-X GET"));
  assert!(curl_command.contains("https://api.openai.com/v1/models"));
  assert!(curl_command.contains("Authorization: Bearer") || curl_command.contains("authorization: Bearer"));
}

#[ test ]
fn test_models_retrieve_request_to_curl()
{
  let client = create_test_client().expect("Failed to create test client");

  let curl_command = client.models().retrieve_to_curl("gpt-5-nano").expect("Failed to generate cURL");

  assert!(curl_command.contains("curl"));
  assert!(curl_command.contains("-X GET"));
  assert!(curl_command.contains("https://api.openai.com/v1/models/gpt-5-nano"));
  assert!(curl_command.contains("Authorization: Bearer") || curl_command.contains("authorization: Bearer"));
}

#[ test ]
fn test_curl_generation_with_custom_headers()
{
  let client = create_test_client().expect("Failed to create test client");

  let mut custom_headers = HashMap::new();
  custom_headers.insert("X-Custom-Header".to_string(), "custom-value".to_string());
  custom_headers.insert("User-Agent".to_string(), "MyApp/1.0".to_string());

  let request = CreateEmbeddingRequest::new_single(
    "test".to_string(),
    "text-embedding-ada-002".to_string()
  );

  let curl_command = client.embeddings()
    .to_curl_with_headers(&request, &custom_headers)
    .expect("Failed to generate cURL");

  assert!(curl_command.contains("-H 'X-Custom-Header: custom-value'"));
  assert!(curl_command.contains("-H 'User-Agent: MyApp/1.0'"));
}

#[ test ]
fn test_curl_generation_with_organization_project()
{
  let secret = Secret::load_from_env("OPENAI_API_KEY")
    .unwrap_or_else(|_| Secret::load_with_fallbacks("OPENAI_API_KEY")
    .unwrap_or_else(|_| panic!("No API key available for testing")));
  let env = OpenaiEnvironmentImpl::build(
    secret,
    Some("org-123".to_string()),
    Some("proj-456".to_string()),
    api_openai ::environment::OpenAIRecommended::base_url().to_string(),
    api_openai ::environment::OpenAIRecommended::realtime_base_url().to_string()
  ).expect("Failed to create environment");
  let client = Client::build(env).expect("Failed to create client");

  let request = CreateEmbeddingRequest::new_single(
    "test".to_string(),
    "text-embedding-ada-002".to_string()
  );

  let curl_command = client.embeddings().to_curl(&request).expect("Failed to generate cURL");

  assert!(curl_command.contains("-H 'openai-organization: org-123'"));
  assert!(curl_command.contains("-H 'openai-project: proj-456'"));
}

#[ test ]
fn test_curl_security_header_redaction()
{
  let client = create_test_client().expect("Failed to create test client");

  let request = CreateEmbeddingRequest::new_single(
    "test".to_string(),
    "text-embedding-ada-002".to_string()
  );

  let curl_command = client.embeddings().to_curl_safe(&request).expect("Failed to generate safe cURL");

  // API key should be redacted for security
  assert!(curl_command.contains("Authorization: Bearer [REDACTED]"));
  assert!(!curl_command.contains("sk-")); // No actual API keys should be present
}

#[ test ]
fn test_curl_generation_error_handling()
{
  let client = create_test_client().expect("Failed to create test client");

  // Test with invalid request that should fail serialization
  let invalid_request = CreateEmbeddingRequest::new_single(
    String::new(), // Empty input might cause validation errors
    String::new()  // Empty model
  );

  let result = client.embeddings().to_curl(&invalid_request);

  // Should handle serialization errors gracefully
  match result
  {
    Ok(_) => {}, // Some implementations might allow empty values
    Err(e) =>
    {
      let error_str = format!("{e:?}");
      assert!(error_str.contains("model") || error_str.contains("input") || error_str.contains("serialization"));
    }
  }
}

// === INTEGRATION TESTS ===

#[ cfg( feature = "integration" ) ]
#[ tokio::test ]
async fn test_curl_generation_integration_embeddings()
{
  // INTEGRATION TEST - Skip if no API key available
  if !should_run_integration_tests()
  {
    eprintln!("Skipping integration test : OPENAI_API_KEY not available");
    return;
  }

  let client = create_test_client().expect("Failed to create test client");

  let request = CreateEmbeddingRequest::new_single(
    "Integration test for cURL generation".to_string(),
    "text-embedding-ada-002".to_string()
  );

  // Test that we can generate a valid cURL command
  let curl_command = client.embeddings().to_curl(&request).expect("Failed to generate cURL");

  // Validate the generated command structure
  assert!(curl_command.starts_with("curl"));
  assert!(curl_command.contains("https://api.openai.com/v1/embeddings"));
  assert!(curl_command.contains("Integration test for cURL generation"));

  // The command should be executable (though we won't execute it in tests)
  let lines : Vec< &str > = curl_command.split('\n').collect();
  assert!(!lines.is_empty()); // Should have at least the curl command line
}

// Note : Chat integration tests will be added once request structures are properly exposed

#[ cfg( feature = "integration" ) ]
#[ tokio::test ]
async fn test_curl_generation_integration_models()
{
  // INTEGRATION TEST - Skip if no API key available
  if !should_run_integration_tests()
  {
    eprintln!("Skipping integration test : OPENAI_API_KEY not available");
    return;
  }

  let client = create_test_client().expect("Failed to create test client");

  // Test list models cURL generation
  let list_curl = client.models().list_to_curl().expect("Failed to generate list cURL");
  assert!(list_curl.contains("-X GET"));
  assert!(list_curl.contains("https://api.openai.com/v1/models"));

  // Test retrieve model cURL generation
  let retrieve_curl = client.models().retrieve_to_curl("gpt-5-nano").expect("Failed to generate retrieve cURL");
  assert!(retrieve_curl.contains("-X GET"));
  assert!(retrieve_curl.contains("https://api.openai.com/v1/models/gpt-5-nano"));
}

#[ test ]
fn test_curl_generation_performance_benchmark()
{
  let client = create_test_client().expect("Failed to create test client");

  let request = CreateEmbeddingRequest::new_single(
    "Performance test request".to_string(),
    "text-embedding-ada-002".to_string()
  );

  let start = std::time::Instant::now();

  // Generate 100 cURL commands to test performance
  for _ in 0..100
  {
    let _curl_command = client.embeddings().to_curl(&request).expect("Failed to generate cURL");
  }

  let elapsed = start.elapsed();

  // cURL generation should be fast (< 100ms for 100 generations)
  assert!(elapsed < core::time::Duration::from_millis(100),
          "cURL generation too slow : {elapsed:?}");
}

#[ test ]
fn test_curl_generation_memory_efficiency()
{
  let client = create_test_client().expect("Failed to create test client");

  let request = CreateEmbeddingRequest::new_single(
    "Memory efficiency test".to_string(),
    "text-embedding-ada-002".to_string()
  );

  // Generate many cURL commands and ensure they're properly freed
  for _ in 0..1000
  {
    let curl_command = client.embeddings().to_curl(&request).expect("Failed to generate cURL");

    // Basic validation that command is generated correctly
    assert!(curl_command.contains("curl"));
    assert!(curl_command.len() > 100); // Should have reasonable length

    // Allow the string to be dropped at end of loop iteration
  }

  // If we reach here without OOM, memory efficiency is acceptable
  // Test passes by successful completion without memory errors
}

// === STRUCTURE IMPLEMENTATIONS COMPLETED ===
// All cURL generation functionality is now implemented