Fix net client app data

This commit is contained in:
Filip Tibell 2023-08-20 19:13:00 -05:00
parent 3b0f6ebab2
commit 90fce384d0

View file

@ -29,14 +29,6 @@ use server::{NetLocalExec, NetService};
use websocket::NetWebSocket; use websocket::NetWebSocket;
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> { pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
// Create a reusable client for performing our
// web requests and store it in the lua registry,
// allowing us to reuse headers and internal structs
let client = NetClientBuilder::new()
.headers(&[("User-Agent", create_user_agent_header())])?
.build()?;
lua.set_app_data(client);
// Create the global table for net
TableBuilder::new(lua)? TableBuilder::new(lua)?
.with_function("jsonEncode", net_json_encode)? .with_function("jsonEncode", net_json_encode)?
.with_function("jsonDecode", net_json_decode)? .with_function("jsonDecode", net_json_decode)?
@ -72,10 +64,20 @@ async fn net_request<'lua>(lua: &'lua Lua, config: RequestConfig<'lua>) -> LuaRe
where where
'lua: 'static, // FIXME: Get rid of static lifetime bound here 'lua: 'static, // FIXME: Get rid of static lifetime bound here
{ {
// Create a reusable client for performing our
// web requests and store it in the lua registry,
// allowing us to reuse headers and internal structs
let client = match lua.app_data_ref::<NetClient>() {
Some(client) => client,
None => {
let client = NetClientBuilder::new()
.headers(&[("User-Agent", create_user_agent_header())])?
.build()?;
lua.set_app_data(client);
lua.app_data_ref::<NetClient>().unwrap()
}
};
// Create and send the request // Create and send the request
let client = lua
.app_data_ref::<NetClient>()
.expect("Missing net client in lua app data");
let mut request = client.request(config.method, &config.url); let mut request = client.request(config.method, &config.url);
for (query, value) in config.query { for (query, value) in config.query {
request = request.query(&[(query.to_str()?, value.to_str()?)]); request = request.query(&[(query.to_str()?, value.to_str()?)]);