mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Fix net client app data
This commit is contained in:
parent
3b0f6ebab2
commit
90fce384d0
1 changed files with 13 additions and 11 deletions
|
@ -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()?)]);
|
||||||
|
|
Loading…
Reference in a new issue