Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/elixir.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up Elixir
uses: erlef/setup-beam@988e02bfe678367a02564f65ca2e37726dc0268f
uses: erlef/setup-beam@v1
with:
elixir-version: '1.12.3' # Define the elixir version [required]
otp-version: '24.1' # Define the OTP version [required]
otp-version: '24.3' # Define the OTP version [required]
- name: Check formatting
run: mix format --check-formatted

Expand All @@ -30,10 +30,10 @@ jobs:
steps:
- uses: actions/checkout@v2
- name: Set up Elixir
uses: erlef/setup-beam@988e02bfe678367a02564f65ca2e37726dc0268f
uses: erlef/setup-beam@v1
with:
elixir-version: '1.12.3' # Define the elixir version [required]
otp-version: '24.1' # Define the OTP version [required]
otp-version: '24.3' # Define the OTP version [required]
- name: Restore dependencies cache
uses: actions/cache@v2
with:
Expand Down
4 changes: 4 additions & 0 deletions examples/calendar.ex
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ try do
description: "This is an example calendar."
)

{: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
24 changes: 24 additions & 0 deletions lib/caldav_client/xml/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,30 @@ 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 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
19 changes: 19 additions & 0 deletions lib/caldav_client/xml/parser.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ defmodule CalDAVClient.XML.Parser do
@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

@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 +27,19 @@ 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
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