Skip to main content

src/gdav.erl

-module(gdav).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]).
-define(FILEPATH, "src/gdav.gleam").
-export([credentials/1, with_basic_auth/3, with_bearer_auth/2]).
-export_type([dav_error/0, credentials/0, event_entry/0, contact_entry/0]).

-type dav_error() :: authentication_failed |
    not_found |
    {could_not_parse_xml, binary()} |
    {unexpected_response, gleam@http@response:response(binary())}.

-type credentials() :: {credentials,
        gleam@http:scheme(),
        gleam@option:option(integer()),
        binary(),
        binary(),
        gleam@option:option(binary())}.

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

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

-file("src/gdav.gleam", 25).
-spec credentials(binary()) -> {ok, credentials()} | {error, nil}.
credentials(Base_url) ->
    gleam@result:'try'(
        gleam_stdlib:uri_parse(Base_url),
        fun(Parsed_uri) ->
            gleam@result:'try'(
                gleam@option:to_result(erlang:element(2, Parsed_uri), nil),
                fun(Scheme_str) ->
                    gleam@result:'try'(
                        gleam@http:scheme_from_string(Scheme_str),
                        fun(Scheme) ->
                            gleam@result:'try'(
                                gleam@option:to_result(
                                    erlang:element(4, Parsed_uri),
                                    nil
                                ),
                                fun(Host) ->
                                    {ok,
                                        {credentials,
                                            Scheme,
                                            erlang:element(5, Parsed_uri),
                                            Host,
                                            erlang:element(6, Parsed_uri),
                                            none}}
                                end
                            )
                        end
                    )
                end
            )
        end
    ).

-file("src/gdav.gleam", 39).
-spec with_basic_auth(credentials(), binary(), binary()) -> credentials().
with_basic_auth(Credentials, Username, Password) ->
    Auth_header = <<<<"Basic"/utf8, " "/utf8>>/binary,
        (gleam_stdlib:base64_encode(
            <<Username/binary, ":"/utf8, Password/binary>>,
            true
        ))/binary>>,
    {credentials,
        erlang:element(2, Credentials),
        erlang:element(3, Credentials),
        erlang:element(4, Credentials),
        erlang:element(5, Credentials),
        {some, Auth_header}}.

-file("src/gdav.gleam", 52).
-spec with_bearer_auth(credentials(), binary()) -> credentials().
with_bearer_auth(Credentials, Token) ->
    {credentials,
        erlang:element(2, Credentials),
        erlang:element(3, Credentials),
        erlang:element(4, Credentials),
        erlang:element(5, Credentials),
        {some, <<"Bearer "/utf8, Token/binary>>}}.