Allow multiple urls which are split with a semicolon

This commit is contained in:
Jan-Henrik 2024-01-07 13:17:37 +01:00
parent 868d27a113
commit 35349b9d04

View file

@ -10,13 +10,14 @@ use serde::Serialize;
async fn handler(params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
match params.get("url") {
Some(url) => Ok(warp::reply::json(&convert(&url, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries)),
Some(url) => Ok(warp::reply::json(&convert(&[url], params.get("days")).await.map_err(|_| warp::reject::reject())?.entries)),
None => Err(warp::reject::reject())
}
}
async fn new_handler(url: String, params: HashMap<String, String>) -> Result<impl Reply, Rejection> {
Ok(warp::reply::json(&convert(&url, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries))
let urls: Vec<&str> = url.split(";").collect();
Ok(warp::reply::json(&convert(&urls, params.get("days")).await.map_err(|_| warp::reject::reject())?.entries))
}
#[derive(Serialize)]
@ -34,7 +35,9 @@ struct CustomCalendarEntry {
isallday: bool,
}
async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
async fn convert(urls: &[&str], days: Option<&String>) -> Result<CustomCalendar> {
let mut entries = Vec::new();
for url in urls {
let url = urlencoding::decode(url)?.into_owned();
let ics_text = reqwest::get(url)
.await?
@ -43,7 +46,6 @@ async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
let calendar = ics_text.parse::<Calendar>().map_err(|e| anyhow::Error::msg(e))?;
let mut entries = Vec::new();
let filter_start = chrono::Utc::now().date_naive().and_hms_opt(0, 0, 0).unwrap().and_utc();
let filter_end = filter_start + chrono::Duration::days(days.unwrap_or(&String::from("1")).parse().unwrap_or(1));
@ -91,6 +93,7 @@ async fn convert(url: &str, days: Option<&String>) -> Result<CustomCalendar> {
});
}
}
}
Ok(CustomCalendar{entries})
}