Skip to content

Commit

Permalink
require api key for last geocode updates policy, and avoid rate limiting
Browse files Browse the repository at this point in the history
  • Loading branch information
m4salah committed Dec 30, 2023
1 parent b9a7148 commit dba1f60
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/handlers/day21.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::error::Error;
use std::{env, error::Error, thread, time::Duration};

use axum::{extract::Path, http::StatusCode, routing::get, Router};
use dms_coordinates::DMS;
Expand Down Expand Up @@ -60,25 +60,32 @@ async fn country(Path(binary): Path<String>) -> Result<String, StatusCode> {
)
}

#[derive(Deserialize, Debug, Clone)]
#[derive(Deserialize)]
struct Geocode {
address: Address,
pub address: Address,
}

#[derive(Deserialize, Debug, Clone)]
struct Address {
country: String,
#[derive(Deserialize)]
pub struct Address {
pub country: String,
}

async fn fetch_country_from_latlong(lat: f64, long: f64) -> Result<String, Box<dyn Error>> {
Ok(reqwest::get(format!(
"https://geocode.maps.co/reverse?lat={lat}&lon={long}"
))
.await?
.json::<Geocode>()
.await?
.address
.country)
let geocode_api_key = env::var("GEOCODING_API_KEY")?;
let endpoint =
format!("https://geocode.maps.co/reverse?lat={lat}&lon={long}&api_key={geocode_api_key}");
let country = loop {
let endpoint = endpoint.clone();
if let Ok(req) = reqwest::get(endpoint).await {
if let Ok(c) = req.json::<Geocode>().await {
break c.address.country;
}
}
// sleep for 1 second and repeat, because the rate limit in this service is 1 req/second
// https://geocode.maps.co/
thread::sleep(Duration::from_secs(1));
};
Ok(country)
}

#[cfg(test)]
Expand Down

0 comments on commit dba1f60

Please sign in to comment.