2024-03-06 15:42:04 +00:00
|
|
|
use async_trait::async_trait;
|
2024-02-27 16:50:48 +00:00
|
|
|
use std::time::Duration;
|
|
|
|
|
2024-11-15 00:37:52 +00:00
|
|
|
use redis::{AsyncCommands, RedisError};
|
2024-02-27 16:50:48 +00:00
|
|
|
use tracing::{debug, info};
|
|
|
|
|
|
|
|
use super::APL;
|
|
|
|
use crate::AuthData;
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct RedisApl {
|
|
|
|
pub client: redis::Client,
|
|
|
|
pub app_api_base_url: String,
|
|
|
|
}
|
|
|
|
|
2024-11-15 00:37:52 +00:00
|
|
|
#[derive(thiserror::Error, Debug)]
|
|
|
|
pub enum RedisAplError {
|
|
|
|
#[error("Error during redis operation, {0}")]
|
|
|
|
RedisError(#[from] RedisError),
|
|
|
|
#[error("Failed parsing from/to json, {0}")]
|
|
|
|
SerdeJsonDeError(#[from] serde_json::Error),
|
|
|
|
#[error("RedisAPL doesn't support requested feature: {0}")]
|
|
|
|
NotSupported(String),
|
|
|
|
}
|
|
|
|
|
2024-03-06 15:42:04 +00:00
|
|
|
#[async_trait]
|
2024-11-15 00:37:52 +00:00
|
|
|
impl APL<RedisAplError> for RedisApl {
|
|
|
|
async fn get(&self, saleor_api_url: &str) -> Result<AuthData, RedisAplError> {
|
2024-04-11 16:19:22 +00:00
|
|
|
debug!("get()");
|
2024-04-15 17:50:54 +00:00
|
|
|
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
2024-02-27 16:50:48 +00:00
|
|
|
let val: String = conn.get(self.prepare_key(saleor_api_url)).await?;
|
|
|
|
let val: AuthData = serde_json::from_str(&val)?;
|
|
|
|
info!("sucessful get");
|
|
|
|
|
|
|
|
Ok(val)
|
|
|
|
}
|
2024-11-15 00:37:52 +00:00
|
|
|
async fn set(&self, auth_data: AuthData) -> Result<(), RedisAplError> {
|
2024-04-11 16:19:22 +00:00
|
|
|
debug!("set()");
|
2024-04-15 17:50:54 +00:00
|
|
|
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
2024-07-10 21:52:16 +00:00
|
|
|
conn.set::<_, _, String>(
|
2024-02-27 16:50:48 +00:00
|
|
|
self.prepare_key(&auth_data.saleor_api_url),
|
|
|
|
serde_json::to_string(&auth_data)?,
|
|
|
|
)
|
|
|
|
.await?;
|
|
|
|
info!("sucessful set");
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-11-15 00:37:52 +00:00
|
|
|
async fn delete(&self, saleor_api_url: &str) -> Result<(), RedisAplError> {
|
2024-02-27 16:50:48 +00:00
|
|
|
debug!("delete(), {}", saleor_api_url);
|
2024-04-15 17:50:54 +00:00
|
|
|
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
2024-02-27 16:50:48 +00:00
|
|
|
let val: String = conn.get_del(self.prepare_key(saleor_api_url)).await?;
|
|
|
|
|
|
|
|
debug!("sucessful delete(), {}", val);
|
|
|
|
info!("sucessful del");
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-11-15 00:37:52 +00:00
|
|
|
async fn is_ready(&self) -> Result<(), RedisAplError> {
|
2024-02-27 16:50:48 +00:00
|
|
|
debug!("is_ready()");
|
2024-04-15 17:50:54 +00:00
|
|
|
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
2024-02-27 16:50:48 +00:00
|
|
|
let val: String = redis::cmd("INFO")
|
|
|
|
.arg("server")
|
|
|
|
.query_async(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
debug!("sucessful is_ready(), info: {}", val);
|
|
|
|
info!("sucessful is_ready");
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-11-15 00:37:52 +00:00
|
|
|
async fn is_configured(&self) -> Result<(), RedisAplError> {
|
2024-02-27 16:50:48 +00:00
|
|
|
debug!("is_configured()");
|
2024-04-15 17:50:54 +00:00
|
|
|
let mut conn = self.client.get_multiplexed_async_connection().await?;
|
2024-02-27 16:50:48 +00:00
|
|
|
let val: String = redis::cmd("INFO")
|
|
|
|
.arg("server")
|
|
|
|
.query_async(&mut conn)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
debug!("sucessful is_configured(), info: {}", val);
|
|
|
|
info!("sucessful is_configured");
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-11-15 00:37:52 +00:00
|
|
|
async fn get_all(&self) -> Result<Vec<AuthData>, RedisAplError> {
|
|
|
|
Err(RedisAplError::NotSupported(
|
|
|
|
"Redis doens't support getall".to_owned(),
|
|
|
|
))
|
2024-02-27 16:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RedisApl {
|
2024-11-15 00:37:52 +00:00
|
|
|
pub fn new(redis_url: &str, app_api_base_url: &str) -> Result<Self, RedisAplError> {
|
2024-03-18 15:12:39 +00:00
|
|
|
debug!("creating redis apl with {redis_url}...");
|
2024-02-27 16:50:48 +00:00
|
|
|
let client = redis::Client::open(redis_url)?;
|
|
|
|
let mut conn = client.get_connection_with_timeout(Duration::from_secs(3))?;
|
|
|
|
let val: Result<String, redis::RedisError> =
|
|
|
|
redis::cmd("INFO").arg("server").query(&mut conn);
|
|
|
|
|
|
|
|
match val {
|
|
|
|
Ok(_) => Ok(Self {
|
|
|
|
client,
|
2024-03-06 15:42:04 +00:00
|
|
|
app_api_base_url: app_api_base_url.to_owned(),
|
2024-02-27 16:50:48 +00:00
|
|
|
}),
|
2024-11-15 00:37:52 +00:00
|
|
|
Err(e) => Err(e.into()),
|
2024-02-27 16:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn prepare_key(&self, saleor_api_url: &str) -> String {
|
|
|
|
let key = format!("{}:{saleor_api_url}", self.app_api_base_url);
|
|
|
|
key
|
|
|
|
}
|
|
|
|
}
|