freeman 0.1.0

A terminal-based API testing tool - like Postman, but for your terminal
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
423
424
425
426
427
428
429
//! HTTP client wrapper - executes requests and formats responses

use base64::Engine;
use futures_util::StreamExt;
use std::error::Error;
use std::time::Instant;
use tokio::sync::{mpsc, oneshot};

use crate::messages::NetworkResponse;
use crate::models::{AuthType, Environment, HttpMethod, Request};

/// Format detailed error messages for HTTP request failures
fn format_request_error(e: &reqwest::Error, url: &str) -> String {
    let mut lines = Vec::new();

    // Main error classification
    if e.is_timeout() {
        lines.push("⏱ TIMEOUT: Request timed out after 30 seconds".to_string());
        lines.push("  → The server took too long to respond".to_string());
        lines.push("  → Check if the server is running and accessible".to_string());
    } else if e.is_connect() {
        lines.push("🔌 CONNECTION FAILED: Unable to connect to server".to_string());

        // Try to extract more specific connection error info
        let error_str = e.to_string().to_lowercase();

        if error_str.contains("dns")
            || error_str.contains("name resolution")
            || error_str.contains("getaddrinfo")
        {
            lines.push("  → DNS resolution failed - hostname not found".to_string());
            lines.push("  → Verify the hostname is correct".to_string());
        } else if error_str.contains("refused") || error_str.contains("111") {
            lines.push("  → Connection refused by the server".to_string());
            lines.push("  → The server may not be running on this port".to_string());
        } else if error_str.contains("no route") || error_str.contains("network unreachable") {
            lines.push("  → Network unreachable".to_string());
            lines.push("  → Check your network connection".to_string());
        } else if error_str.contains("reset") {
            lines.push("  → Connection was reset by the server".to_string());
            lines.push("  → The server forcibly closed the connection".to_string());
        } else {
            lines.push(format!("{}", e));
        }
    } else if e.is_request() {
        lines.push("📝 REQUEST ERROR: Failed to build/send request".to_string());

        // Check for specific request issues
        if e.is_body() {
            lines.push("  → Problem with request body".to_string());
        }

        if let Some(source) = e.source() {
            lines.push(format!("{}", source));
        }
    } else if e.is_redirect() {
        lines.push("↪ REDIRECT ERROR: Too many redirects or redirect loop".to_string());
        lines.push("  → The server redirected too many times".to_string());
        lines.push("  → Check for redirect loops in server config".to_string());
    } else if e.is_status() {
        // This shouldn't normally happen here since we handle status in Ok branch
        if let Some(status) = e.status() {
            lines.push(format!("❌ HTTP ERROR: Status {}", status.as_u16()));
            lines.push(format!(
                "{}",
                status.canonical_reason().unwrap_or("Unknown")
            ));
        }
    } else if e.is_decode() {
        lines.push("📦 DECODE ERROR: Failed to decode response".to_string());
        lines.push("  → Response body could not be parsed".to_string());
    } else {
        // Generic error with full details
        lines.push("❓ REQUEST FAILED".to_string());

        let error_str = e.to_string().to_lowercase();

        // Check for TLS/SSL errors
        if error_str.contains("ssl")
            || error_str.contains("tls")
            || error_str.contains("certificate")
        {
            lines.push("  → TLS/SSL error detected".to_string());
            if error_str.contains("certificate") && error_str.contains("expired") {
                lines.push("  → Server certificate may be expired".to_string());
            } else if error_str.contains("self-signed") || error_str.contains("unknown ca") {
                lines.push("  → Server has an untrusted certificate".to_string());
            } else if error_str.contains("handshake") {
                lines.push("  → TLS handshake failed".to_string());
            }
        }

        lines.push(format!("{}", e));
    }

    // Add URL info for context
    lines.push(String::new());
    lines.push(format!("URL: {}", url));

    // Check for common URL issues
    if url.starts_with("https://localhost") || url.starts_with("https://127.0.0.1") {
        lines.push("  ⚠ TIP: Local servers often run on HTTP, not HTTPS".to_string());
    }

    if !url.contains("://") {
        lines.push("  ⚠ TIP: URL may be missing the protocol (http:// or https://)".to_string());
    }

    lines.join("\n")
}

/// Build a request from the given parameters
fn build_request(
    client: &reqwest::Client,
    request: &Request,
    environment: &Option<Environment>,
) -> reqwest::RequestBuilder {
    // Apply environment variable substitution
    let url = if let Some(env) = environment {
        env.substitute(&request.url)
    } else {
        request.url.clone()
    };

    let mut req_builder = match request.method {
        HttpMethod::GET => client.get(&url),
        HttpMethod::POST => client.post(&url),
        HttpMethod::PUT => client.put(&url),
        HttpMethod::PATCH => client.patch(&url),
        HttpMethod::DELETE => client.delete(&url),
    };

    // Add headers
    for header in &request.headers {
        if header.enabled {
            let value = if let Some(env) = environment {
                env.substitute(&header.value)
            } else {
                header.value.clone()
            };
            req_builder = req_builder.header(&header.key, value);
        }
    }

    // Add auth
    match &request.auth {
        AuthType::Bearer(token) => {
            let token = if let Some(env) = environment {
                env.substitute(token)
            } else {
                token.clone()
            };
            req_builder = req_builder.header("Authorization", format!("Bearer {}", token));
        }
        AuthType::Basic { username, password } => {
            let credentials = format!("{}:{}", username, password);
            let encoded = base64::engine::general_purpose::STANDARD.encode(credentials);
            req_builder = req_builder.header("Authorization", format!("Basic {}", encoded));
        }
        AuthType::None => {}
    }

    // Add body
    if request.method.has_body() && !request.body.is_empty() {
        let body = if let Some(env) = environment {
            env.substitute(&request.body)
        } else {
            request.body.clone()
        };
        req_builder = req_builder.body(body);
    }

    req_builder
}

/// Execute an HTTP request and return the response (buffered)
pub async fn execute_request(
    client: &reqwest::Client,
    request: Request,
    environment: Option<Environment>,
    request_id: u64,
) -> NetworkResponse {
    let start = Instant::now();
    let req_builder = build_request(client, &request, &environment);

    let result = req_builder.send().await;
    let elapsed = start.elapsed().as_millis() as u64;

    match result {
        Ok(resp) => {
            let status = resp.status().as_u16();
            match resp.text().await {
                Ok(body) => {
                    let formatted =
                        if let Ok(json) = serde_json::from_str::<serde_json::Value>(&body) {
                            serde_json::to_string_pretty(&json).unwrap_or(body)
                        } else {
                            body
                        };
                    NetworkResponse::Success {
                        id: request_id,
                        status,
                        body: formatted,
                        time_ms: elapsed,
                    }
                }
                Err(e) => NetworkResponse::Error {
                    id: request_id,
                    message: format!("Error reading body: {}", e),
                    time_ms: elapsed,
                },
            }
        }
        Err(e) => {
            let msg = format_request_error(&e, &request.url);
            NetworkResponse::Error {
                id: request_id,
                message: msg,
                time_ms: elapsed,
            }
        }
    }
}

/// Execute an HTTP request with streaming response
pub async fn execute_streaming_request(
    client: &reqwest::Client,
    request: Request,
    environment: Option<Environment>,
    request_id: u64,
    response_tx: mpsc::UnboundedSender<NetworkResponse>,
    mut cancel_rx: oneshot::Receiver<()>,
) {
    let start = Instant::now();
    let req_builder = build_request(client, &request, &environment);

    let result = req_builder.send().await;

    match result {
        Ok(resp) => {
            let status = resp.status().as_u16();
            let mut stream = resp.bytes_stream();
            let mut total_bytes = 0usize;
            let mut body = String::new();

            loop {
                tokio::select! {
                    biased;

                    _ = &mut cancel_rx => {
                        // Request was cancelled
                        return;
                    }
                    chunk = stream.next() => {
                        match chunk {
                            Some(Ok(bytes)) => {
                                total_bytes += bytes.len();
                                if let Ok(text) = String::from_utf8(bytes.to_vec()) {
                                    body.push_str(&text);
                                    let _ = response_tx.send(NetworkResponse::StreamChunk {
                                        id: request_id,
                                        chunk: text,
                                        bytes_received: total_bytes,
                                    });
                                }
                            }
                            Some(Err(e)) => {
                                let _ = response_tx.send(NetworkResponse::Error {
                                    id: request_id,
                                    message: format!("Stream error: {}", e),
                                    time_ms: start.elapsed().as_millis() as u64,
                                });
                                return;
                            }
                            None => {
                                // Stream complete - try to format as JSON
                                let formatted = if let Ok(json) = serde_json::from_str::<serde_json::Value>(&body) {
                                    serde_json::to_string_pretty(&json).unwrap_or(body)
                                } else {
                                    body
                                };

                                // Send final formatted body as success
                                let _ = response_tx.send(NetworkResponse::Success {
                                    id: request_id,
                                    status,
                                    body: formatted,
                                    time_ms: start.elapsed().as_millis() as u64,
                                });
                                return;
                            }
                        }
                    }
                }
            }
        }
        Err(e) => {
            let msg = format_request_error(&e, &request.url);
            let _ = response_tx.send(NetworkResponse::Error {
                id: request_id,
                message: msg,
                time_ms: start.elapsed().as_millis() as u64,
            });
        }
    }
}

/// Execute a GraphQL query
pub async fn execute_graphql(
    client: &reqwest::Client,
    endpoint: String,
    query: String,
    variables: Option<String>,
    headers: Vec<crate::models::Header>,
    auth: crate::models::AuthType,
    request_id: u64,
) -> NetworkResponse {
    use base64::Engine;
    use std::time::Instant;

    let start = Instant::now();

    // Build GraphQL request body
    let body = if let Some(vars) = &variables {
        // Try to parse variables as JSON
        match serde_json::from_str::<serde_json::Value>(vars) {
            Ok(vars_json) => serde_json::json!({
                "query": query,
                "variables": vars_json
            }),
            Err(_) => serde_json::json!({
                "query": query
            }),
        }
    } else {
        serde_json::json!({
            "query": query
        })
    };

    // Build request
    let mut req_builder = client
        .post(&endpoint)
        .header("Content-Type", "application/json")
        .json(&body);

    // Add custom headers
    for header in &headers {
        if header.enabled {
            req_builder = req_builder.header(&header.key, &header.value);
        }
    }

    // Add auth
    match &auth {
        crate::models::AuthType::Bearer(token) => {
            if !token.is_empty() {
                req_builder = req_builder.header("Authorization", format!("Bearer {}", token));
            }
        }
        crate::models::AuthType::Basic { username, password } => {
            let credentials = format!("{}:{}", username, password);
            let encoded = base64::engine::general_purpose::STANDARD.encode(credentials);
            req_builder = req_builder.header("Authorization", format!("Basic {}", encoded));
        }
        crate::models::AuthType::None => {}
    }

    let result = req_builder.send().await;
    let elapsed = start.elapsed().as_millis() as u64;

    match result {
        Ok(resp) => {
            let status = resp.status().as_u16();
            match resp.text().await {
                Ok(body) => {
                    // Pretty-print JSON response
                    let formatted =
                        if let Ok(json) = serde_json::from_str::<serde_json::Value>(&body) {
                            serde_json::to_string_pretty(&json).unwrap_or(body)
                        } else {
                            body
                        };
                    NetworkResponse::Success {
                        id: request_id,
                        status,
                        body: formatted,
                        time_ms: elapsed,
                    }
                }
                Err(e) => NetworkResponse::Error {
                    id: request_id,
                    message: format!("Error reading response: {}", e),
                    time_ms: elapsed,
                },
            }
        }
        Err(e) => {
            let msg = format_request_error(&e, &endpoint);
            NetworkResponse::Error {
                id: request_id,
                message: msg,
                time_ms: elapsed,
            }
        }
    }
}

/// Create an HTTP client with default configuration
pub fn create_client() -> reqwest::Client {
    use std::time::Duration;

    reqwest::Client::builder()
        .timeout(Duration::from_secs(30))
        .build()
        .unwrap_or_else(|_| reqwest::Client::new())
}

/// Create an HTTP client that ignores SSL certificate errors
/// WARNING: Only use for testing environments, not production!
pub fn create_insecure_client() -> reqwest::Client {
    use std::time::Duration;

    reqwest::Client::builder()
        .timeout(Duration::from_secs(30))
        .danger_accept_invalid_certs(true)
        .build()
        .unwrap_or_else(|_| reqwest::Client::new())
}