Skip to main content

src/gdav@get_calendar_info.erl

-module(gdav@get_calendar_info).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gdav/get_calendar_info.gleam").
-export([request/1, build/2, response/1]).
-export_type([calendar_info/0, request_builder/0]).

-type calendar_info() :: {calendar_info,
        binary(),
        binary(),
        gleam@option:option(binary())}.

-type request_builder() :: {request_builder, binary()}.

-file("src/gdav/get_calendar_info.gleam", 18).
-spec request(binary()) -> request_builder().
request(Collection_path) ->
    {request_builder, Collection_path}.

-file("src/gdav/get_calendar_info.gleam", 22).
-spec build(request_builder(), gdav:credentials()) -> gleam@http@request:request(binary()).
build(Builder, Credentials) ->
    Headers = [{<<"Depth"/utf8>>, <<"0"/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/\">"/utf8,
                            "<d:prop>"/utf8>>/binary,
                        "<d:displayname />"/utf8>>/binary,
                    "<cs:getctag />"/utf8>>/binary,
                "<d:sync-token />"/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/get_calendar_info.gleam", 49).
-spec response(gleam@http@response:response(binary())) -> {ok, calendar_info()} |
    {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_propfind(erlang:element(4, Res)),
                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}} ->
                            Sync_token = case Find(
                                <<"DAV:"/utf8>>,
                                <<"sync-token"/utf8>>
                            ) of
                                {ok, T} when T =/= <<""/utf8>> ->
                                    {some, T};

                                _ ->
                                    none
                            end,
                            {ok, {calendar_info, Displayname, Ctag, Sync_token}};

                        {{error, _}, _} ->
                            {error,
                                {could_not_parse_xml,
                                    <<"Missing displayname"/utf8>>}};

                        {_, {error, _}} ->
                            {error,
                                {could_not_parse_xml, <<"Missing ctag"/utf8>>}}
                    end
                end
            );

        404 ->
            {error, not_found};

        401 ->
            {error, authentication_failed};

        403 ->
            {error, authentication_failed};

        _ ->
            {error, {unexpected_response, Res}}
    end.