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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#[derive(Debug)]
pub enum HealthcheckError {
    RequestFailure(reqwest::Error),
    InvalidHeaderValue(reqwest::header::InvalidHeaderValue),
    UnexpectedStatus(reqwest::StatusCode),
}

impl From<reqwest::Error> for HealthcheckError {
    fn from(error: reqwest::Error) -> HealthcheckError {
        HealthcheckError::RequestFailure(error)
    }
}

impl From<reqwest::header::InvalidHeaderValue> for HealthcheckError {
    fn from(error: reqwest::header::InvalidHeaderValue) -> HealthcheckError {
        HealthcheckError::InvalidHeaderValue(error)
    }
}

#[async_trait::async_trait]
pub trait Healthcheck {
    async fn healthcheck(&self, ) -> Result<(), HealthcheckError>;
    async fn version(&self, ) -> Result<crate::model::VersionInfo, HealthcheckError>;
}

#[derive(Clone, Debug)]
pub struct HealthcheckLive {
    pub base_url: reqwest::Url,
    pub allow_insecure: bool,
}

#[async_trait::async_trait]
impl Healthcheck for HealthcheckLive {
    async fn healthcheck(&self, ) -> Result<(), HealthcheckError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("healthcheck");

        {
            let headers_vec: Vec<(&str, String)> = vec![];            tracing::info!(method="get", url=url.to_string(), headers=?headers_vec, body="<no_body>", "healthcheck");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .get(url)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = ();
                Ok(body)
            }

            _ => Err(HealthcheckError::UnexpectedStatus(result.status()))
        }
    }

    async fn version(&self, ) -> Result<crate::model::VersionInfo, HealthcheckError> {
        let mut url = self.base_url.clone();
        url.path_segments_mut().unwrap()
            .push("v1")
            .push("version");

        {
            let headers_vec: Vec<(&str, String)> = vec![];            tracing::info!(method="get", url=url.to_string(), headers=?headers_vec, body="<no_body>", "version");
        }
        let mut builder = reqwest::Client::builder();
        if self.allow_insecure {
            builder = builder.danger_accept_invalid_certs(true);
        }
        let client = builder.build()?;
        let result = client
            .get(url)
            .send()
            .await?;
        match result.status().as_u16() {
            200 => {
                let body = result.json::<crate::model::VersionInfo>().await?;
                Ok(body)
            }

            _ => Err(HealthcheckError::UnexpectedStatus(result.status()))
        }
    }
}