use std::collections::HashMap;
use shuttle_core::{Amount, PublicKey};
use error::Result;
use client::{self, InnerClient};
use link::Link;
use deserialize;
#[derive(Debug, Clone)]
pub struct AccountsRequest {
inner: InnerClient,
}
impl AccountsRequest {
pub fn new(inner: InnerClient) -> AccountsRequest {
AccountsRequest { inner }
}
pub fn account_id(self, account_id: &PublicKey) -> Result<AccountIdRequest> {
AccountIdRequest::new(self.inner, &account_id)
}
}
#[derive(Debug, Clone)]
pub struct AccountIdRequest {
inner: InnerClient,
account_id: String,
}
impl AccountIdRequest {
pub fn new(inner: InnerClient, key: &PublicKey) -> Result<AccountIdRequest> {
let account_id = key.account_id()?;
Ok(AccountIdRequest { inner, account_id })
}
pub fn send(&self) -> Result<AccountIdResponse> {
client::parse_response(&mut self.inner.get(&["accounts", &self.account_id])?.send()?)
}
}
#[derive(Debug, Clone, Deserialize)]
pub struct AccountIdResponse {
#[serde(rename = "_links")] links: AccountIdLinks,
id: String,
paging_token: String,
account_id: String,
#[serde(deserialize_with = "deserialize::from_str")]
pub sequence: u64,
subentry_count: u64,
thresholds: Thresholds,
flags: Flags,
balances: Vec<Balance>,
signers: Vec<Signer>,
data: HashMap<String, String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct AccountIdLinks {
#[serde(rename = "self")] self_: Link,
pub transactions: Link,
pub operations: Link,
pub payments: Link,
pub effects: Link,
pub offers: Link,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Thresholds {
low_threshold: u64,
med_threshold: u64,
high_threshold: u64,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Flags {
auth_required: bool,
auth_revocable: bool,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Balance {
#[serde(deserialize_with = "deserialize::from_str")] balance: Amount,
limit: Option<String>,
asset_type: String,
asset_code: Option<String>,
asset_issuer: Option<String>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct Signer {
#[serde(deserialize_with = "deserialize::account_id")] public_key: PublicKey,
weight: u64,
}