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
mod private
{
//! CURL command generation for debugging XAI API requests.
//!
//! This module provides utilities to convert XAI API requests into
//! equivalent CURL commands for debugging, testing, and sharing.
//!
//! # Use Cases
//!
//! 1. **Debugging**: Test requests directly with CURL
//! 2. **Reproduction**: Share reproducible API calls with support
//! 3. **Documentation**: Generate example requests for docs
//! 4. **Learning**: Understand the underlying HTTP requests
//!
//! # Design Decisions
//!
//! ## Why CURL?
//!
//! CURL is the universal HTTP debugging tool:
//!
//! 1. **Ubiquity**: Available on all platforms
//! 2. **Simplicity**: Easy to understand and modify
//! 3. **Portability**: Works in any terminal
//! 4. **Standard**: Industry-standard HTTP debugging
//!
//! ## Command Format
//!
//! Generated commands use:
//!
//! - **Readable formatting**: Multi-line with backslash continuation
//! - **Environment variables**: `$XAI_API_KEY` for security
//! - **Pretty JSON**: Indented request body for readability
//! - **Explicit headers**: All required headers shown
//!
//! ## Security
//!
//! **IMPORTANT**: Generated commands use `$XAI_API_KEY` environment
//! variable instead of embedding the actual key. This prevents
//! accidental key exposure when sharing commands.
use crate::ChatCompletionRequest;
/// Converts a chat completion request to a CURL command.
///
/// Generates a ready-to-execute CURL command that makes the same
/// API request. The API key is referenced via `$XAI_API_KEY`
/// environment variable for security.
///
/// # Arguments
///
/// * `request` - The chat completion request to convert
///
/// # Returns
///
/// A CURL command string that can be executed in a shell.
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "curl_diagnostics") ]
/// # {
/// use api_xai::{ to_curl, ChatCompletionRequest, Message };
///
/// let request = ChatCompletionRequest::former()
/// .model( "grok-2-1212".to_string() )
/// .messages( vec![ Message::user( "Hello!" ) ] )
/// .temperature( 0.7 )
/// .form();
///
/// let curl = to_curl( &request );
/// println!( "{}", curl );
///
/// // Output:
/// // curl -X POST https://api.x.ai/v1/chat/completions \
/// // -H "Authorization : Bearer $XAI_API_KEY" \
/// // -H "Content-Type : application/json" \
/// // -d '{
/// // "model": "grok-2-1212",
/// // "messages": [{"role": "user", "content": "Hello!"}],
/// // "temperature": 0.7
/// // }'
/// # }
/// ```
#[ cfg( feature = "curl_diagnostics" ) ]
pub fn to_curl( request : &ChatCompletionRequest ) -> String
{
let json = serde_json::to_string_pretty( request )
.unwrap_or_else( | _ | "{}".to_string() );
format!
(
"curl -X POST https://api.x.ai/v1/chat/completions \\\n \
-H \"Authorization : Bearer $XAI_API_KEY\" \\\n \
-H \"Content-Type : application/json\" \\\n \
-d '{json}'"
)
}
/// Converts a chat completion request to a CURL command with a custom API key.
///
/// **WARNING**: This function embeds the API key directly in the command.
/// Only use for testing. Never share the output.
///
/// # Arguments
///
/// * `request` - The chat completion request to convert
/// * `api_key` - The API key to embed (WARNING: visible in output)
///
/// # Returns
///
/// A CURL command string with embedded API key.
///
/// # Security Warning
///
/// The generated command contains your API key in plain text.
/// Do not share this command or save it in version control.
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "curl_diagnostics") ]
/// # {
/// use api_xai::{ to_curl_with_key, ChatCompletionRequest, Message };
///
/// let request = ChatCompletionRequest::former()
/// .model( "grok-2-1212".to_string() )
/// .messages( vec![ Message::user( "Hello!" ) ] )
/// .form();
///
/// // WARNING: This embeds your API key in the output!
/// let curl = to_curl_with_key( &request, "xai-your-key-here" );
///
/// // Only use for immediate testing, never share
/// println!( "{}", curl );
/// # }
/// ```
#[ cfg( feature = "curl_diagnostics" ) ]
pub fn to_curl_with_key( request : &ChatCompletionRequest, api_key : &str ) -> String
{
let json = serde_json::to_string_pretty( request )
.unwrap_or_else( | _ | "{}".to_string() );
format!
(
"curl -X POST https://api.x.ai/v1/chat/completions \\\n \
-H \"Authorization : Bearer {api_key}\" \\\n \
-H \"Content-Type : application/json\" \\\n \
-d '{json}'"
)
}
/// Converts a chat completion request to a CURL command with custom endpoint.
///
/// Useful for testing with proxy servers or alternative endpoints.
///
/// # Arguments
///
/// * `request` - The chat completion request to convert
/// * `endpoint` - The custom endpoint URL (must include full path)
///
/// # Returns
///
/// A CURL command string targeting the custom endpoint.
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "curl_diagnostics") ]
/// # {
/// use api_xai::{ to_curl_with_endpoint, ChatCompletionRequest, Message };
///
/// let request = ChatCompletionRequest::former()
/// .model( "grok-2-1212".to_string() )
/// .messages( vec![ Message::user( "Hello!" ) ] )
/// .form();
///
/// // Test with local proxy
/// let curl = to_curl_with_endpoint
/// (
/// &request,
/// "http://localhost:8080/v1/chat/completions"
/// );
///
/// println!( "{}", curl );
/// # }
/// ```
#[ cfg( feature = "curl_diagnostics" ) ]
pub fn to_curl_with_endpoint
(
request : &ChatCompletionRequest,
endpoint : &str
)
-> String
{
let json = serde_json::to_string_pretty( request )
.unwrap_or_else( | _ | "{}".to_string() );
format!
(
"curl -X POST {endpoint} \\\n \
-H \"Authorization : Bearer $XAI_API_KEY\" \\\n \
-H \"Content-Type : application/json\" \\\n \
-d '{json}'"
)
}
/// Converts a chat completion request to a compact CURL command (single line).
///
/// Generates a single-line CURL command without pretty formatting.
/// Useful for copy-paste into logs or when space is limited.
///
/// # Arguments
///
/// * `request` - The chat completion request to convert
///
/// # Returns
///
/// A compact CURL command string (single line).
///
/// # Examples
///
/// ```
/// # #[ cfg( feature = "curl_diagnostics") ]
/// # {
/// use api_xai::{ to_curl_compact, ChatCompletionRequest, Message };
///
/// let request = ChatCompletionRequest::former()
/// .model( "grok-2-1212".to_string() )
/// .messages( vec![ Message::user( "Hello!" ) ] )
/// .form();
///
/// let curl = to_curl_compact( &request );
/// println!( "{}", curl );
///
/// // Output (single line):
/// // curl -X POST https://api.x.ai/v1/chat/completions -H "Authorization : Bearer $XAI_API_KEY" -H "Content-Type : application/json" -d '{"model":"grok-2-1212","messages":[...]}'
/// # }
/// ```
#[ cfg( feature = "curl_diagnostics" ) ]
pub fn to_curl_compact( request : &ChatCompletionRequest ) -> String
{
let json = serde_json::to_string( request )
.unwrap_or_else( | _ | "{}".to_string() );
format!
(
"curl -X POST https://api.x.ai/v1/chat/completions -H \"Authorization : Bearer $XAI_API_KEY\" -H \"Content-Type : application/json\" -d '{json}'"
)
}
}
#[ cfg( feature = "curl_diagnostics" ) ]
crate::mod_interface!
{
exposed use
{
to_curl,
to_curl_with_key,
to_curl_with_endpoint,
to_curl_compact,
};
}