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
use crate::geolocate_lib::helpers::fetch_many;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{fmt::Display, net::IpAddr};
/// The supported geolocation data providers.
/// Their API will be used to fetch geo data from.
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum Provider {
Ip2Location,
IpGeolocation,
}
impl Display for Provider {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Provider::Ip2Location => write!(f, "ip2location"),
Provider::IpGeolocation => write!(f, "ipgeolocation"),
}
}
}
/// Represents the operation of geolocation.
/// Give it IP address/addresses and an API key
/// and will give you geolocation data.
#[derive(Clone, Debug)]
pub struct Geolocation {
ip_addrs: Vec<IpAddr>,
api_key: String,
client: reqwest::Client,
}
impl Geolocation {
pub fn new(ip_addrs: Vec<IpAddr>, api_key: String) -> Self {
Self {
ip_addrs,
api_key,
client: reqwest::Client::new(),
}
}
/// Fetch geolocation data from a provider.
pub async fn fetch<T>(
&mut self,
provider: Provider,
) -> anyhow::Result<Vec<T>>
where
T: Serialize + DeserializeOwned + Send + 'static,
{
match provider {
Provider::Ip2Location => match self.ip_addrs.len() {
1 => {
let url = format!(
"https://api.ip2location.io/?ip={}&key={}",
self.ip_addrs.first().unwrap(),
self.api_key
);
let json_result = self
.client
.get(url)
.send()
.await?
.json::<T>()
.await
.map_err(|err| anyhow::anyhow!("{}", err));
match json_result {
Ok(json) => Ok(vec![json]),
Err(err) => anyhow::bail!("{}", err),
}
}
_ => self.fetch_many(provider).await,
},
Provider::IpGeolocation => match self.ip_addrs.len() {
1 => {
let url = format!(
"https://api.ipgeolocation.io/ipgeo?apiKey={}&ip={}",
self.api_key,
self.ip_addrs.first().unwrap()
);
let json_result = self
.client
.get(url)
.send()
.await?
.json::<T>()
.await
.map_err(|err| anyhow::anyhow!("{}", err));
match json_result {
Ok(json) => Ok(vec![json]),
Err(err) => anyhow::bail!("{}", err),
}
}
_ => self.fetch_many(provider).await,
},
}
}
/// Fetch geolocation data from a provider,
/// but for multiple IP addresses.
/// Initiates concurrent requests.
async fn fetch_many<T>(
&mut self,
provider: Provider,
) -> anyhow::Result<Vec<T>>
where
T: Serialize + DeserializeOwned + Send + 'static,
{
match provider {
Provider::Ip2Location => {
let urls = self
.ip_addrs
.iter()
.map(|ip_addr| {
format!(
"https://api.ip2location.io/?ip={}&key={}",
ip_addr, self.api_key
)
})
.collect::<Vec<String>>();
fetch_many(urls, &self.client).await
}
Provider::IpGeolocation => {
let urls = self.ip_addrs
.iter()
.map(|ip_addr| {
format!(
"https://api.ipgeolocation.io/ipgeo?apiKey={}&ip={}",
self.api_key, ip_addr
)
})
.collect::<Vec<String>>();
fetch_many(urls, &self.client).await
}
}
}
}