Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/principal #19

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
8 changes: 8 additions & 0 deletions examples/calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ try do
description: "This is an example calendar."
)

{:ok, principal} =
client
|> CalDAVClient.Principal.fetch()

{:ok, calendars} =
client
|> CalDAVClient.Calendar.list()

:ok =
client
|> CalDAVClient.Calendar.update(calendar_url,
Expand Down
34 changes: 34 additions & 0 deletions lib/caldav_client/calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,45 @@ defmodule CalDAVClient.Calendar do
import CalDAVClient.HTTP.Error
import CalDAVClient.Tesla

@type t :: %__MODULE__{
url: String.t(),
name: String.t(),
type: String.t(),
timezone: String.t()
}
@enforce_keys [:url, :name, :type, :timezone]
defstruct @enforce_keys

@xml_middlewares [
CalDAVClient.Tesla.ContentTypeXMLMiddleware,
CalDAVClient.Tesla.ContentLengthMiddleware
]

@doc """
Fetches the list of calendars (see [RFC 4791, section 4.2](https://tools.ietf.org/html/rfc4791#section-4.2)).
"""
@spec list(CalDAVClient.Client.t()) ::
{:ok, [t()]} | {:error, any()}
def list(caldav_client) do
case caldav_client
|> make_tesla_client(@xml_middlewares)
|> Tesla.request(
method: :propfind,
url: "",
body: CalDAVClient.XML.Builder.build_list_calendar_xml()
) do
{:ok, %Tesla.Env{status: 207, body: response_xml}} ->
calendars = response_xml |> CalDAVClient.XML.Parser.parse_calendars()
{:ok, calendars}

{:ok, %Tesla.Env{status: code}} ->
{:error, reason_atom(code)}

{:error, _reason} = error ->
error
end
end

@doc """
Creates a calendar (see [RFC 4791, section 5.3.1.2](https://tools.ietf.org/html/rfc4791#section-5.3.1.2)).

Expand Down
71 changes: 71 additions & 0 deletions lib/caldav_client/principal.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
defmodule CalDAVClient.Principal do
@moduledoc """
Allows for managing calendars on the calendar server.
"""

import CalDAVClient.HTTP.Error
import CalDAVClient.Tesla

@type t :: %__MODULE__{
current_user_principal: String.t(),
resource_type: String.t()
}
@enforce_keys [:current_user_principal, :resource_type]
defstruct @enforce_keys

@xml_middlewares [
CalDAVClient.Tesla.ContentTypeXMLMiddleware,
CalDAVClient.Tesla.ContentLengthMiddleware
]

@doc """
Check credentials
"""
@spec check(CalDAVClient.Client.t()) ::
:ok | {:error, any()}
def check(caldav_client) do
case caldav_client
|> make_tesla_client(@xml_middlewares)
|> Tesla.request(
method: :options,
url: ""
) do
{:ok, %Tesla.Env{status: 200, body: response_xml, headers: headers}} ->
{:ok, headers}

{:ok, %Tesla.Env{status: 401, body: response_xml}} ->
{:error, :unauthorized}

{:error, reason, body: response_xml} = error ->
:error

{:error, reason} = error ->
:error
end
end

@doc """
Fetches principal information (see [RFC 4791, section 6.2](https://tools.ietf.org/html/rfc4791#section-6.2)).
"""
@spec fetch(CalDAVClient.Client.t()) ::
:ok | {:error, any()}
def fetch(caldav_client) do
case caldav_client
|> make_tesla_client(@xml_middlewares)
|> Tesla.request(
method: :propfind,
url: "",
body: CalDAVClient.XML.Builder.build_fetch_principal_xml()
) do
{:ok, %Tesla.Env{status: 207, body: response_xml}} ->
calendars = response_xml |> CalDAVClient.XML.Parser.parse_principal()
{:ok, calendars}

{:ok, %Tesla.Env{status: code, body: response_xml}} ->
{:error, reason_atom(code)}

{:error, _reason, body: response_xml} = error ->
:error
end
end
end
46 changes: 46 additions & 0 deletions lib/caldav_client/xml/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,52 @@ defmodule CalDAVClient.XML.Builder do
@default_event_from DateTime.from_naive!(~N[0000-01-01 00:00:00], "Etc/UTC")
@default_event_to DateTime.from_naive!(~N[9999-12-31 23:59:59], "Etc/UTC")

@doc """
Generates XML request body to fetch principals
(see [RFC 4791, section 4.2](https://tools.ietf.org/html/rfc4791#section-4.2)).
"""
@spec build_fetch_principal_xml() :: String.t()
def build_fetch_principal_xml() do
{"D:propfind",
[
"xmlns:D": "DAV:",
"xmlns:CS": "http://calendarserver.org/ns/",
"xmlns:C": "urn:ietf:params:xml:ns:caldav"
],
[
{"D:prop", nil,
[
{"D:resourcetype"},
{"D:current-user-principal"}
]}
]}
|> serialize()
end

@doc """
Generates XML request body to fetch the list of calendars
(see [RFC 4791, section 4.2](https://tools.ietf.org/html/rfc4791#section-4.2)).
"""
@spec build_list_calendar_xml() :: String.t()
def build_list_calendar_xml() do
{"D:propfind",
[
"xmlns:D": "DAV:",
"xmlns:CS": "http://calendarserver.org/ns/",
"xmlns:C": "urn:ietf:params:xml:ns:caldav"
],
[
{"D:prop", nil,
[
{"D:resourcetype"},
{"D:displayname"},
{"C:calendar-timezone"},
{"C:supported-calendar-component-set"}
]}
]}
|> serialize()
end

@doc """
Generates XML request body to create a calendar
(see [RFC 4791, section 5.3.1.2](https://tools.ietf.org/html/rfc4791#section-5.3.1.2)).
Expand Down
47 changes: 47 additions & 0 deletions lib/caldav_client/xml/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ defmodule CalDAVClient.XML.Parser do
@url_xpath ~x"./*[local-name()='href']/text()"s
@icalendar_xpath ~x"./*[local-name()='propstat']/*[local-name()='prop']/*[local-name()='calendar-data']/text()"s
@etag_xpath ~x"./*[local-name()='propstat']/*[local-name()='prop']/*[local-name()='getetag']/text()"s
@resource_type_xpath ~x"name(./*[local-name()='propstat']/*[local-name()='prop']/*[local-name()='resourcetype']/*)"s

@cal_name_xpath ~x"./*[local-name()='propstat']/*[local-name()='prop']/*[local-name()='displayname']/text()"s
@cal_type_xpath ~x"./*[local-name()='propstat']/*[local-name()='prop']/*[local-name()='supported-calendar-component-set']/*[local-name()='comp']/@name"s
@cal_timezone_xpath ~x"./*[local-name()='propstat']/*[local-name()='prop']/*[local-name()='calendar-timezone']/text()"s

@doc """
Parses XML response body into a list of events.
Expand All @@ -23,4 +28,46 @@ defmodule CalDAVClient.XML.Parser do
)
|> Enum.map(&struct(CalDAVClient.Event, &1))
end

@doc """
Parses XML response body into a list of calendars.
"""
@spec parse_calendars(response_xml :: String.t()) :: [CalDAVClient.Calendar.t()]
def parse_calendars(response_xml) do
response_xml
|> xpath(@event_xpath,
url: @url_xpath,
name: @cal_name_xpath,
type: @cal_type_xpath,
timezone: @cal_timezone_xpath
)
|> Enum.map(&struct(CalDAVClient.Calendar, &1))
end

@doc """
Parses XML response body of a principal
"""
@spec parse_principal(response_xml :: String.t()) :: [CalDAVClient.Principal.t()]
def parse_principal(response_xml) do
response_xml
|> xpath(@event_xpath,
current_user_principal: @url_xpath,
resource_type: @resource_type_xpath
)
|> Enum.map(&struct(CalDAVClient.Principal, &1))
end

@doc """
Parses XML response body into a list of todos.
"""
@spec parse_todos(response_xml :: String.t()) :: [CalDAVClient.Todo.t()]
def parse_todos(response_xml) do
response_xml
|> xpath(@todo_xpath,
url: @url_xpath,
icalendar: @icalendar_xpath,
etag: @etag_xpath
)
|> Enum.map(&struct(CalDAVClient.Todo, &1))
end
end
2 changes: 1 addition & 1 deletion mix.lock
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"},
"nimble_parsec": {:hex, :nimble_parsec, "1.2.3", "244836e6e3f1200c7f30cb56733fd808744eca61fd182f731eac4af635cc6d0b", [:mix], [], "hexpm", "c8d789e39b9131acf7b99291e93dae60ab48ef14a7ee9d58c6964f59efb570b0"},
"parse_trans": {:hex, :parse_trans, "3.3.1", "16328ab840cc09919bd10dab29e431da3af9e9e7e7e6f0089dd5a2d2820011d8", [:rebar3], [], "hexpm", "07cd9577885f56362d414e8c4c4e6bdf10d43a8767abb92d24cbe8b24c54888b"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.6", "cf344f5692c82d2cd7554f5ec8fd961548d4fd09e7d22f5b62482e5aeaebd4b0", [:make, :mix, :rebar3], [], "hexpm", "bdb0d2471f453c88ff3908e7686f86f9be327d065cc1ec16fa4540197ea04680"},
"ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"},
"sweet_xml": {:hex, :sweet_xml, "0.7.2", "4729f997286811fabdd8288f8474e0840a76573051062f066c4b597e76f14f9f", [:mix], [], "hexpm", "6894e68a120f454534d99045ea3325f7740ea71260bc315f82e29731d570a6e8"},
"tesla": {:hex, :tesla, "1.4.0", "1081bef0124b8bdec1c3d330bbe91956648fb008cf0d3950a369cda466a31a87", [:mix], [{:castore, "~> 0.1", [hex: :castore, repo: "hexpm", optional: true]}, {:exjsx, ">= 3.0.0", [hex: :exjsx, repo: "hexpm", optional: true]}, {:finch, "~> 0.3", [hex: :finch, repo: "hexpm", optional: true]}, {:fuse, "~> 2.4", [hex: :fuse, repo: "hexpm", optional: true]}, {:gun, "~> 1.3", [hex: :gun, repo: "hexpm", optional: true]}, {:hackney, "~> 1.6", [hex: :hackney, repo: "hexpm", optional: true]}, {:ibrowse, "~> 4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:jason, ">= 1.0.0", [hex: :jason, repo: "hexpm", optional: true]}, {:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:mint, "~> 1.0", [hex: :mint, repo: "hexpm", optional: true]}, {:poison, ">= 1.0.0", [hex: :poison, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm", "bf1374a5569f5fca8e641363b63f7347d680d91388880979a33bc12a6eb3e0aa"},
"tzdata": {:hex, :tzdata, "1.1.0", "72f5babaa9390d0f131465c8702fa76da0919e37ba32baa90d93c583301a8359", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "18f453739b48d3dc5bcf0e8906d2dc112bb40baafe2c707596d89f3c8dd14034"},
Expand Down
20 changes: 20 additions & 0 deletions test/caldav_client/xml/builder_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,26 @@ defmodule CalDAVClient.XML.BuilderTest do
use ExUnit.Case, async: true
doctest CalDAVClient.XML.Builder

test "generates list calendar XML request body" do
# https://tools.ietf.org/html/rfc4791#section-4.2

actual = CalDAVClient.XML.Builder.build_list_calendar_xml()

expected = """
<?xml version="1.0" encoding="utf-8"?>
<D:propfind xmlns:D="DAV:" xmlns:CS="http://calendarserver.org/ns/" xmlns:C="urn:ietf:params:xml:ns:caldav">
<D:prop>
<D:resourcetype/>
<D:displayname/>
<C:calendar-timezone/>
<C:supported-calendar-component-set/>
</D:prop>
</D:propfind>
"""

assert_xml_identical(actual, expected)
end

test "generates create calendar XML request body" do
# https://tools.ietf.org/html/rfc4791#section-5.3.1.2

Expand Down
Loading