fauna-api 1.0.858

The Fauna SDK for Rust: accounts, Drive, groups, calendar, chats, payments, Parrot and live rooms.
Documentation
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};

use crate::authentification::{Credentials, PartialProfile, Profile};
use crate::error::Result;
use crate::http::{client, ok};
use crate::integrations::Platform;

/// One of an account's API keys as an admin sees it: its id, never the key itself. `balance`
/// is money paid in and `credit` what Fauna gave, both in dollars.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct AccountKey {
    pub id: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    pub balance: f64,
    pub credit: f64,
}

impl AccountKey {
    pub fn from_json(json: &Value) -> AccountKey {
        AccountKey {
            id: match &json["id"] {
                Value::String(id) => id.clone(),
                id => id.to_string(),
            },
            name: json["name"].as_str().map(str::to_string),
            balance: json["balance"].as_f64().unwrap_or(0.0),
            credit: json["credit"].as_f64().unwrap_or(0.0),
        }
    }
}

/// An account as an admin sees it: profile, verification, permissions and
/// balances. No secrets. `balance` is money paid in and `credit` what Fauna
/// gave, both in dollars. `banned` is whether an admin banned it.
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Account {
    pub email: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub display_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub icon: Option<String>,
    pub phone: String,
    pub email_verified: bool,
    pub phone_verified: bool,
    pub permissions: Vec<String>,
    pub integrations: Vec<Platform>,
    pub balance: f64,
    pub credit: f64,
    pub api_keys: Vec<AccountKey>,
    pub banned: bool,
}

impl Account {
    pub fn from_json(json: &Value) -> Account {
        let list = |key: &str| json[key].as_array().cloned().unwrap_or_default();
        Account {
            email: match &json["email"] {
                Value::String(email) => email.clone(),
                email => email.to_string(),
            },
            display_name: json["displayName"].as_str().map(str::to_string),
            icon: json["icon"].as_str().map(str::to_string),
            phone: json["phone"].as_str().unwrap_or_default().to_string(),
            email_verified: json["emailVerified"].as_bool() == Some(true),
            phone_verified: json["phoneVerified"].as_bool() == Some(true),
            permissions: list("permissions")
                .iter()
                .map(|permission| match permission {
                    Value::String(permission) => permission.clone(),
                    permission => permission.to_string(),
                })
                .collect(),
            integrations: list("integrations")
                .into_iter()
                .filter_map(|platform| serde_json::from_value(platform).ok())
                .collect(),
            balance: json["balance"].as_f64().unwrap_or(0.0),
            credit: json["credit"].as_f64().unwrap_or(0.0),
            api_keys: list("apiKeys").iter().map(AccountKey::from_json).collect(),
            banned: json["banned"].as_bool() == Some(true),
        }
    }
}

pub async fn query(credentials: &Credentials, profile: &PartialProfile) -> Result<Vec<Profile>> {
    let response = client()
        .post(format!("{}/credentials/query", crate::get_url()))
        .header("Authorization", format!("Token {}", credentials.token))
        .json(&json!({ "email": profile.email, "displayName": profile.display_name }))
        .send()
        .await?;
    let json: Value = ok(response).await?.json().await?;
    Ok(serde_json::from_value(
        json.get("accounts").cloned().unwrap_or(json),
    )?)
}