Skip to main content

fauna_api/magpie/
explorer.rs

1use serde::{Deserialize, Serialize};
2use serde_json::json;
3
4use super::portfolio::Asset;
5use crate::authentification::CredentialsOrApiKey;
6use crate::error::Result;
7use crate::http::{client, encode, ok};
8
9pub use super::room::ExplorerRoom;
10
11/// A trendline anchored to the chart as [fromTime, fromPrice, toTime, toPrice].
12pub type Trend = [f64; 4];
13
14/// Per-ticker explorer state. Horizontals are plain price lines, notifications
15/// are price lines that fire an email (and remove themselves) when crossed.
16#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
17pub struct Explorer {
18    pub ticker: String,
19    pub asset: Asset,
20    #[serde(default)]
21    pub trends: Vec<Trend>,
22    #[serde(default)]
23    pub horizontals: Vec<f64>,
24    #[serde(default)]
25    pub notifications: Vec<f64>,
26}
27
28fn authorization_header(authorization: &CredentialsOrApiKey) -> String {
29    match authorization {
30        CredentialsOrApiKey::Credentials(credentials) => format!("Token {}", credentials.token),
31        CredentialsOrApiKey::ApiKey(key) => format!("ApiKey {}", key.id),
32    }
33}
34
35pub async fn list_explorers(authorization: &CredentialsOrApiKey) -> Result<Vec<Explorer>> {
36    let response = client()
37        .get(format!("{}/magpie/explorers", super::get_url()))
38        .header("Authorization", authorization_header(authorization))
39        .send()
40        .await?;
41    Ok(ok(response).await?.json().await?)
42}
43
44pub async fn get_explorer(
45    authorization: &CredentialsOrApiKey,
46    asset: Asset,
47    ticker: &str,
48) -> Result<Explorer> {
49    let response = client()
50        .get(format!(
51            "{}/magpie/explorer/{}/{}",
52            super::get_url(),
53            encode(asset.as_str()),
54            encode(ticker)
55        ))
56        .header("Authorization", authorization_header(authorization))
57        .send()
58        .await?;
59    Ok(ok(response).await?.json().await?)
60}
61
62pub async fn set_explorer(authorization: &CredentialsOrApiKey, explorer: &Explorer) -> Result<()> {
63    let response = client()
64        .put(format!("{}/magpie/explorer", super::get_url()))
65        .header("Authorization", authorization_header(authorization))
66        .json(&json!({ "explorer": explorer }))
67        .send()
68        .await?;
69    ok(response).await?;
70    Ok(())
71}