| Crates.io | geoiplocation |
| lib.rs | geoiplocation |
| version | 0.3.0 |
| created_at | 2022-06-22 05:08:19.215399+00 |
| updated_at | 2022-06-23 04:45:33.664528+00 |
| description | Get location based on IP |
| homepage | |
| repository | |
| max_upload_size | |
| id | 610660 |
| size | 7,657 |
Get location data for a given IP
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.
Eg:
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);
Ok(())
}
Output:
Some(
Location {
ip: Some(
"8.8.8.8",
),
city: Some(
"",
),
country: Some(
"United States",
),
continent: Some(
"North America",
),
},
)