feat: update fetch
This commit is contained in:
@@ -1,9 +1,13 @@
|
||||
extern crate core;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::io::{ErrorKind, Read, Write};
|
||||
use std::time::Duration;
|
||||
|
||||
use anyhow::Result;
|
||||
use reqwest::Method;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::{Map, Number, Value};
|
||||
use wasmtime::{Config, Engine, Instance, Linker, Module, Store};
|
||||
|
||||
wit_bindgen_wasmtime::export!("../container.wit");
|
||||
@@ -22,6 +26,13 @@ impl FetchResult {
|
||||
}
|
||||
}
|
||||
|
||||
fn success(result: String) -> Self {
|
||||
FetchResult {
|
||||
result: Some(result),
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_json(&self) -> String {
|
||||
serde_json::to_string(&self).expect("JSResult to json error")
|
||||
}
|
||||
@@ -34,14 +45,9 @@ impl Into<String> for FetchResult {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
struct JsResult {
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl JsResult {
|
||||
fn to_json(&self) -> String {
|
||||
serde_json::to_string(&self).expect("JSResult to json error")
|
||||
}
|
||||
struct FetchParams {
|
||||
url: String,
|
||||
options: Option<FetchOptions>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
@@ -51,12 +57,6 @@ struct FetchOptions {
|
||||
body: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Serialize, Deserialize)]
|
||||
struct FetchParams {
|
||||
url: String,
|
||||
options: Option<FetchOptions>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MyContainer;
|
||||
|
||||
@@ -107,22 +107,49 @@ impl container::Container for MyContainer {
|
||||
}
|
||||
}
|
||||
|
||||
let fetch_response = match request_builder.send() {
|
||||
let mut fetch_response = match request_builder.send() {
|
||||
Err(e) => return FetchResult::fail(format!("Send request failed: {}", e)).to_json(),
|
||||
Ok(fetch_response) => fetch_response,
|
||||
};
|
||||
|
||||
let status = fetch_response.status().as_u16();
|
||||
let headers = fetch_response.headers();
|
||||
let body = fetch_response.bytes();
|
||||
let headers = fetch_response.headers().clone();
|
||||
|
||||
// FetchResult {
|
||||
// error: None,
|
||||
// result: Some(JsResult {
|
||||
// message: format!("fetched: {:?}", r.text()),
|
||||
// }.to_json()),
|
||||
// }.to_json()
|
||||
"".to_string()
|
||||
let mut body_buff = Vec::new();
|
||||
let mut buff = [0_u8; 1024];
|
||||
loop {
|
||||
match fetch_response.read(&mut buff) {
|
||||
Err(e) if e.kind() == ErrorKind::Interrupted => continue,
|
||||
Err(e) => return FetchResult::fail(format!("Fetch response failed: {}", e)).to_json(),
|
||||
Ok(len) => if len == 0 {
|
||||
break;
|
||||
} else {
|
||||
if let Err(e) = body_buff.write_all(&buff[0..len]) {
|
||||
return FetchResult::fail(format!("Fetch response failed: {}", e)).to_json();
|
||||
}
|
||||
if body_buff.len() > 1024 * 1024 {
|
||||
return FetchResult::fail(format!("Fetch response too large, {} more than 1MB", body_buff.len())).to_json();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut result_map = Map::new();
|
||||
result_map.insert("status".to_string(), Value::Number(Number::from(status)));
|
||||
let mut header_map = Map::new();
|
||||
for (k, v) in headers {
|
||||
let header_key = k.map(|n| n.as_str().to_string()).unwrap_or_else(|| "".to_string());
|
||||
let header_value = match v.to_str() {
|
||||
Err(_) => continue,
|
||||
Ok(v) => v.to_string(),
|
||||
};
|
||||
header_map.insert(header_key, Value::String(header_value));
|
||||
}
|
||||
result_map.insert("headers".to_string(), Value::Object(header_map));
|
||||
result_map.insert("body".to_string(), Value::String(String::from_utf8_lossy(&body_buff).to_string()));
|
||||
|
||||
|
||||
FetchResult::success(format!("{}", Value::Object(result_map))).to_json()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,22 +165,30 @@ fn main() -> Result<()> {
|
||||
)?;
|
||||
let a = exports.eval_javascript(&mut store, r##"
|
||||
function hi(name) { return "hi: " + name; }
|
||||
let a = [];
|
||||
a.push(fetch('https://hatter.ink/util/print_request.action'));
|
||||
for (let i = 0; i < 3; i++) { a.push(i); }
|
||||
a.push({
|
||||
id: 1, name: 'hatter'
|
||||
});
|
||||
a.push(hi('001'));
|
||||
a.push(hi('002'));
|
||||
// JSON.stringify(a)
|
||||
a
|
||||
// let a = [];
|
||||
// a.push(fetch('https://hatter.ink/util/print_request.action'));
|
||||
// for (let i = 0; i < 3; i++) { a.push(i); }
|
||||
// a.push({
|
||||
// id: 1, name: 'hatter'
|
||||
// });
|
||||
// a.push(hi('001'));
|
||||
// a.push(hi('002'));
|
||||
let a = fetch('https://hatter.ink/util/print_request.action');
|
||||
JSON.stringify(a)
|
||||
// a
|
||||
"##);
|
||||
let a = a.expect("error");
|
||||
let a: Value = serde_json::from_str(&a).expect("error");
|
||||
let a = match a {
|
||||
Value::String(a) => a,
|
||||
_ => panic!("error"),
|
||||
};
|
||||
|
||||
match a {
|
||||
Ok(a) => println!("Hello, world! {:?}", a),
|
||||
Err(e) => println!("ERROR: {}", e),
|
||||
}
|
||||
// println!(">>>>>>> {}", a);
|
||||
let v: Value = serde_json::from_str(&a).expect("errorr");
|
||||
let p = serde_json::to_string_pretty(&v).expect("error");
|
||||
// println!("------- {}", v);
|
||||
println!("{}", p);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user