-
-
Notifications
You must be signed in to change notification settings - Fork 196
Expand file tree
/
Copy pathasync_http_reqwest.rs
More file actions
29 lines (24 loc) · 822 Bytes
/
async_http_reqwest.rs
File metadata and controls
29 lines (24 loc) · 822 Bytes
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
use mlua::{ExternalResult, Lua, LuaSerdeExt, Result, Value, chunk};
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<()> {
let lua = Lua::new();
let fetch_json = lua.create_async_function(|lua, uri: String| async move {
let resp = reqwest::get(&uri)
.await
.and_then(|resp| resp.error_for_status())
.into_lua_err()?;
let json = resp.json::<serde_json::Value>().await.into_lua_err()?;
lua.to_value(&json)
})?;
let dbg = lua.create_function(|_, value: Value| {
println!("{value:#?}");
Ok(())
})?;
let f = lua
.load(chunk! {
local res = $fetch_json(...)
$dbg(res)
})
.into_function()?;
f.call_async("https://httpbin.org/anything?arg0=val0").await
}