use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Serialize, Deserialize)]
pub struct CommandResponse {
#[serde(rename = "schemaVersion")]
pub schema_version: u32,
#[serde(rename = "type")]
pub response_type: String,
pub ok: bool,
pub response: Value,
pub meta: CommandMeta,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CommandMeta {
pub profile_name: Option<String>,
pub team_id: String,
pub user_id: String,
pub method: String,
pub command: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub token_type: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub idempotency_key: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub idempotency_status: Option<String>,
}
impl CommandResponse {
pub fn new(
response: Value,
profile_name: Option<String>,
team_id: String,
user_id: String,
method: String,
command: String,
) -> Self {
let ok = response
.as_object()
.and_then(|obj| obj.get("ok"))
.and_then(|v| v.as_bool())
.unwrap_or(true);
let response_type = method.clone();
Self {
schema_version: 1,
response_type,
ok,
response,
meta: CommandMeta {
profile_name,
team_id,
user_id,
method,
command,
token_type: None,
idempotency_key: None,
idempotency_status: None,
},
}
}
pub fn with_token_type(
response: Value,
profile_name: Option<String>,
team_id: String,
user_id: String,
method: String,
command: String,
token_type: Option<String>,
) -> Self {
let ok = response
.as_object()
.and_then(|obj| obj.get("ok"))
.and_then(|v| v.as_bool())
.unwrap_or(true);
let response_type = method.clone();
Self {
schema_version: 1,
response_type,
ok,
response,
meta: CommandMeta {
profile_name,
team_id,
user_id,
method,
command,
token_type,
idempotency_key: None,
idempotency_status: None,
},
}
}
pub fn with_idempotency(mut self, key: String, status: String) -> Self {
self.meta.idempotency_key = Some(key);
self.meta.idempotency_status = Some(status);
self
}
}