2021-06-19 09:41:48 -04:00
|
|
|
use mlua::{chunk, ExternalResult, Lua, LuaSerdeExt, Result};
|
2020-12-27 20:10:47 -05:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
let lua = Lua::new();
|
2021-06-19 09:41:48 -04:00
|
|
|
|
|
|
|
let null = lua.null();
|
2020-12-27 20:10:47 -05:00
|
|
|
|
|
|
|
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())
|
2023-01-06 16:35:15 -05:00
|
|
|
.into_lua_err()?;
|
|
|
|
let json = resp.json::<serde_json::Value>().await.into_lua_err()?;
|
2020-12-27 20:10:47 -05:00
|
|
|
lua.to_value(&json)
|
|
|
|
})?;
|
|
|
|
|
|
|
|
let f = lua
|
2021-06-19 09:41:48 -04:00
|
|
|
.load(chunk! {
|
2020-12-27 20:10:47 -05:00
|
|
|
function print_r(t, indent)
|
2021-06-19 09:41:48 -04:00
|
|
|
local indent = indent or ""
|
2020-12-27 20:10:47 -05:00
|
|
|
for k, v in pairs(t) do
|
|
|
|
io.write(indent, tostring(k))
|
2021-06-19 09:41:48 -04:00
|
|
|
if type(v) == "table" then io.write(":\n") print_r(v, indent.." ")
|
|
|
|
else io.write(": ", v == $null and "null" or tostring(v), "\n") end
|
2020-12-27 20:10:47 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-19 09:41:48 -04:00
|
|
|
local res = $fetch_json(...)
|
2020-12-27 20:10:47 -05:00
|
|
|
print_r(res)
|
2021-06-19 09:41:48 -04:00
|
|
|
})
|
2020-12-27 20:10:47 -05:00
|
|
|
.into_function()?;
|
|
|
|
|
|
|
|
f.call_async("https://httpbin.org/anything?arg0=val0").await
|
|
|
|
}
|