Use app data for net builtin client

This commit is contained in:
Filip Tibell 2023-08-20 18:56:28 -05:00
parent 59aef5c170
commit 59788d9116

View file

@ -35,7 +35,7 @@ pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
let client = NetClientBuilder::new() let client = NetClientBuilder::new()
.headers(&[("User-Agent", create_user_agent_header())])? .headers(&[("User-Agent", create_user_agent_header())])?
.build()?; .build()?;
lua.set_named_registry_value("net.client", client)?; lua.set_app_data(client);
// Create the global table for net // Create the global table for net
TableBuilder::new(lua)? TableBuilder::new(lua)?
.with_function("jsonEncode", net_json_encode)? .with_function("jsonEncode", net_json_encode)?
@ -73,7 +73,9 @@ where
'lua: 'static, // FIXME: Get rid of static lifetime bound here 'lua: 'static, // FIXME: Get rid of static lifetime bound here
{ {
// Create and send the request // Create and send the request
let client: LuaUserDataRef<NetClient> = lua.named_registry_value("net.client")?; 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()?)]);