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
use std::sync::Arc;

use http::Request;
use serde::{Deserialize, Serialize};

use super::NewRelicCredentials;
use crate::{http::HttpClient, sinks::HealthcheckError};

#[derive(Serialize, Deserialize, Debug)]
struct NewRelicStatusModel {
    page: NewRelicStatusPage,
    components: Vec<NewRelicStatusComponent>,
}

#[derive(Serialize, Deserialize, Debug)]
struct NewRelicStatusPage {
    id: String,
    name: String,
    url: String,
}

#[derive(Serialize, Deserialize, Debug)]
struct NewRelicStatusComponent {
    id: String,
    name: String,
    status: String,
}

pub(crate) async fn healthcheck(
    client: HttpClient,
    credentials: Arc<NewRelicCredentials>,
) -> crate::Result<()> {
    let request = Request::post(credentials.get_uri())
        .header("Api-Key", credentials.license_key.clone())
        .body(hyper::Body::empty())
        .unwrap();

    let response = client.send(request).await?;

    match response.status() {
        status if status.is_success() => Ok(()),
        other => Err(HealthcheckError::UnexpectedStatus { status: other }.into()),
    }
}