mrapids 0.1.7

Your OpenAPI, but executable
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
// Request configuration runner
use anyhow::{Context, Result};
use colored::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::fs;
use std::path::{Path, PathBuf};

#[derive(Debug, Deserialize, Serialize)]
pub struct RequestConfig {
    pub operation: String,
    pub method: String,
    pub path: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(default)]
    pub headers: HashMap<String, String>,
    #[serde(default)]
    pub params: HashMap<String, String>,
    #[serde(default)]
    pub path_params: HashMap<String, Value>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub body: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expect: Option<ExpectConfig>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub base_url: Option<String>,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ExpectConfig {
    pub status: u16,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub content_type: Option<String>,
}

/// Check if a file is a request config (not an API spec)
pub fn is_request_config(path: &Path) -> bool {
    if let Ok(content) = fs::read_to_string(path) {
        // Quick check - request configs have specific fields
        content.contains("operation:")
            && content.contains("method:")
            && content.contains("path:")
            && !content.contains("openapi:")
            && !content.contains("swagger:")
            && !content.contains("paths:")
    } else {
        false
    }
}

/// Load request configuration from YAML file
pub fn load_request_config(path: &Path) -> Result<RequestConfig> {
    let content = fs::read_to_string(path)
        .with_context(|| format!("Failed to read request config: {}", path.display()))?;

    let config: RequestConfig = serde_yaml::from_str(&content)
        .with_context(|| format!("Failed to parse request config: {}", path.display()))?;

    Ok(config)
}

/// Execute a request from configuration
pub fn execute_request_config(
    config: RequestConfig,
    override_url: Option<String>,
    override_data: Option<String>,
    output_format: &str,
) -> Result<()> {
    println!(
        "⚡ Executing request: {} {}",
        config.method.bright_green(),
        config.path.bright_cyan()
    );

    if let Some(desc) = &config.description {
        println!("   {}", desc.dimmed());
    }

    // Determine base URL
    let base_url = override_url
        .or(config.base_url)
        .or_else(|| {
            // Try to find base URL from environment or default
            std::env::var("API_BASE_URL").ok()
        })
        .unwrap_or_else(|| {
            // Check if we're in a petstore project
            if config.path.contains("/pet")
                || config.path.contains("/store")
                || config.path.contains("/user")
            {
                "https://petstore.swagger.io/v2".to_string()
            } else {
                "http://localhost:8080".to_string()
            }
        });

    // Validate URL for security
    validate_request_url(&base_url)?;

    // Build full URL with path parameter substitution
    let mut url_path = config.path.clone();
    for (param_name, param_value) in &config.path_params {
        let placeholder = format!("{{{}}}", param_name);
        let value_str = match param_value {
            Value::String(s) => s.clone(),
            Value::Number(n) => n.to_string(),
            Value::Bool(b) => b.to_string(),
            _ => param_value.to_string(),
        };
        url_path = url_path.replace(&placeholder, &value_str);
    }

    let full_url = format!("{}{}", base_url.trim_end_matches('/'), url_path);
    println!("🌐 Request URL: {}", full_url.bright_blue());

    // Prepare request
    let client = reqwest::blocking::Client::new();
    let mut request = match config.method.to_uppercase().as_str() {
        "GET" => client.get(&full_url),
        "POST" => client.post(&full_url),
        "PUT" => client.put(&full_url),
        "DELETE" => client.delete(&full_url),
        "PATCH" => client.patch(&full_url),
        "HEAD" => client.head(&full_url),
        _ => {
            return Err(anyhow::anyhow!(
                "Unsupported HTTP method: {}",
                config.method
            ))
        }
    };

    // Add headers
    for (key, value) in &config.headers {
        request = request.header(key, value);
    }

    // Add query parameters
    if !config.params.is_empty() {
        request = request.query(&config.params);
    }

    // Add body if present
    if let Some(body_ref) = &config.body {
        let body_content = if let Some(override_body) = override_data {
            // Check if override starts with @ to indicate file
            if override_body.starts_with('@') {
                let file_path = &override_body[1..];
                let path = if file_path.starts_with('/') {
                    PathBuf::from(file_path)
                } else {
                    std::env::current_dir()?.join(file_path)
                };

                let content = fs::read_to_string(&path)
                    .with_context(|| format!("Failed to read data file: {}", path.display()))?;

                // Strip comment lines if JSON file
                if file_path.ends_with(".json") {
                    content
                        .lines()
                        .filter(|line| !line.trim().starts_with("//"))
                        .collect::<Vec<_>>()
                        .join("\n")
                } else {
                    content
                }
            } else {
                override_body
            }
        } else if body_ref.starts_with("data/") || body_ref.ends_with(".json") {
            // Load from file
            let body_path = if body_ref.starts_with('/') {
                PathBuf::from(body_ref)
            } else {
                std::env::current_dir()?.join(body_ref)
            };

            {
                let content = fs::read_to_string(&body_path).with_context(|| {
                    format!("Failed to read body file: {}", body_path.display())
                })?;
                // Strip comment lines (lines starting with //)
                let cleaned: String = content
                    .lines()
                    .filter(|line| !line.trim().starts_with("//"))
                    .collect::<Vec<_>>()
                    .join("\n");
                cleaned
            }
        } else {
            // Use as inline JSON
            body_ref.clone()
        };

        request = request.body(body_content);
    }

    // Execute request
    println!("🚀 Sending request...");
    let response = request
        .send()
        .with_context(|| format!("Failed to send request to {}", full_url))?;

    let status = response.status();
    let status_code = status.as_u16();

    // Check expectations if present
    if let Some(expect) = &config.expect {
        if status_code != expect.status {
            println!(
                "⚠️  Status mismatch: expected {}, got {}",
                expect.status.to_string().yellow(),
                status_code.to_string().red()
            );
        } else {
            println!(
                "✅ Status: {} {}",
                status_code.to_string().green(),
                status.canonical_reason().unwrap_or("")
            );
        }
    } else {
        let status_color = if status.is_success() {
            status_code.to_string().green()
        } else if status.is_client_error() {
            status_code.to_string().yellow()
        } else {
            status_code.to_string().red()
        };
        println!(
            "📡 Status: {} {}",
            status_color,
            status.canonical_reason().unwrap_or("")
        );
    }

    // Display response
    display_response(response, output_format)?;

    Ok(())
}

/// Display response in requested format
fn display_response(response: reqwest::blocking::Response, format: &str) -> Result<()> {
    let headers = response.headers().clone();
    let content_type = headers
        .get("content-type")
        .and_then(|v| v.to_str().ok())
        .unwrap_or("text/plain");

    println!("📥 Response (Content-Type: {}):", content_type.dimmed());

    let body = response.text()?;

    match format {
        "json" => {
            if content_type.contains("json") {
                if let Ok(json) = serde_json::from_str::<Value>(&body) {
                    println!("{}", serde_json::to_string_pretty(&json)?);
                } else {
                    println!("{}", body);
                }
            } else {
                println!("{}", body);
            }
        }
        "yaml" => {
            if content_type.contains("json") {
                if let Ok(json) = serde_json::from_str::<Value>(&body) {
                    println!("{}", serde_yaml::to_string(&json)?);
                } else {
                    println!("{}", body);
                }
            } else {
                println!("{}", body);
            }
        }
        "pretty" | _ => {
            if content_type.contains("json") {
                if let Ok(json) = serde_json::from_str::<Value>(&body) {
                    print_json_pretty(&json, 0);
                } else {
                    println!("{}", body);
                }
            } else {
                println!("{}", body);
            }
        }
    }

    Ok(())
}

/// Pretty print JSON with colors
pub fn print_json_pretty(value: &Value, indent: usize) {
    let indent_str = "  ".repeat(indent);

    match value {
        Value::Object(map) => {
            println!("{{");
            let entries: Vec<_> = map.iter().collect();
            for (i, (key, val)) in entries.iter().enumerate() {
                print!("{}  {}: ", indent_str, key.bright_blue());
                match val {
                    Value::Object(_) | Value::Array(_) => {
                        print_json_pretty(val, indent + 1);
                    }
                    _ => {
                        print_json_value(val);
                        if i < entries.len() - 1 {
                            println!(",");
                        } else {
                            println!();
                        }
                    }
                }
            }
            print!("{}}}", indent_str);
            if indent > 0 {
                println!(",");
            } else {
                println!();
            }
        }
        Value::Array(arr) => {
            println!("[");
            for (i, val) in arr.iter().enumerate() {
                print!("{}  ", indent_str);
                print_json_pretty(val, indent + 1);
                if i < arr.len() - 1 {
                    println!(",");
                }
            }
            print!("{}]", indent_str);
        }
        _ => print_json_value(value),
    }
}

/// Print a JSON value with color
fn print_json_value(value: &Value) {
    match value {
        Value::String(s) => print!("{}", format!("\"{}\"", s).green()),
        Value::Number(n) => print!("{}", n.to_string().yellow()),
        Value::Bool(b) => print!("{}", b.to_string().cyan()),
        Value::Null => print!("{}", "null".dimmed()),
        _ => print!("{}", value),
    }
}

/// Validate request URL for security
fn validate_request_url(base_url: &str) -> Result<()> {
    let url_lower = base_url.to_lowercase();

    // Block localhost and private IPs
    if url_lower.contains("localhost")
        || url_lower.contains("127.0.0.1")
        || url_lower.contains("0.0.0.0")
        || url_lower.contains("[::1]")
    {
        return Err(anyhow::anyhow!(
            "Access to localhost is not allowed. Use actual hostnames or IPs."
        ));
    }

    // Block private IP ranges
    if url_lower.contains("192.168.")
        || url_lower.contains("10.0.")
        || url_lower.contains("10.1.")
        || url_lower.contains("172.16.")
        || url_lower.contains("172.17.")
        || url_lower.contains("172.18.")
        || url_lower.contains("172.19.")
        || url_lower.contains("172.20.")
        || url_lower.contains("172.21.")
        || url_lower.contains("172.22.")
        || url_lower.contains("172.23.")
        || url_lower.contains("172.24.")
        || url_lower.contains("172.25.")
        || url_lower.contains("172.26.")
        || url_lower.contains("172.27.")
        || url_lower.contains("172.28.")
        || url_lower.contains("172.29.")
        || url_lower.contains("172.30.")
        || url_lower.contains("172.31.")
    {
        return Err(anyhow::anyhow!(
            "Access to private IP ranges is not allowed for security reasons."
        ));
    }

    // Block metadata endpoints
    if url_lower.contains("169.254.169.254")
        || url_lower.contains("metadata.google")
        || url_lower.contains("metadata.goog")
        || url_lower.contains("metadata") && url_lower.contains("internal")
    {
        return Err(anyhow::anyhow!(
            "Access to cloud metadata endpoints is not allowed."
        ));
    }

    // Block file:// and other dangerous schemes
    if !base_url.starts_with("http://") && !base_url.starts_with("https://") {
        return Err(anyhow::anyhow!("Only HTTP and HTTPS URLs are allowed."));
    }

    Ok(())
}