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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
use std::collections::HashMap;

struct ApiClient {
    user_id: String,
    license_key: String,
    client: reqwest::Client,
}

impl ApiClient {
    pub fn new_client(user_id: String, license_key: String) -> ApiClient {
        let client = reqwest::Client::new();
        ApiClient {
            user_id,
            license_key,
            client,
        }
    }

    #[tokio::main]
    async fn make_request(
        &self,
        prefix: String,
        ip_address: String,
    ) -> Result<Response, Box<dyn std::error::Error>> {
        let url = format!("{}/{}", prefix, ip_address);

        let resp = self
            .client
            .get(url)
            .basic_auth(&self.user_id, Some(&self.license_key))
            .send()
            .await?
            .json::<Response>()
            .await?;
        Ok(resp)
    }

    pub fn get_country(&self, ip_address: String) -> Result<Response, Box<dyn std::error::Error>> {
        self.make_request(
            String::from("https://geoip.maxmind.com/geoip/v2.1/country/"),
            ip_address,
        )
    }

    pub fn get_city(&self, ip_address: String) -> Result<Response, Box<dyn std::error::Error>> {
        self.make_request(
            String::from("https://geoip.maxmind.com/geoip/v2.1/city/"),
            ip_address,
        )
    }

    pub fn get_insights(&self, ip_address: String) -> Result<Response, Box<dyn std::error::Error>> {
        self.make_request(
            String::from("https://geoip.maxmind.com/geoip/v2.1/insights/"),
            ip_address,
        )
    }
}

#[derive(serde::Deserialize)]
pub struct City {
    pub confidence: Option<i64>,
    pub geno_name_id: Option<i64>,
    pub names: Option<HashMap<String, String>>,
}

#[derive(serde::Deserialize)]
pub struct Continent {
    pub code: Option<String>,
    pub geno_name_id: Option<i64>,
    pub names: Option<HashMap<String, String>>,
}

#[derive(serde::Deserialize)]
pub struct Country {
    pub confidence: Option<i64>,
    pub geo_name_id: Option<i64>,
    pub iso_code: Option<String>,
    pub names: Option<HashMap<String, String>>,
}

#[derive(serde::Deserialize)]
pub struct Location {
    pub accuracy_radius: Option<i64>,
    pub average_income: Option<i64>,
    pub latitude: Option<f64>,
    pub longitude: Option<f64>,
    pub metro_code: Option<i64>,
    pub population_density: Option<i64>,
    pub time_zone: Option<String>,
}

#[derive(serde::Deserialize)]
pub struct Postal {
    pub code: Option<String>,
    pub confidence: Option<i64>,
}

#[derive(serde::Deserialize)]
pub struct RegisteredCountry {
    pub geo_name_id: Option<i64>,
    pub iso_code: Option<String>,
    pub names: Option<HashMap<String, String>>,
}

#[derive(serde::Deserialize)]
pub struct RepresentedCountry {
    pub geo_name_id: Option<i64>,
    pub iso_code: Option<String>,
    pub names: Option<HashMap<String, String>>,
    pub r#type: Option<String>,
}

#[derive(serde::Deserialize)]
pub struct Subdivision {
    pub confidence: Option<i64>,
    pub geo_name_id: Option<i64>,
    pub iso_code: Option<String>,
    pub names: Option<HashMap<String, String>>,
}

#[derive(serde::Deserialize)]
pub struct Traits {
    pub autonomous_system_number: Option<i64>,
    pub autonomous_system_organization: Option<String>,
    pub domain: Option<String>,
    pub is_anonymous_proxy: Option<bool>,
    pub is_satellite_provider: Option<bool>,
    pub isp: Option<String>,
    pub ip_address: Option<String>,
    pub organization: Option<String>,
    pub user_type: Option<String>,
}

#[derive(serde::Deserialize)]
pub struct MaxMind {
    pub queries_remaining: Option<i64>,
}

#[derive(serde::Deserialize)]
pub struct Response {
    city: Option<City>,
    continent: Option<Continent>,
    country: Option<Country>,
    location: Option<Location>,
    postal: Option<Postal>,
    registered_country: Option<RegisteredCountry>,
    represented_country: Option<RepresentedCountry>,
    subdivisions: Option<Vec<Subdivision>>,
    traits: Option<Traits>,
    maxmind: Option<MaxMind>,
}