Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: improve error message for not yet archived historical weather #188

Merged
merged 4 commits into from
Oct 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/modules/weather.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use anyhow::{Context, Result};
use chrono::NaiveDate;
use chrono::{Local, NaiveDate};
use optional_struct::{optional_struct, Applyable};
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -108,12 +108,18 @@ latitude={lat}
}

pub async fn get_date(date: NaiveDate, lat: f64, lon: f64, units: &Units) -> Result<OptionalWeather> {
// It takes up to five days until temperature data is available in open-meteo's archive.
// Therefore, we use the past_days endpoints for the last five days.
let base_url = if date.signed_duration_since(Local::now().date_naive()).num_days() > -5 {
"https://api.open-meteo.com/v1/forecast?&past_days=10".to_string()
} else {
format!("https://archive-api.open-meteo.com/v1/archive?&start_date={date}&end_date={date}")
};

let url = format!(
"https://archive-api.open-meteo.com/v1/archive?
latitude={lat}
"{base_url}
&latitude={lat}
&longitude={lon}
&start_date={date}
&end_date={date}
&temperature_unit={}
&windspeed_unit={}
&precipitation_unit={}
Expand Down
Loading