From 59bb97ec4e871403a8e55d32ede24b0460e717b5 Mon Sep 17 00:00:00 2001 From: brotoskyj Date: Thu, 11 Dec 2025 11:47:50 -0500 Subject: [PATCH] Refactored LoxideLibs 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. --- airlock_libs/Cargo.lock | 2 +- airlock_libs/Cargo.toml | 2 +- airlock_libs/pyproject.toml | 2 +- airlock_libs/src/modules/datatypes.rs | 46 ++++++++++++--------------- airlock_libs/src/services.rs | 17 +++++----- requirements.txt | 2 +- 6 files changed, 32 insertions(+), 39 deletions(-) diff --git a/airlock_libs/Cargo.lock b/airlock_libs/Cargo.lock index 64e4271..e02d86e 100644 --- a/airlock_libs/Cargo.lock +++ b/airlock_libs/Cargo.lock @@ -26,7 +26,7 @@ dependencies = [ [[package]] name = "airlock_libs" -version = "5.2.0" +version = "5.2.1" dependencies = [ "chrono", "crossbeam", diff --git a/airlock_libs/Cargo.toml b/airlock_libs/Cargo.toml index 211f014..040ecf4 100644 --- a/airlock_libs/Cargo.toml +++ b/airlock_libs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "airlock_libs" -version = "5.2.0" +version = "5.2.1" edition = "2024" [lib] diff --git a/airlock_libs/pyproject.toml b/airlock_libs/pyproject.toml index 9c11f5e..2aa44c8 100644 --- a/airlock_libs/pyproject.toml +++ b/airlock_libs/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "maturin" [project] name = "airlock_libs" -version = "5.2.0" +version = "5.2.1" description = "Airlock Digital API Wrapper" readme = "README.md" license = { text = "AGPL-3.0-only" } diff --git a/airlock_libs/src/modules/datatypes.rs b/airlock_libs/src/modules/datatypes.rs index 5df65f6..9b4b351 100644 --- a/airlock_libs/src/modules/datatypes.rs +++ b/airlock_libs/src/modules/datatypes.rs @@ -64,36 +64,30 @@ pub struct Group { pub(crate) localip: String, } -pub enum ExtractedValues { - Headers(reqwest::header::HeaderMap), - BaseUrl(String), +pub struct PyData { + pub headers: reqwest::header::HeaderMap, + pub base_url: String, } -pub trait Converter { - fn convert(py: Python<'_>, py_self: &Py, extract_headers: bool) -> ExtractedValues; -} - -pub struct PyData; - -impl Converter for PyData { - fn convert(py: Python<'_>, py_self: &Py, 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(); - if let Some(obj) = parsed.as_object() { - for (_key, value) in obj { - if let Some(v) = value.as_str() { - let val = HeaderValue::from_str(v).unwrap(); - header_map.insert(HeaderName::from_str("X-APIKey").unwrap(), val); - } +impl PyData { + pub fn extract_data(py: Python<'_>, obj: &Py) -> Self { + let headers_raw = obj.getattr(py, "headers").unwrap().to_string(); + let headers_json = headers_raw.replace('\'', "\""); + let parsed: Value = serde_json::from_str(&headers_json).unwrap(); + let mut header_map = HeaderMap::new(); + if let Some(obj) = parsed.as_object() { + for (key, val) in obj { + if let Some(v) = val.as_str() { + let header_name = HeaderName::from_str(key).unwrap(); + let header_value: HeaderValue = HeaderValue::from_str(v).unwrap(); + header_map.insert(header_name, header_value); } } - ExtractedValues::Headers(header_map) - } else { - let base_url = py_self.getattr(py, "base_url").unwrap().to_string(); - ExtractedValues::BaseUrl(base_url) + } + let base_url = obj.getattr(py, "base_url").unwrap().to_string(); + Self { + headers: header_map, + base_url, } } } diff --git a/airlock_libs/src/services.rs b/airlock_libs/src/services.rs index a0362f9..d54ceed 100644 --- a/airlock_libs/src/services.rs +++ b/airlock_libs/src/services.rs @@ -11,14 +11,9 @@ pub fn pull_policy_exec_histories( exec_types: String, days: i64, ) -> Py { - let headers: HeaderMap = match PyData::convert(py, &py_self, true) { - ExtractedValues::Headers(h) => h, - ExtractedValues::BaseUrl(_) => std::process::abort(), - }; - let base_url: String = match PyData::convert(py, &py_self, false) { - ExtractedValues::Headers(_) => std::process::abort(), - ExtractedValues::BaseUrl(b) => b, - }; + let data = PyData::extract_data(py, &py_self); + let headers = data.headers; + let base_url = data.base_url; let handle: thread::JoinHandle = std::thread::spawn(move || { let rt: tokio::runtime::Runtime = match tokio::runtime::Runtime::new() { Ok(rt) => rt, @@ -310,7 +305,11 @@ pub fn get_base_directory() -> PathBuf { .unwrap_or_else(|| home.join("AppData").join("Roaming")); 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(); + } } } diff --git a/requirements.txt b/requirements.txt index b11687f..71907d6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -11,4 +11,4 @@ urllib3==2.5.0 pyperclip==1.11.0 --extra-index-url https://git.racooncity.org/api/packages/brotoskyj/pypi/simple/ -airlock_libs==5.2.0 \ No newline at end of file +airlock_libs==5.2.1 \ No newline at end of file