From 528a391132ab6f4d1e528f787c39182221ce8096 Mon Sep 17 00:00:00 2001 From: djkato Date: Wed, 10 Jul 2024 23:52:16 +0200 Subject: [PATCH] FileApl, refactor --- sdk/Cargo.toml | 3 ++ sdk/src/apl/env_apl.rs | 31 ----------------- sdk/src/apl/file_apl.rs | 75 +++++++++++++++++++++++++++++++++------- sdk/src/apl/redis_apl.rs | 2 +- sdk/src/lib.rs | 2 +- sdk/src/tests/mod.rs | 1 + 6 files changed, 69 insertions(+), 45 deletions(-) delete mode 100644 sdk/src/apl/env_apl.rs create mode 100644 sdk/src/tests/mod.rs diff --git a/sdk/Cargo.toml b/sdk/Cargo.toml index e069ba8..cde2b66 100644 --- a/sdk/Cargo.toml +++ b/sdk/Cargo.toml @@ -63,6 +63,8 @@ features = [ "console", ] +[dev-dependencies] + [features] default = ["middleware", "redis_apl", "webhook_utils", "tracing"] middleware = [ @@ -73,6 +75,7 @@ middleware = [ "dep:http", ] redis_apl = ["dep:redis"] +file_apl = [] webhook_utils = ["dep:http"] tracing = ["dep:tracing", "dep:tracing-subscriber"] bridge = [ diff --git a/sdk/src/apl/env_apl.rs b/sdk/src/apl/env_apl.rs deleted file mode 100644 index 0b508aa..0000000 --- a/sdk/src/apl/env_apl.rs +++ /dev/null @@ -1,31 +0,0 @@ -use super::APL; -use anyhow::Result; -use async_trait::async_trait; - -#[derive(Clone, Debug)] -/** -is not implemented yet! -*/ -pub struct EnvApl {} - -#[async_trait] -impl APL for EnvApl { - async fn set(&self, _auth_data: crate::AuthData) -> Result<()> { - todo!() - } - async fn get(&self, _saleor_api_url: &str) -> Result { - todo!() - } - async fn get_all(&self) -> Result> { - todo!() - } - async fn delete(&self, _saleor_api_url: &str) -> Result<()> { - todo!() - } - async fn is_ready(&self) -> Result<()> { - todo!() - } - async fn is_configured(&self) -> Result<()> { - todo!() - } -} diff --git a/sdk/src/apl/file_apl.rs b/sdk/src/apl/file_apl.rs index d593057..4d20a30 100644 --- a/sdk/src/apl/file_apl.rs +++ b/sdk/src/apl/file_apl.rs @@ -1,13 +1,19 @@ +use std::{ + collections::HashMap, + ops::{Deref, DerefMut}, +}; +use crate::AuthData; use super::APL; -use anyhow::{Result}; +use anyhow::{anyhow, Result}; use async_trait::async_trait; - +use serde::{Deserialize, Serialize}; +use tracing::debug; #[derive(Clone, Debug)] /** -is not implemented yet! + Only works for this app, can't have multiple apps use same file */ pub struct FileApl { pub path: String, @@ -15,22 +21,67 @@ pub struct FileApl { #[async_trait] impl APL for FileApl { - async fn set(&self, _auth_data: crate::AuthData) -> Result<()> { - todo!() + async fn set(&self, auth_data: crate::AuthData) -> Result<()> { + let path = std::path::Path::new(&self.path); + debug!("reading from {:?}", &path); + let mut auths: FileStructure = serde_json::from_str(&std::fs::read_to_string(path)?)?; + + auths.insert(auth_data.saleor_api_url.clone(), auth_data); + + debug!("writing to {:?}", &path); + std::fs::write(path, &serde_json::to_string_pretty(&auths)?.as_bytes())?; + Ok(()) } - async fn get(&self, _saleor_api_url: &str) -> Result { - todo!() + + async fn get(&self, saleor_api_url: &str) -> Result { + let path = std::path::Path::new(&self.path); + debug!("reading from {:?}", &path); + let auth_data: FileStructure = serde_json::from_str(&std::fs::read_to_string(path)?)?; + auth_data + .get(saleor_api_url) + .cloned() + .ok_or(anyhow!("AuthData for {saleor_api_url} not found")) } + async fn get_all(&self) -> Result> { - todo!() + let path = std::path::Path::new(&self.path); + debug!("reading from {:?}", &path); + let auth_data: FileStructure = serde_json::from_str(&std::fs::read_to_string(path)?)?; + Ok(auth_data.0.values().cloned().collect()) } - async fn delete(&self, _saleor_api_url: &str) -> Result<()> { - todo!() + + async fn delete(&self, saleor_api_url: &str) -> Result<()> { + let path = std::path::Path::new(&self.path); + debug!("reading from {:?}", &path); + let mut auths: FileStructure = serde_json::from_str(&std::fs::read_to_string(path)?)?; + auths.remove(saleor_api_url); + + debug!("writing to {:?}", &path); + std::fs::write(path, &serde_json::to_string_pretty(&auths)?.as_bytes())?; + Ok(()) } + async fn is_ready(&self) -> Result<()> { - todo!() + Ok(()) } + async fn is_configured(&self) -> Result<()> { - todo!() + Ok(()) + } +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +struct FileStructure(HashMap); + +impl Deref for FileStructure { + type Target = HashMap; + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for FileStructure { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 } } diff --git a/sdk/src/apl/redis_apl.rs b/sdk/src/apl/redis_apl.rs index f611466..8662864 100644 --- a/sdk/src/apl/redis_apl.rs +++ b/sdk/src/apl/redis_apl.rs @@ -28,7 +28,7 @@ impl APL for RedisApl { async fn set(&self, auth_data: AuthData) -> Result<()> { debug!("set()"); let mut conn = self.client.get_multiplexed_async_connection().await?; - conn.set( + conn.set::<_, _, String>( self.prepare_key(&auth_data.saleor_api_url), serde_json::to_string(&auth_data)?, ) diff --git a/sdk/src/lib.rs b/sdk/src/lib.rs index 99e7621..af09012 100644 --- a/sdk/src/lib.rs +++ b/sdk/src/lib.rs @@ -73,7 +73,7 @@ impl SaleorApp { File => { #[cfg(feature = "file_apl")] return Ok(Box::new(FileApl { - path: "apl.txt".to_owned(), + path: config.apl_url.to_owned(), })); #[cfg(not(feature = "file_apl"))] { diff --git a/sdk/src/tests/mod.rs b/sdk/src/tests/mod.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/sdk/src/tests/mod.rs @@ -0,0 +1 @@ +