diff --git a/src/rest.erl b/src/rest.erl index e5c6fd963..091002fa5 100644 --- a/src/rest.erl +++ b/src/rest.erl @@ -28,7 +28,7 @@ -behaviour(ejabberd_config). -export([start/1, stop/1, get/2, get/3, post/4, delete/2, - request/6, with_retry/4, opt_type/1]). + put/4, patch/4, request/6, with_retry/4, opt_type/1]). -include("logger.hrl"). @@ -71,18 +71,17 @@ delete(Server, Path) -> request(Server, delete, Path, [], "application/json", <<>>). post(Server, Path, Params, Content) -> - Data = case catch jiffy:encode(Content) of - {'EXIT', Reason} -> - ?ERROR_MSG("HTTP content encodage failed:~n" - "** Content = ~p~n" - "** Err = ~p", - [Content, Reason]), - <<>>; - Encoded -> - Encoded - end, + Data = encode_json(Content), request(Server, post, Path, Params, "application/json", Data). +put(Server, Path, Params, Content) -> + Data = encode_json(Content), + request(Server, put, Path, Params, "application/json", Data). + +patch(Server, Path, Params, Content) -> + Data = encode_json(Content), + request(Server, patch, Path, Params, "application/json", Data). + request(Server, Method, Path, Params, Mime, Data) -> URI = url(Server, Path, Params), Opts = [{connect_timeout, ?CONNECT_TIMEOUT}, @@ -147,6 +146,18 @@ request(Server, Method, Path, Params, Mime, Data) -> %%% HTTP helpers %%%---------------------------------------------------------------------- +encode_json(Content) -> + case catch jiffy:encode(Content) of + {'EXIT', Reason} -> + ?ERROR_MSG("HTTP content encodage failed:~n" + "** Content = ~p~n" + "** Err = ~p", + [Content, Reason]), + <<>>; + Encoded -> + Encoded + end. + base_url(Server, Path) -> Tail = case iolist_to_binary(Path) of <<$/, Ok/binary>> -> Ok;