use js_sys::{Object, Reflect};
use wasm_bindgen::prelude::*;
pub(crate) async fn do_http_fetch(worker: web_sys::Worker, id: i32, url: String) {
let Some((signer, _)) = crate::app::chat::credit_signer().await else {
post_http_result(&worker, id, None);
return;
};
let now = (js_sys::Date::now() / 1000.0) as u64;
let token = crate::registry::proxy_auth_token(&signer, now, "fetch");
let endpoint = format!(
"{}api/fetch",
crate::registry::CREDIT_PROXY_URL
);
let send = async {
let resp = reqwest::Client::new()
.post(&endpoint)
.header("content-type", "application/json")
.header("x-goog-api-key", token)
.json(&serde_json::json!({ "url": url }))
.send()
.await
.map_err(|e| format!("proxy request: {e}"))?;
let status = resp.status();
let body = resp
.json::<serde_json::Value>()
.await
.map_err(|e| format!("proxy response decode: {e}"))?;
Ok::<_, String>((status, body))
};
match crate::app::net::with_timeout(20_000, send).await {
Ok(Ok((status, body))) if status.is_success() => {
let upstream = body.get("status").and_then(|v| v.as_i64()).unwrap_or(0) as i32;
let text = body.get("body").and_then(|v| v.as_str()).unwrap_or("");
post_http_result_ok(&worker, id, upstream, text);
}
_ => post_http_result(&worker, id, None),
}
}
fn post_http_result_ok(worker: &web_sys::Worker, id: i32, status: i32, body: &str) {
let msg = Object::new();
let _ = Reflect::set(&msg, &JsValue::from_str("type"), &JsValue::from_str("http_result"));
let _ = Reflect::set(&msg, &JsValue::from_str("id"), &JsValue::from_f64(id as f64));
let _ = Reflect::set(&msg, &JsValue::from_str("status"), &JsValue::from_f64(status as f64));
let _ = Reflect::set(&msg, &JsValue::from_str("body"), &JsValue::from_str(body));
let _ = worker.post_message(&msg);
}
fn post_http_result(worker: &web_sys::Worker, id: i32, ok: Option<(i32, &str)>) {
match ok {
Some((status, body)) => post_http_result_ok(worker, id, status, body),
None => {
let msg = Object::new();
let _ = Reflect::set(&msg, &JsValue::from_str("type"), &JsValue::from_str("http_result"));
let _ = Reflect::set(&msg, &JsValue::from_str("id"), &JsValue::from_f64(id as f64));
let _ = Reflect::set(&msg, &JsValue::from_str("error"), &JsValue::TRUE);
let _ = worker.post_message(&msg);
}
}
}