Skip to main content

src/gdav@create_contact.erl

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

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

-file("src/gdav/create_contact.gleam", 11).
-spec request(binary(), binary(), binary()) -> request_builder().
request(Collection_path, Filename, Vcardstring) ->
    {request_builder, Collection_path, Filename, Vcardstring}.

-file("src/gdav/create_contact.gleam", 19).
-spec build(request_builder(), gdav:credentials()) -> gleam@http@request:request(binary()).
build(Builder, Credentials) ->
    Headers = [{<<"Content-Type"/utf8>>, <<"text/vcard; charset=utf-8"/utf8>>}],
    gdav@internal:request(
        Credentials,
        put,
        <<<<(erlang:element(2, Builder))/binary, "/"/utf8>>/binary,
            (erlang:element(3, Builder))/binary>>,
        Headers,
        erlang:element(4, Builder)
    ).

-file("src/gdav/create_contact.gleam", 34).
-spec response(gleam@http@response:response(binary())) -> {ok, binary()} |
    {error, gdav:dav_error()}.
response(Res) ->
    case erlang:element(2, Res) of
        S when (S >= 200) andalso (S < 300) ->
            {ok, erlang:element(4, Res)};

        404 ->
            {error, not_found};

        401 ->
            {error, authentication_failed};

        403 ->
            {error, authentication_failed};

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