Skip to main content

src/gdav@list_addressbooks.erl

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

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

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

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

-file("src/gdav/list_addressbooks.gleam", 22).
-spec build(request_builder(), gdav:credentials()) -> gleam@http@request:request(binary()).
build(Builder, Credentials) ->
    Headers = [{<<"Depth"/utf8>>, <<"1"/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:resourcetype />"/utf8>>/binary,
                    "<d:displayname />"/utf8>>/binary,
                "<cs:getctag />"/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_addressbooks.gleam", 48).
-spec response(gleam@http@response:response(binary())) -> {ok,
        list(addressbook())} |
    {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) ->
                    Addressbooks = 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,
                                        {addressbook,
                                            erlang:element(2, R),
                                            Displayname,
                                            Ctag}};

                                {_, _} ->
                                    {error, nil}
                            end
                        end
                    ),
                    {ok, Addressbooks}
                end
            );

        401 ->
            {error, authentication_failed};

        403 ->
            {error, authentication_failed};

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