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
51
52
53
54
55
use color_eyre::eyre::Result;
use reqwest::{
    blocking::Client,
    header::{HeaderMap, HeaderValue},
    Url,
};
use serde::Deserialize;

use super::{HttpClient, HttpResponse};

const BASE_IDEASCALE_URL: &str = "https://cardano.ideascale.com/a/rest/v1/";

#[derive(Debug)]
pub struct ReqwestClient {
    client: Client,
    base_url: Url,
}

impl ReqwestClient {
    pub fn new(api_key: Option<&str>) -> Self {
        let mut client = Client::builder();

        if let Some(api_key) = api_key {
            let mut headers = HeaderMap::new();
            let mut auth_value = HeaderValue::from_str(api_key).unwrap();
            auth_value.set_sensitive(true);
            headers.insert("api_token", auth_value);
            client = client.default_headers(headers);
        };

        let client = client.build().unwrap();

        Self {
            client,
            base_url: BASE_IDEASCALE_URL.try_into().unwrap(),
        }
    }
}

impl HttpClient for ReqwestClient {
    fn get<T>(&self, path: &str) -> Result<HttpResponse<T>>
    where
        T: for<'a> Deserialize<'a>,
    {
        let url = self.base_url.join(path.as_ref())?;
        let response = self.client.get(url).send()?;
        let status = response.status();

        Ok(HttpResponse {
            _marker: std::marker::PhantomData,
            status,
            body: response.text()?,
        })
    }
}