fauna-api 1.0.789

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

use super::portfolio::Asset;
use crate::authentification::CredentialsOrApiKey;
use crate::error::Result;
use crate::http::{client, encode, ok};

pub use super::room::ExplorerRoom;

/// A trendline anchored to the chart as [fromTime, fromPrice, toTime, toPrice].
pub type Trend = [f64; 4];

/// Per-ticker explorer state. Horizontals are plain price lines, notifications
/// are price lines that fire an email (and remove themselves) when crossed.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Explorer {
    pub ticker: String,
    pub asset: Asset,
    #[serde(default)]
    pub trends: Vec<Trend>,
    #[serde(default)]
    pub horizontals: Vec<f64>,
    #[serde(default)]
    pub notifications: Vec<f64>,
}

fn authorization_header(authorization: &CredentialsOrApiKey) -> String {
    match authorization {
        CredentialsOrApiKey::Credentials(credentials) => format!("Token {}", credentials.token),
        CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
    }
}

pub async fn list_explorers(authorization: &CredentialsOrApiKey) -> Result<Vec<Explorer>> {
    let response = client()
        .get(format!("{}/magpie/explorers", super::get_url()))
        .header("Authorization", authorization_header(authorization))
        .send()
        .await?;
    Ok(ok(response).await?.json().await?)
}

pub async fn get_explorer(
    authorization: &CredentialsOrApiKey,
    asset: Asset,
    ticker: &str,
) -> Result<Explorer> {
    let response = client()
        .get(format!(
            "{}/magpie/explorer/{}/{}",
            super::get_url(),
            encode(asset.as_str()),
            encode(ticker)
        ))
        .header("Authorization", authorization_header(authorization))
        .send()
        .await?;
    Ok(ok(response).await?.json().await?)
}

pub async fn set_explorer(authorization: &CredentialsOrApiKey, explorer: &Explorer) -> Result<()> {
    let response = client()
        .put(format!("{}/magpie/explorer", super::get_url()))
        .header("Authorization", authorization_header(authorization))
        .json(&json!({ "explorer": explorer }))
        .send()
        .await?;
    ok(response).await?;
    Ok(())
}