]> granicus.if.org Git - ejabberd/commitdiff
Add missing verbs for RESTfull operation
authorChristophe Romain <christophe.romain@process-one.net>
Tue, 9 Aug 2016 08:53:58 +0000 (10:53 +0200)
committerChristophe Romain <christophe.romain@process-one.net>
Tue, 15 Nov 2016 13:35:26 +0000 (14:35 +0100)
src/rest.erl

index e5c6fd963ed94d2657ef12cbdd6bea1aae2f733f..091002fa574d9ba7aa50b910d30ac3a8e437025e 100644 (file)
@@ -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;