Refactored LoxideLibs
Build Library / Build Library (push) Successful in 5m30s

1. Added compatibility check, LoxideLibs will now abort the entire program if OS is not linux or windows.
2. Changed the python data extraction compatibility layer, LoxideLibs was calling the extract data function twice, causing very slight overhead. I have now changed this so that the function returns a Struct that is now easily extractable via dot method notation.
This commit is contained in:
brotoskyj
2025-12-11 11:47:50 -05:00
parent 0ac3b54d89
commit 59bb97ec4e
6 changed files with 32 additions and 39 deletions
+1 -1
View File
@@ -26,7 +26,7 @@ dependencies = [
[[package]] [[package]]
name = "airlock_libs" name = "airlock_libs"
version = "5.2.0" version = "5.2.1"
dependencies = [ dependencies = [
"chrono", "chrono",
"crossbeam", "crossbeam",
+1 -1
View File
@@ -1,6 +1,6 @@
[package] [package]
name = "airlock_libs" name = "airlock_libs"
version = "5.2.0" version = "5.2.1"
edition = "2024" edition = "2024"
[lib] [lib]
+1 -1
View File
@@ -4,7 +4,7 @@ build-backend = "maturin"
[project] [project]
name = "airlock_libs" name = "airlock_libs"
version = "5.2.0" version = "5.2.1"
description = "Airlock Digital API Wrapper" description = "Airlock Digital API Wrapper"
readme = "README.md" readme = "README.md"
license = { text = "AGPL-3.0-only" } license = { text = "AGPL-3.0-only" }
+17 -23
View File
@@ -64,36 +64,30 @@ pub struct Group {
pub(crate) localip: String, pub(crate) localip: String,
} }
pub enum ExtractedValues { pub struct PyData {
Headers(reqwest::header::HeaderMap), pub headers: reqwest::header::HeaderMap,
BaseUrl(String), pub base_url: String,
} }
pub trait Converter { impl PyData {
fn convert(py: Python<'_>, py_self: &Py<PyAny>, extract_headers: bool) -> ExtractedValues; pub fn extract_data(py: Python<'_>, obj: &Py<PyAny>) -> Self {
} let headers_raw = obj.getattr(py, "headers").unwrap().to_string();
let headers_json = headers_raw.replace('\'', "\"");
pub struct PyData; let parsed: Value = serde_json::from_str(&headers_json).unwrap();
impl Converter for PyData {
fn convert(py: Python<'_>, py_self: &Py<PyAny>, extract_headers: bool) -> ExtractedValues {
if extract_headers {
let headers = py_self.getattr(py, "headers").unwrap().to_string();
let headers_replace = headers.replace('\'', "\"");
let parsed: Value = serde_json::from_str(headers_replace.as_str()).unwrap();
let mut header_map = HeaderMap::new(); let mut header_map = HeaderMap::new();
if let Some(obj) = parsed.as_object() { if let Some(obj) = parsed.as_object() {
for (_key, value) in obj { for (key, val) in obj {
if let Some(v) = value.as_str() { if let Some(v) = val.as_str() {
let val = HeaderValue::from_str(v).unwrap(); let header_name = HeaderName::from_str(key).unwrap();
header_map.insert(HeaderName::from_str("X-APIKey").unwrap(), val); let header_value: HeaderValue = HeaderValue::from_str(v).unwrap();
header_map.insert(header_name, header_value);
} }
} }
} }
ExtractedValues::Headers(header_map) let base_url = obj.getattr(py, "base_url").unwrap().to_string();
} else { Self {
let base_url = py_self.getattr(py, "base_url").unwrap().to_string(); headers: header_map,
ExtractedValues::BaseUrl(base_url) base_url,
} }
} }
} }
+8 -9
View File
@@ -11,14 +11,9 @@ pub fn pull_policy_exec_histories(
exec_types: String, exec_types: String,
days: i64, days: i64,
) -> Py<PyString> { ) -> Py<PyString> {
let headers: HeaderMap = match PyData::convert(py, &py_self, true) { let data = PyData::extract_data(py, &py_self);
ExtractedValues::Headers(h) => h, let headers = data.headers;
ExtractedValues::BaseUrl(_) => std::process::abort(), let base_url = data.base_url;
};
let base_url: String = match PyData::convert(py, &py_self, false) {
ExtractedValues::Headers(_) => std::process::abort(),
ExtractedValues::BaseUrl(b) => b,
};
let handle: thread::JoinHandle<String> = std::thread::spawn(move || { let handle: thread::JoinHandle<String> = std::thread::spawn(move || {
let rt: tokio::runtime::Runtime = match tokio::runtime::Runtime::new() { let rt: tokio::runtime::Runtime = match tokio::runtime::Runtime::new() {
Ok(rt) => rt, Ok(rt) => rt,
@@ -310,7 +305,11 @@ pub fn get_base_directory() -> PathBuf {
.unwrap_or_else(|| home.join("AppData").join("Roaming")); .unwrap_or_else(|| home.join("AppData").join("Roaming"));
appdata.join("Loxide") appdata.join("Loxide")
} }
_ => home.join(".local").join("share").join("Loxide"), "linux" => home.join(".local").join("share").join("Loxide"),
_ => {
println!("{} is currently not compatible with LoxideLibs", os);
std::process::abort();
}
} }
} }
+1 -1
View File
@@ -11,4 +11,4 @@ urllib3==2.5.0
pyperclip==1.11.0 pyperclip==1.11.0
--extra-index-url https://git.racooncity.org/api/packages/brotoskyj/pypi/simple/ --extra-index-url https://git.racooncity.org/api/packages/brotoskyj/pypi/simple/
airlock_libs==5.2.0 airlock_libs==5.2.1