Implemented create_calendar for caldav client

This commit is contained in:
daladim 2021-04-20 00:01:15 +02:00
parent 26c49a85c2
commit 5322f04d83
3 changed files with 44 additions and 1 deletions

View file

@ -18,6 +18,19 @@ bitflags! {
}
}
impl SupportedComponents {
pub fn to_xml_string(&self) -> String {
format!(r#"
<C:supported-calendar-component-set>
{} {}
</C:supported-calendar-component-set>
"#,
if self.contains(Self::EVENT) { "<C:comp name=\"VEVENT\"/>" } else { "" },
if self.contains(Self::TODO) { "<C:comp name=\"VTODO\"/>" } else { "" },
)
}
}
impl TryFrom<minidom::Element> for SupportedComponents {
type Error = Box<dyn Error>;

View file

@ -6,6 +6,7 @@ use std::collections::HashMap;
use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use reqwest::Method;
use reqwest::header::CONTENT_TYPE;
use minidom::Element;
use url::Url;
@ -233,8 +234,36 @@ impl CalDavSource<RemoteCalendar> for Client {
}
async fn create_calendar(&mut self, id: CalendarId, name: String, supported_components: SupportedComponents) -> Result<Arc<Mutex<RemoteCalendar>>, Box<dyn Error>> {
todo!();
let creation_body = calendar_body(name, supported_components);
reqwest::Client::new()
.request(Method::from_bytes(b"MKCALENDAR").unwrap(), id.clone())
.header(CONTENT_TYPE, "application/xml")
.basic_auth(self.resource.username(), Some(self.resource.password()))
.body(creation_body)
.send()
.await?;
self.populate_calendars().await?;
self.get_calendar(&id).await.ok_or(format!("Unable to insert calendar {:?}", id).into())
}
}
fn calendar_body(name: String, supported_components: SupportedComponents) -> String {
// This is taken from https://tools.ietf.org/html/rfc4791#page-24
format!(r#"<?xml version="1.0" encoding="utf-8" ?>
<C:mkcalendar xmlns:D="DAV:"
xmlns:C="urn:ietf:params:xml:ns:caldav">
<D:set>
<D:prop>
<D:displayname>{}</D:displayname>
{}
</D:prop>
</D:set>
</C:mkcalendar>
"#,
name,
supported_components.to_xml_string(),
)
}

View file

@ -6,6 +6,7 @@ pub const PASSWORD: &str = "secret_password";
pub const EXAMPLE_TASK_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/6121A0BE-C2E0-4F16-A3FA-658E54E7062A/74439558-CDFF-426C-92CD-ECDDACE971B0.ics";
pub const EXAMPLE_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_name/";
pub const EXAMPLE_CREATED_CALENDAR_URL: &str = "https://my.server.com/remote.php/dav/calendars/john/a_calendar_that_we_have_created/";
pub const ORG_NAME: &str = "My organisation";
pub const PRODUCT_NAME: &str = "My CalDAV client";