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
use chrono::DateTime;
use colored::*;
use prettytable::{cell, row, Table};
use reqwest::blocking::{Client, Request};
use reqwest::Method;
use serde_json::{from_str as json_from_str, Value as JsonValue};

fn get_covid19_api(country: String) -> Result<String, Box<dyn std::error::Error>> {
    Ok(format!(
        "https://covid19.mathdro.id/api/countries/{}",
        country
    ))
}

fn handle_fetch_request(
    url: String,
) -> Result<reqwest::blocking::Response, Box<dyn std::error::Error>> {
    Ok(Client::new()
        .execute(Request::new(Method::GET, url.parse().unwrap()))
        .unwrap())
}

pub fn init(country: String) {
    let response = handle_fetch_request(get_covid19_api(country).unwrap()).unwrap();
    let response_json: JsonValue = json_from_str(&response.text().unwrap()).unwrap();
    let response_map = response_json.as_object().unwrap().to_owned();

    let mut table = Table::new();

    table.add_row(row![
        "Last Update",
        DateTime::parse_from_rfc3339(response_map["lastUpdate"].as_str().unwrap()).unwrap()
    ]);

    table.add_row(row![
        "Confirmed",
        response_map["confirmed"].as_object().unwrap()["value"]
            .as_i64()
            .unwrap()
            .to_string()
            .yellow()
    ]);

    table.add_row(row![
        "Recovered",
        response_map["recovered"].as_object().unwrap()["value"]
            .as_i64()
            .unwrap()
            .to_string()
            .green()
    ]);

    table.add_row(row![
        "Deaths",
        response_map["deaths"].as_object().unwrap()["value"]
            .as_i64()
            .unwrap()
            .to_string()
            .red()
    ]);

    table.printstd();
}