1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use thiserror::Error;

use crate::api::{self, Query};
use crate::api::{eod, AsyncQuery};
use crate::types::EodData;

/// A Marketstack API token.
///
/// Marketstack only supports one kind of token.
#[derive(Debug, Clone)]
pub enum Auth {
    /// A personal access token, obtained through Marketstack dashboard.
    Token(String),
}

impl Auth {
    pub fn check_connection<C>(&self, api: &C) -> Result<(), api::ApiError<C::Error>>
    where
        C: api::Client,
    {
        let _: EodData = eod::Eod::builder()
            .symbol("AAPL")
            .build()
            .unwrap()
            .query(api)?;

        Ok(())
    }

    pub async fn check_connection_async<C>(&self, api: &C) -> Result<(), api::ApiError<C::Error>>
    where
        C: api::AsyncClient + Sync,
    {
        let _: EodData = eod::Eod::builder()
            .symbol("AAPL")
            .build()
            .unwrap()
            .query_async(api)
            .await?;

        Ok(())
    }
}

#[derive(Debug, Error)]
#[non_exhaustive]
pub enum AuthError {
    #[error("missing auth error")]
    MissingAuth,
}