-module(gdav@list_calendars).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gdav/list_calendars.gleam").
-export([request/1, build/2, response/1]).
-export_type([calendar/0, request_builder/0]).
-type calendar() :: {calendar, binary(), binary(), binary()}.
-type request_builder() :: {request_builder, binary()}.
-file("src/gdav/list_calendars.gleam", 18).
-spec request(binary()) -> request_builder().
request(Calendar_home_path) ->
{request_builder, Calendar_home_path}.
-file("src/gdav/list_calendars.gleam", 22).
-spec build(request_builder(), gdav:credentials()) -> gleam@http@request:request(binary()).
build(Builder, Credentials) ->
Headers = [{<<"Depth"/utf8>>, <<"1"/utf8>>},
{<<"Prefer"/utf8>>, <<"return-minimal"/utf8>>},
{<<"Content-Type"/utf8>>, <<"application/xml; charset=utf-8"/utf8>>}],
Body = <<<<<<<<<<<<<<"<d:propfind xmlns:d=\"DAV:\" xmlns:cs=\"http://calendarserver.org/ns/\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\">"/utf8,
"<d:prop>"/utf8>>/binary,
"<d:resourcetype />"/utf8>>/binary,
"<d:displayname />"/utf8>>/binary,
"<cs:getctag />"/utf8>>/binary,
"<c:supported-calendar-component-set />"/utf8>>/binary,
"</d:prop>"/utf8>>/binary,
"</d:propfind>"/utf8>>,
gdav@internal:request(
Credentials,
{other, <<"PROPFIND"/utf8>>},
erlang:element(2, Builder),
Headers,
Body
).
-file("src/gdav/list_calendars.gleam", 50).
-spec response(gleam@http@response:response(binary())) -> {ok, list(calendar())} |
{error, gdav:dav_error()}.
response(Res) ->
case erlang:element(2, Res) of
S when (S >= 200) andalso (S < 300) ->
gleam@result:'try'(
gdav@internal@xml:parse_multistatus(erlang:element(4, Res)),
fun(Responses) ->
Calendars = gleam@list:filter_map(
Responses,
fun(R) ->
Find = fun(Ns, Local) ->
gdav@internal@xml:find_text(
erlang:element(4, R),
Ns,
Local
)
end,
case {Find(<<"DAV:"/utf8>>, <<"displayname"/utf8>>),
Find(
<<"http://calendarserver.org/ns/"/utf8>>,
<<"getctag"/utf8>>
)} of
{{ok, Displayname}, {ok, Ctag}} ->
{ok,
{calendar,
erlang:element(2, R),
Displayname,
Ctag}};
{_, _} ->
{error, nil}
end
end
),
{ok, Calendars}
end
);
401 ->
{error, authentication_failed};
403 ->
{error, authentication_failed};
_ ->
{error, {unexpected_response, Res}}
end.