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 dyn_fmt::AsStrFormatExt;
use lazy_static::lazy_static;
use reqwest::blocking::Client;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct IsItUpResponse {
    pub status_code: i8,
}

pub enum DomainStatus {
    Up,
    Down,
    DoesNotExist,
}

const URL: &str = "https://isitup.org/{}.json";

lazy_static! {
    static ref CLIENT: Client = reqwest::blocking::Client::new();
}

/// Taps into the [isitup](https://isitup.org) API
/// to check if a domain is up or down
///
/// ```
/// let response = isitup::isitup("github.com".to_string());
/// assert!(response.is_ok());
/// ```
pub fn isitup(domain: String) -> Result<DomainStatus, reqwest::Error> {
    let isitup_response = CLIENT.get(URL.format(&[domain])).send()?;
    if isitup_response.status().is_client_error() {
        return Ok(DomainStatus::DoesNotExist);
    }
    match isitup_response.json::<IsItUpResponse>() {
        Ok(valid_json) => {
            if valid_json.status_code == 1 {
                Ok(DomainStatus::Up)
            } else {
                Ok(DomainStatus::Down)
            }
        }
        Err(invalid_json) => Err(invalid_json),
    }
}

/// Check if [isitup](https://isitup.org) is accessible
///
/// ```
/// let response = isitup::ping_isitup();
/// assert!(response.is_ok());
/// ```
pub fn ping_isitup() -> Result<bool, reqwest::Error> {
    Ok(CLIENT.get(&URL[..18]).send()?.status().is_success())
}