Skip to content

software-mansion-labs/elixir-caldav-client

Repository files navigation

Elixir CalDAV Client

Hex.pm API Docs

This library allows for managing calendars and events on a remote calendar server according to CalDAV specification (RFC 4791). Supports time zones, recurrence expansion and ETags. Internally uses Tesla HTTP client.

Please note that conversion between native Elixir structures and iCalendar format (RFC 5545) is beyond the scope of this library. The following packages are recommended:

Installation

CalDAV Client is published on Hex. Add it to your list of dependencies in mix.exs:

def deps do
  [
    {:caldav_client, "~> 2.0"},

    # time zone database
    {:tzdata, "~> 1.1"},

    # recommended Tesla adapter
    {:hackney, "~> 1.18"},
  ]
end

Then run mix deps.get to install the package and its dependencies.

It is also required to configure the time zone database and the default Tesla adapter in the config/config.exs of your project:

# config/config.exs

import Config

config :elixir, :time_zone_database, Tzdata.TimeZoneDatabase

config :tesla, adapter: Tesla.Adapter.Hackney

The default Tesla adapter is Erlang's built-in httpc, but currently it does not support custom HTTP methods such as MKCALENDAR or REPORT.

Documentation

Available at HexDocs.

Examples

Client

The %CalDAVClient.Client{} struct aggregates the connection details such as the server address and user credentials.

client = %CalDAVClient.Client{
  server_url: "https://127.0.0.1:8800/cal.php",
  auth: %CalDAVClient.Auth.Basic{
    username: "username",
    password: "password"
  }
}

The library supports Basic, Digest and Bearer authentication:

%CalDAVClient.Auth.Basic{
  username: "username",
  password: "password"
}

%CalDAVClient.Auth.Digest{
  username: "username",
  password: "password"
}

%CalDAVClient.Auth.Bearer{
  token: "token"
}

Calendar

Each calendar user (or principal, according to CalDAV terminology) can have multiple calendars, which are identified by URLs.

calendar_url = CalDAVClient.URL.Builder.build_calendar_url("username", "example")
# "/calendars/username/example"

:ok =
  client
  |> CalDAVClient.Calendar.create(calendar_url,
    name: "Example calendar",
    description: "This is an example calendar."
  )

:ok = client |> CalDAVClient.Calendar.update(calendar_url, name: "Lorem ipsum")

:ok = client |> CalDAVClient.Calendar.delete<