diff --git a/src/lune/builtins/net/mod.rs b/src/lune/builtins/net/mod.rs index c06d0f1..1fe2da7 100644 --- a/src/lune/builtins/net/mod.rs +++ b/src/lune/builtins/net/mod.rs @@ -29,14 +29,6 @@ use server::{NetLocalExec, NetService}; use websocket::NetWebSocket; pub fn create(lua: &'static Lua) -> LuaResult { - // 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)? .with_function("jsonEncode", net_json_encode)? .with_function("jsonDecode", net_json_decode)? @@ -72,10 +64,20 @@ async fn net_request<'lua>(lua: &'lua Lua, config: RequestConfig<'lua>) -> LuaRe where '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::() { + 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::().unwrap() + } + }; // Create and send the request - let client = lua - .app_data_ref::() - .expect("Missing net client in lua app data"); let mut request = client.request(config.method, &config.url); for (query, value) in config.query { request = request.query(&[(query.to_str()?, value.to_str()?)]);