Implemented add_item for remote calendars
This commit is contained in:
parent
e39cdc7cd5
commit
e24fab2ccb
1 changed files with 23 additions and 3 deletions
|
@ -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<SyncStatus, Box<dyn Error>> {
|
||||
todo!()
|
||||
async fn add_item(&mut self, item: Item) -> Result<SyncStatus, Box<dyn Error>> {
|
||||
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))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue