use serde::Deserialize;
use crate::account::holdings::account::Lookup;
use crate::client::{ApiError, Call, Transport, read};
use crate::utils::ids::whole;
use crate::utils::lenient::{Done, list, text};
use crate::utils::{AccountIndex, ApiKeyIndex};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Default, Deserialize)]
pub struct SubAccountMetadata {
#[serde(default, deserialize_with = "text")]
pub color: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct AccountMetadata {
pub account_index: AccountIndex,
#[serde(default, deserialize_with = "text")]
pub name: String,
#[serde(default, deserialize_with = "text")]
pub description: String,
#[serde(default)]
pub can_invite: bool,
#[serde(default, deserialize_with = "text")]
pub referral_points_percentage: String,
#[serde(default, deserialize_with = "whole")]
pub created_at: u64,
#[serde(default)]
pub can_rfq: bool,
#[serde(default, deserialize_with = "list")]
pub can_rfq_market_ids: Vec<String>,
#[serde(default)]
pub metadata: SubAccountMetadata,
#[serde(default)]
pub agent_enabled: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
pub struct AccountMetadataPage {
#[serde(default, deserialize_with = "list")]
pub account_metadatas: Vec<AccountMetadata>,
#[serde(default)]
pub next_cursor: Option<String>,
}
#[must_use]
pub fn account_metadata_call(lookup: Lookup, cursor: Option<&str>) -> Call {
Call::get("accountMetadata")
.field("by", lookup.by())
.field("value", lookup.value())
.maybe("cursor", cursor)
.authed()
}
pub async fn account_metadata(
transport: &impl Transport,
lookup: Lookup,
cursor: Option<&str>,
) -> Result<AccountMetadataPage, ApiError> {
read(transport, account_metadata_call(lookup, cursor)).await
}
#[must_use]
pub fn set_account_metadata_call(
master: AccountIndex,
target: AccountIndex,
slot: ApiKeyIndex,
metadata: &str,
) -> Call {
let body = serde_json::json!({
"master_account_index": master.get(),
"target_account_index": target.get(),
"api_key_index": slot.get(),
"metadata": metadata,
});
Call::post("setAccountMetadata").with_json(body.to_string()).authed()
}
pub async fn set_account_metadata(
transport: &impl Transport,
master: AccountIndex,
target: AccountIndex,
slot: ApiKeyIndex,
metadata: &str,
) -> Result<(), ApiError> {
read::<Done>(transport, set_account_metadata_call(master, target, slot, metadata)).await.map(drop)
}
#[must_use]
pub fn ack_notification_call(account: AccountIndex, notification_id: &str) -> Call {
Call::post("notification/ack").field("notif_id", notification_id).field("account_index", account).authed()
}
pub async fn ack_notification(
transport: &impl Transport,
account: AccountIndex,
notification_id: &str,
) -> Result<(), ApiError> {
read::<Done>(transport, ack_notification_call(account, notification_id)).await.map(drop)
}
#[cfg(test)]
#[path = "tests/metadata.rs"]
mod tests;