use crate::types::{CalendarError, FreeBusyResponse};
const FREEBUSY_URL: &str = "https://www.googleapis.com/calendar/v3/freeBusy";
pub async fn query_freebusy(
client: &reqwest::Client,
access_token: &str,
calendars: &[String],
time_min: &str,
time_max: &str,
) -> Result<FreeBusyResponse, CalendarError> {
let items: Vec<serde_json::Value> = calendars
.iter()
.map(|id| serde_json::json!({"id": id}))
.collect();
let body = serde_json::json!({
"timeMin": time_min,
"timeMax": time_max,
"items": items,
});
let resp = client
.post(FREEBUSY_URL)
.bearer_auth(access_token)
.json(&body)
.send()
.await?;
if !resp.status().is_success() {
let status = resp.status().as_u16();
let message = resp.text().await.unwrap_or_default();
return Err(CalendarError::Api { status, message });
}
let freebusy: FreeBusyResponse = resp.json().await?;
Ok(freebusy)
}