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
use crate::app::AppState;
use crate::curl;
use crate::app::state::{WsDirection, WsLogEntry};
use crate::messages::ui_events::AuthField;
use crate::messages::{NetworkCommand, NetworkResponse};
use crate::models::{AuthType, Header, HistoryEntry, Request};

impl AppState {
    // ========================
    // HTTP Method
    // ========================

    pub fn cycle_method(&mut self) {
        if !self.is_loading {
            self.request.method = self.request.method.next();
        }
    }

    /// Toggle whether to ignore SSL certificate errors for this request.
    /// Useful for testing environments with self-signed certificates.
    pub fn toggle_ssl_errors(&mut self) {
        self.request.ignore_ssl_errors = !self.request.ignore_ssl_errors;
    }

    // ========================
    // Response scrolling
    // ========================

    pub fn scroll_up(&mut self) {
        self.response_scroll = self.response_scroll.saturating_sub(1);
    }

    pub fn scroll_down(&mut self) {
        self.response_scroll = self.response_scroll.saturating_add(1);
    }

    // ========================
    // Headers
    // ========================

    pub fn next_header(&mut self) {
        if !self.request.headers.is_empty() {
            self.selected_header = (self.selected_header + 1) % self.request.headers.len();
        }
    }

    pub fn prev_header(&mut self) {
        if !self.request.headers.is_empty() {
            self.selected_header = self
                .selected_header
                .checked_sub(1)
                .unwrap_or(self.request.headers.len() - 1);
        }
    }

    pub fn toggle_header(&mut self) {
        if let Some(header) = self.request.headers.get_mut(self.selected_header) {
            header.enabled = !header.enabled;
        }
    }

    pub fn add_header(&mut self) {
        self.request.headers.push(Header::new("X-Custom", "value"));
        self.selected_header = self.request.headers.len() - 1;
    }

    pub fn delete_header(&mut self) {
        if !self.request.headers.is_empty() {
            self.request.headers.remove(self.selected_header);
            if self.selected_header > 0 {
                self.selected_header -= 1;
            }
        }
    }

    // ========================
    // Auth
    // ========================

    pub fn cycle_auth(&mut self) {
        self.request.auth = match &self.request.auth {
            AuthType::None => AuthType::Bearer(String::new()),
            AuthType::Bearer(_) => AuthType::Basic {
                username: String::new(),
                password: String::new(),
            },
            AuthType::Basic { .. } => AuthType::None,
        };
        self.auth_field = AuthField::Token;
    }

    pub fn next_auth_field(&mut self) {
        if matches!(self.request.auth, AuthType::Basic { .. }) {
            self.auth_field = match self.auth_field {
                AuthField::Username => AuthField::Password,
                AuthField::Password => AuthField::Username,
                _ => AuthField::Username,
            };
            self.cursor_position = self.current_input().len();
        }
    }

    // ========================
    // History
    // ========================

    pub fn history_prev(&mut self) {
        if self.storage.history_len() == 0 {
            return;
        }

        let new_index = match self.history_index {
            Option::None => Some(0),
            Some(i) if i + 1 < self.storage.history_len() => Some(i + 1),
            Some(i) => Some(i),
        };

        if let Some(idx) = new_index {
            if let Some(entry) = self.storage.get_history(idx) {
                self.request = entry.request.clone();
                self.history_index = Some(idx);
                self.cursor_position = self.request.url.len();
            }
        }
    }

    pub fn history_next(&mut self) {
        if let Some(idx) = self.history_index {
            if idx > 0 {
                if let Some(entry) = self.storage.get_history(idx - 1) {
                    self.request = entry.request.clone();
                    self.history_index = Some(idx - 1);
                    self.cursor_position = self.request.url.len();
                }
            } else {
                // Back to newest/empty
                self.request = Request::default();
                self.history_index = None;
                self.cursor_position = self.request.url.len();
            }
        }
    }

    // ========================
    // cURL import/export
    // ========================

    pub fn show_curl_import(&mut self) {
        self.show_curl_import = true;
    }

    pub fn curl_import_char(&mut self, c: char) {
        self.curl_import_buffer.push(c);
    }

    pub fn curl_import_backspace(&mut self) {
        self.curl_import_buffer.pop();
    }

    pub fn import_curl(&mut self) {
        if let Ok(request) = curl::parse_curl(&self.curl_import_buffer) {
            self.request = request;
            self.cursor_position = self.request.url.len();
        }
        self.curl_import_buffer.clear();
        self.show_curl_import = false;
    }

    pub fn cancel_curl_import(&mut self) {
        self.curl_import_buffer.clear();
        self.show_curl_import = false;
    }

    pub fn export_curl(&mut self) {
        self.response.body = curl::to_curl(&self.request);
        self.response.status_code = None;
    }

    // ========================
    // Help popup
    #[allow(dead_code)]
    pub fn prepare_request(&mut self) -> Option<NetworkCommand> {
        if self.is_loading {
            return None;
        }

        self.is_loading = true;
        self.response.body = String::from("Loading...");
        self.response.status_code = None;

        let id = self.next_id();
        self.pending_request_id = Some(id);

        Some(NetworkCommand::ExecuteRequest {
            id,
            request: self.request.clone(),
            environment: self.storage.current_environment().cloned(),
        })
    }

    /// Validate a URL and return error message if invalid
    pub(crate) fn validate_url(&self, url: &str) -> Result<(), String> {
        if url.is_empty() {
            return Err("URL cannot be empty".to_string());
        }

        // Check for basic URL structure
        if !url.starts_with("http://") && !url.starts_with("https://") {
            return Err("URL must start with http:// or https://".to_string());
        }

        // Check for host
        let without_scheme = url.split("://").nth(1).unwrap_or("");
        if without_scheme.is_empty() || without_scheme.starts_with('/') {
            return Err("URL must contain a host".to_string());
        }

        Ok(())
    }

    /// Prepare a streaming request (for large responses with incremental updates)
    pub fn prepare_streaming_request(&mut self) -> Option<NetworkCommand> {
        if self.is_loading {
            return None;
        }

        // Validate URL before sending
        if let Err(error) = self.validate_url(&self.request.url) {
            self.response.body = format!("Invalid URL: {}", error);
            self.response.status_code = None;
            return None;
        }

        self.is_loading = true;
        self.response.body = String::from("Starting request...");
        self.response.status_code = None;
        self.streaming_body.clear();
        self.bytes_received = 0;

        let id = self.next_id();
        self.pending_request_id = Some(id);

        Some(NetworkCommand::ExecuteStreamingRequest {
            id,
            request: self.request.clone(),
            environment: self.storage.current_environment().cloned(),
        })
    }

    /// Cancel the current pending request
    pub fn cancel_request(&mut self) -> Option<NetworkCommand> {
        self.pending_request_id.map(NetworkCommand::CancelRequest)
    }

    // ========================
    // Response handling
    // ========================

    pub fn handle_response(&mut self, response: NetworkResponse) {
        // Only process if it matches the pending request (for HTTP responses)
        let response_id = response.id();
        let is_for_pending = self.pending_request_id == Some(response_id);

        match response {
            NetworkResponse::Success {
                status,
                body,
                time_ms,
                ..
            } => {
                if is_for_pending {
                    self.response.status_code = Some(status);
                    self.response.body = body;
                    self.response.time_ms = time_ms;
                    self.finalize_request();
                }
            }
            NetworkResponse::StreamChunk {
                chunk,
                bytes_received,
                ..
            } => {
                if is_for_pending {
                    // Append chunk to streaming body
                    self.streaming_body.push_str(&chunk);
                    self.bytes_received = bytes_received;
                    // Show streaming progress
                    self.response.body = format!(
                        "Streaming... {} bytes received\n\n{}",
                        bytes_received, &self.streaming_body
                    );
                }
            }
            NetworkResponse::StreamComplete {
                status,
                total_bytes,
                time_ms,
                ..
            } => {
                if is_for_pending {
                    // Format final body as JSON if possible
                    let formatted = if let Ok(json) =
                        serde_json::from_str::<serde_json::Value>(&self.streaming_body)
                    {
                        serde_json::to_string_pretty(&json)
                            .unwrap_or_else(|_| self.streaming_body.clone())
                    } else {
                        self.streaming_body.clone()
                    };

                    self.response.status_code = Some(status);
                    self.response.body = formatted;
                    self.response.time_ms = time_ms;
                    self.bytes_received = total_bytes;
                    self.finalize_request();
                }
            }
            NetworkResponse::Error {
                message, time_ms, ..
            } => {
                if is_for_pending {
                    self.response.status_code = None;
                    self.response.body = message;
                    self.response.time_ms = time_ms;
                    self.finalize_request();
                }
            }
            NetworkResponse::Cancelled { .. } => {
                if is_for_pending {
                    self.response.status_code = None;
                    self.response.body = String::from("Request cancelled");
                    self.response.time_ms = 0;
                    self.is_loading = false;
                    self.pending_request_id = None;
                    self.streaming_body.clear();
                    self.bytes_received = 0;
                }
            }
            // WebSocket responses
            NetworkResponse::WebSocketConnected { id } => {
                if self.ws.connection_id == Some(id) {
                    self.ws.connected = true;
                    self.ws.messages.push(WsLogEntry {
                        direction: WsDirection::System,
                        content: "Connected!".to_string(),
                        timestamp: chrono::Utc::now(),
                    });
                }
            }
            NetworkResponse::WebSocketMessage { id, message } => {
                if self.ws.connection_id == Some(id) {
                    self.ws.messages.push(WsLogEntry {
                        direction: WsDirection::Received,
                        content: message,
                        timestamp: chrono::Utc::now(),
                    });
                }
            }
            NetworkResponse::WebSocketClosed { id } => {
                if self.ws.connection_id == Some(id) {
                    self.ws.connected = false;
                    self.ws.connection_id = None;
                    self.ws.messages.push(WsLogEntry {
                        direction: WsDirection::System,
                        content: "Connection closed".to_string(),
                        timestamp: chrono::Utc::now(),
                    });
                }
            }
            NetworkResponse::WebSocketError { id, error } => {
                if self.ws.connection_id == Some(id) {
                    self.ws.connected = false;
                    self.ws.connection_id = None;
                    self.ws.messages.push(WsLogEntry {
                        direction: WsDirection::System,
                        content: format!("Error: {}", error),
                        timestamp: chrono::Utc::now(),
                    });
                }
            }
        }
    }

    // ========================
    // Tab navigation
    // ========================


    /// Finalize a completed request (add to history, reset state)
    pub(crate) fn finalize_request(&mut self) {
        self.is_loading = false;
        self.pending_request_id = None;
        self.response_scroll = 0;
        self.streaming_body.clear();
        self.bytes_received = 0;

        // Add to history
        let entry = HistoryEntry {
            request: self.request.clone(),
            response: self.response.clone(),
            timestamp: chrono::Utc::now(),
        };
        self.storage.add_to_history(entry);
        self.history_index = None;
    }
}