use fusabi_host::{ExecutionContext, Result, Value, Error};
use std::collections::HashMap;
use std::sync::Arc;
use crate::safety::SafetyConfig;
pub fn request(args: &[Value], _ctx: &ExecutionContext) -> Result<Value> {
let method = args
.first()
.and_then(|v| v.as_str())
.ok_or_else(|| Error::host_function("net_http.request: missing method argument"))?;
let url = args
.get(1)
.and_then(|v| v.as_str())
.ok_or_else(|| Error::host_function("net_http.request: missing url argument"))?;
let empty_map = HashMap::new();
let headers = args
.get(2)
.and_then(|v| v.as_map())
.unwrap_or(&empty_map);
let empty_options = HashMap::new();
let options = args
.get(3)
.and_then(|v| v.as_map())
.unwrap_or(&empty_options);
let timeout = options
.get("timeout")
.and_then(|v| v.as_int())
.unwrap_or(30000);
let retries = options
.get("retries")
.and_then(|v| v.as_int())
.unwrap_or(0);
let _retry_delay = options
.get("retry_delay")
.and_then(|v| v.as_int())
.unwrap_or(1000);
let _body = options
.get("body")
.and_then(|v| v.as_str());
tracing::info!(
"net_http.request: {} {} (timeout={}ms, retries={}, headers={})",
method, url, timeout, retries, headers.len()
);
let mut response = HashMap::new();
response.insert("status".to_string(), Value::Int(200));
response.insert("body".to_string(), Value::String(format!("Response from {}", url)));
let mut response_headers = HashMap::new();
response_headers.insert("content-type".to_string(), Value::String("application/json".to_string()));
response.insert("headers".to_string(), Value::Map(response_headers));
Ok(Value::Map(response))
}
pub fn download_stream(args: &[Value], _ctx: &ExecutionContext) -> Result<Value> {
let url = args
.first()
.and_then(|v| v.as_str())
.ok_or_else(|| Error::host_function("net_http.download_stream: missing url argument"))?;
let _chunk_size = args
.get(1)
.and_then(|v| v.as_int())
.unwrap_or(8192);
tracing::debug!("net_http.download_stream: starting download from {}", url);
Ok(Value::Int(1001))
}
pub fn upload_stream(args: &[Value], _ctx: &ExecutionContext) -> Result<Value> {
let url = args
.first()
.and_then(|v| v.as_str())
.ok_or_else(|| Error::host_function("net_http.upload_stream: missing url argument"))?;
let _stream_handle = args
.get(1)
.and_then(|v| v.as_int())
.ok_or_else(|| Error::host_function("net_http.upload_stream: missing stream handle"))?;
tracing::debug!("net_http.upload_stream: uploading to {}", url);
let mut response = HashMap::new();
response.insert("status".to_string(), Value::Int(201));
response.insert("body".to_string(), Value::String("Upload complete".to_string()));
Ok(Value::Map(response))
}
pub fn read_stream_chunk(args: &[Value], _ctx: &ExecutionContext) -> Result<Value> {
let _handle = args
.first()
.and_then(|v| v.as_int())
.ok_or_else(|| Error::host_function("net_http.read_stream_chunk: missing handle argument"))?;
Ok(Value::String("Mock chunk data".to_string()))
}
pub fn close_stream(args: &[Value], _ctx: &ExecutionContext) -> Result<Value> {
let handle = args
.first()
.and_then(|v| v.as_int())
.ok_or_else(|| Error::host_function("net_http.close_stream: missing handle argument"))?;
tracing::debug!("net_http.close_stream: closing handle {}", handle);
Ok(Value::Null)
}
pub fn check_request_safety(
_safety: &Arc<SafetyConfig>,
_url: &str,
) -> Result<()> {
Ok(())
}