use serde_json::Value;
use super::VtaClient;
use crate::error::VtaError;
use crate::protocols::app_state::{
AppStateDeleteBody, AppStateGetBody, AppStateGetManyBody, AppStateListBody, AppStatePutBody,
AppStatePutManyBody, AppStateWrite, PutManyMode,
};
use crate::trust_tasks;
const APP_STATE_TT_TIMEOUT: u64 = 30;
fn body(value: impl serde::Serialize) -> Result<Value, VtaError> {
serde_json::to_value(value)
.map_err(|e| VtaError::Validation(format!("encode app-state payload: {e}")))
}
impl VtaClient {
pub async fn app_state_get(
&self,
context_id: &str,
namespace: &str,
key: &str,
include_deleted: bool,
) -> Result<Value, VtaError> {
let payload = body(AppStateGetBody {
context_id: context_id.to_string(),
namespace: namespace.to_string(),
key: key.to_string(),
include_deleted: include_deleted.then_some(true),
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_GET_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
pub async fn app_state_put(
&self,
context_id: &str,
namespace: &str,
key: &str,
value: Value,
expected_version: Option<u64>,
) -> Result<Value, VtaError> {
let payload = body(AppStatePutBody {
context_id: context_id.to_string(),
namespace: namespace.to_string(),
key: key.to_string(),
value: Some(value),
merge_patch: None,
expected_version,
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_PUT_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
pub async fn app_state_patch(
&self,
context_id: &str,
namespace: &str,
key: &str,
patch: Value,
expected_version: Option<u64>,
) -> Result<Value, VtaError> {
let payload = body(AppStatePutBody {
context_id: context_id.to_string(),
namespace: namespace.to_string(),
key: key.to_string(),
value: None,
merge_patch: Some(patch),
expected_version,
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_PUT_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
pub async fn app_state_list(
&self,
context_id: &str,
namespace: Option<&str>,
prefix: Option<&str>,
include_values: bool,
page_size: Option<usize>,
cursor: Option<&str>,
) -> Result<Value, VtaError> {
let payload = body(AppStateListBody {
context_id: context_id.to_string(),
namespace: namespace.map(str::to_string),
prefix: prefix.map(str::to_string),
since_version: None,
include_values: include_values.then_some(true),
include_deleted: None,
page_size,
cursor: cursor.map(str::to_string),
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_LIST_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
pub async fn app_state_changes_since(
&self,
context_id: &str,
namespace: &str,
since_version: u64,
prefix: Option<&str>,
include_values: bool,
page_size: Option<usize>,
cursor: Option<&str>,
) -> Result<Value, VtaError> {
let payload = body(AppStateListBody {
context_id: context_id.to_string(),
namespace: Some(namespace.to_string()),
prefix: prefix.map(str::to_string),
since_version: Some(since_version),
include_values: include_values.then_some(true),
include_deleted: None,
page_size,
cursor: cursor.map(str::to_string),
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_LIST_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
pub async fn app_state_delete(
&self,
context_id: &str,
namespace: &str,
key: &str,
expected_version: Option<u64>,
) -> Result<Value, VtaError> {
let payload = body(AppStateDeleteBody {
context_id: context_id.to_string(),
namespace: namespace.to_string(),
key: key.to_string(),
expected_version,
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_DELETE_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
pub async fn app_state_get_many(
&self,
context_id: &str,
namespace: &str,
keys: &[String],
include_deleted: bool,
) -> Result<Value, VtaError> {
let payload = body(AppStateGetManyBody {
context_id: context_id.to_string(),
namespace: namespace.to_string(),
keys: keys.to_vec(),
include_deleted: include_deleted.then_some(true),
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_GET_MANY_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
pub async fn app_state_put_many(
&self,
context_id: &str,
namespace: &str,
writes: Vec<AppStateWrite>,
mode: PutManyMode,
) -> Result<Value, VtaError> {
let payload = body(AppStatePutManyBody {
context_id: context_id.to_string(),
namespace: namespace.to_string(),
mode: Some(mode),
writes,
ext: None,
})?;
self.dispatch_trust_task(
trust_tasks::TASK_VTA_APP_STATE_PUT_MANY_1_0,
payload,
APP_STATE_TT_TIMEOUT,
)
.await
}
}