Store net client in registry instead of app data, remove require context from registry

This commit is contained in:
Filip Tibell 2023-08-20 19:34:35 -05:00
parent 616846c316
commit 029873fd5f
3 changed files with 36 additions and 36 deletions

View file

@ -5,6 +5,8 @@ use mlua::prelude::*;
use hyper::{header::HeaderName, http::HeaderValue, HeaderMap};
use reqwest::{IntoUrl, Method, RequestBuilder};
const REGISTRY_KEY: &str = "NetClient";
pub struct NetClientBuilder {
builder: reqwest::ClientBuilder,
}
@ -44,6 +46,35 @@ impl NetClient {
pub fn request<U: IntoUrl>(&self, method: Method, url: U) -> RequestBuilder {
self.0.request(method, url)
}
pub fn into_registry(self, lua: &Lua) {
lua.set_named_registry_value(REGISTRY_KEY, self)
.expect("Failed to store NetClient in lua registry");
}
pub fn from_registry(lua: &Lua) -> Self {
lua.named_registry_value(REGISTRY_KEY)
.expect("Failed to get NetClient from lua registry")
}
}
impl LuaUserData for NetClient {}
impl<'lua> FromLua<'lua> for NetClient {
fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
if let LuaValue::UserData(ud) = value {
if let Ok(ctx) = ud.borrow::<NetClient>() {
return Ok(ctx.clone());
}
}
unreachable!("NetClient should only be used from registry")
}
}
impl<'lua> From<&'lua Lua> for NetClient {
fn from(value: &'lua Lua) -> Self {
value
.named_registry_value(REGISTRY_KEY)
.expect("Missing require context in lua registry")
}
}

View file

@ -29,6 +29,10 @@ use server::{NetLocalExec, NetService};
use websocket::NetWebSocket;
pub fn create(lua: &'static Lua) -> LuaResult<LuaTable> {
NetClientBuilder::new()
.headers(&[("User-Agent", create_user_agent_header())])?
.build()?
.into_registry(lua);
TableBuilder::new(lua)?
.with_function("jsonEncode", net_json_encode)?
.with_function("jsonDecode", net_json_decode)?
@ -64,20 +68,8 @@ 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::<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
let client = NetClient::from_registry(lua);
let mut request = client.request(config.method, &config.url);
for (query, value) in config.query {
request = request.query(&[(query.to_str()?, value.to_str()?)]);

View file

@ -19,8 +19,6 @@ use crate::lune::{
scheduler::{IntoLuaThread, Scheduler},
};
const REGISTRY_KEY: &str = "RequireContext";
/**
Context containing cached results for all `require` operations.
@ -310,24 +308,3 @@ impl<'lua> RequireContext<'lua> {
result
}
}
impl<'lua> LuaUserData for RequireContext<'lua> {}
impl<'lua> FromLua<'lua> for RequireContext<'lua> {
fn from_lua(value: LuaValue<'lua>, _: &'lua Lua) -> LuaResult<Self> {
if let LuaValue::UserData(ud) = value {
if let Ok(ctx) = ud.borrow::<RequireContext>() {
return Ok(ctx.clone());
}
}
unreachable!("RequireContext should only be used from registry")
}
}
impl<'lua> From<&'lua Lua> for RequireContext<'lua> {
fn from(value: &'lua Lua) -> Self {
value
.named_registry_value(REGISTRY_KEY)
.expect("Missing require context in lua registry")
}
}