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
//! Find location given the IP address.
//!
//! Provides abstraction over the fields returned by the JSON response. Returns a `Struct` for the
//! given query.
//!
//! You can set your own env for location api using something like `export SUBCOM_LOCATION_API_URL=<your-url>`
//! The final URL looks something like `your-url/IP?apikey=KEY`.
//!
//! Two methods are provides(more documentation provided):
//!
//! - `async fn get_location(ip: &str, key: &str) -> anyhow::Result<Option<Location>>`
//! - `async fn get_location_fallback(ip: &str, key: &str) -> anyhow::Result<HashMap<String, String>>`
//!
//! **NOTE:** Still in production so a few things might break or change, you can use `cargo doc --open` for better
//! experience or read it [here](https://docs.rs/geoiplocation/latest/geoiplocation/index.html).
use crateLocation;
use Url;
use HashMap;
use env;
/// `async` function to get the location
///
/// Eg:
/// ```rust
/// use geoiplocation::get_location;
///
/// #[tokio::main]
/// async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let resp = get_location(
/// "8.8.8.8".parse().unwrap(),
/// "YOUR-KEY",
/// )
/// .await?;
/// println!("{:#?}", resp);
/// // Some(Location { ip: Some("8.8.8.8"), city: Some(""), country: Some("United States"), continent: Some("North America") })
///
/// Ok(())
/// }
/// ```
pub async
/// An `async` fallback method to get JSON response
///
/// Instead of holding the values in a Struct he hold the in `HashMap<String, String>`, this should
/// provide a fallback in case the api is ever changing to keep on updating the struct
pub async