fauna-api 1.0.871

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

use crate::authentification::{Credentials, CredentialsOrApiKey};
use crate::error::Result;
use crate::groups::Group;
use crate::http::{client, ok};

/// A secret in a person's or a group's store, used by name: a password, an API key, anything an
/// agent needs without ever seeing it. Its value is never returned. A Termite repository
/// switches on the secrets its machines get (`termite::versioning::Repository::set_secret`).
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Secret {
    pub id: u64,
    /// Like an environment variable: capital letters, digits and underscores, starting with a
    /// letter. Programs on a machine see it under this name, holding a stand in.
    pub name: String,
    /// What it is, which the agent reads.
    #[serde(default)]
    pub description: String,
    /// Sites it can be typed into, as hosts. Empty means any.
    #[serde(default)]
    pub sites: Vec<String>,
    /// API hosts programs can send it to. Empty means programs can't use it.
    #[serde(default)]
    pub hosts: Vec<String>,
    pub updated_at: DateTime<Utc>,
}

impl Secret {
    pub fn from_json(json: &serde_json::Value) -> Option<Secret> {
        serde_json::from_value(json.clone()).ok()
    }
}

/// A secret to add, or what to replace in the one by that name.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct Draft {
    pub name: String,
    /// At most 8 KB. Required for a new secret; left out, a replace keeps the value.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sites: Option<Vec<String>>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hosts: Option<Vec<String>>,
}

/// Your secrets, or a group's (any member), by name: never their values.
pub async fn list(
    authorization: &CredentialsOrApiKey,
    group: Option<&Group>,
) -> Result<Vec<Secret>> {
    let mut request = client()
        .get(format!("{}/secrets", crate::get_url()))
        .header(
            "Authorization",
            match authorization {
                CredentialsOrApiKey::Credentials(credentials) => {
                    format!("Token {}", credentials.token)
                }
                CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
            },
        );
    if let Some(group) = group {
        request = request.header("Group", &group.id);
    }
    let response = request.send().await?;
    Ok(ok(response).await?.json().await?)
}

/// Adds a secret to your store, or a group's (its direct admins), or replaces the one by that
/// name.
pub async fn set(
    credentials: &Credentials,
    draft: &Draft,
    group: Option<&Group>,
) -> Result<Secret> {
    let mut request = client()
        .post(format!("{}/secrets", crate::get_url()))
        .header("Authorization", format!("Token {}", credentials.token))
        .json(draft);
    if let Some(group) = group {
        request = request.header("Group", &group.id);
    }
    let response = request.send().await?;
    Ok(ok(response).await?.json().await?)
}

/// Removes a secret from your store, or a group's (its direct admins), and so from every
/// repository that uses it.
pub async fn remove(credentials: &Credentials, id: u64, group: Option<&Group>) -> Result<()> {
    let mut request = client()
        .delete(format!("{}/secrets/{id}", crate::get_url()))
        .header("Authorization", format!("Token {}", credentials.token));
    if let Some(group) = group {
        request = request.header("Group", &group.id);
    }
    let response = request.send().await?;
    ok(response).await?;
    Ok(())
}