saleor-apps-rs/sdk/src/apl/file_apl.rs

88 lines
2.5 KiB
Rust
Raw Normal View History

2024-07-10 21:52:16 +00:00
use std::{
collections::HashMap,
ops::{Deref, DerefMut},
};
2024-03-06 15:42:04 +00:00
2024-07-10 21:52:16 +00:00
use crate::AuthData;
2024-02-27 16:50:48 +00:00
use super::APL;
2024-07-10 21:52:16 +00:00
use anyhow::{anyhow, Result};
2024-03-06 15:42:04 +00:00
use async_trait::async_trait;
2024-07-10 21:52:16 +00:00
use serde::{Deserialize, Serialize};
use tracing::debug;
2024-02-27 16:50:48 +00:00
#[derive(Clone, Debug)]
/**
2024-07-10 21:52:16 +00:00
Only works for this app, can't have multiple apps use same file
2024-02-27 16:50:48 +00:00
*/
pub struct FileApl {
pub path: String,
}
2024-03-06 15:42:04 +00:00
#[async_trait]
2024-02-27 16:50:48 +00:00
impl APL for FileApl {
2024-07-10 21:52:16 +00:00
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(())
2024-02-27 16:50:48 +00:00
}
2024-07-10 21:52:16 +00:00
async fn get(&self, saleor_api_url: &str) -> Result<crate::AuthData> {
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"))
2024-02-27 16:50:48 +00:00
}
2024-07-10 21:52:16 +00:00
2024-02-27 16:50:48 +00:00
async fn get_all(&self) -> Result<Vec<crate::AuthData>> {
2024-07-10 21:52:16 +00:00
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())
2024-02-27 16:50:48 +00:00
}
2024-07-10 21:52:16 +00:00
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(())
2024-02-27 16:50:48 +00:00
}
2024-07-10 21:52:16 +00:00
2024-02-27 16:50:48 +00:00
async fn is_ready(&self) -> Result<()> {
2024-07-10 21:52:16 +00:00
Ok(())
2024-02-27 16:50:48 +00:00
}
2024-07-10 21:52:16 +00:00
2024-02-27 16:50:48 +00:00
async fn is_configured(&self) -> Result<()> {
2024-07-10 21:52:16 +00:00
Ok(())
}
}
#[derive(Debug, Clone, Deserialize, Serialize)]
struct FileStructure(HashMap<String, AuthData>);
impl Deref for FileStructure {
type Target = HashMap<String, AuthData>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for FileStructure {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
2024-02-27 16:50:48 +00:00
}
}