From e24fab2ccb6e886b5f6254b0b9af40e922a17a46 Mon Sep 17 00:00:00 2001 From: daladim Date: Tue, 13 Apr 2021 23:33:38 +0200 Subject: [PATCH] Implemented add_item for remote calendars --- src/calendar/remote_calendar.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/calendar/remote_calendar.rs b/src/calendar/remote_calendar.rs index a4864f7..b4dfc10 100644 --- a/src/calendar/remote_calendar.rs +++ b/src/calendar/remote_calendar.rs @@ -3,7 +3,7 @@ use std::error::Error; use std::sync::Mutex; use async_trait::async_trait; -use reqwest::header::CONTENT_TYPE; +use reqwest::{header::CONTENT_TYPE, header::CONTENT_LENGTH}; use crate::traits::BaseCalendar; use crate::traits::DavCalendar; @@ -49,8 +49,28 @@ impl BaseCalendar for RemoteCalendar { } /// Add an item into this calendar - async fn add_item(&mut self, _item: Item) -> Result> { - todo!() + async fn add_item(&mut self, item: Item) -> Result> { + let ical_text = crate::ical::build_from(&item)?; + + let request = reqwest::Client::new() + .put(item.id().as_url().clone()) + .header("If-None-Match", "*") + .header(CONTENT_TYPE, "text/calendar") + .header(CONTENT_LENGTH, ical_text.len()) + .basic_auth(self.resource.username(), Some(self.resource.password())) + .body(ical_text) + .send() + .await?; + + let reply_hdrs = request.headers(); + match reply_hdrs.get("ETag") { + None => Err(format!("No ETag in these response headers: {:?} (request was {:?})", reply_hdrs, item.id()).into()), + Some(etag) => { + let vtag_str = etag.to_str()?; + let vtag = VersionTag::from(String::from(vtag_str)); + Ok(SyncStatus::Synced(vtag)) + } + } } }