aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorChristophe Romain <christophe.romain@process-one.net>2018-05-17 14:47:21 +0200
committerChristophe Romain <christophe.romain@process-one.net>2018-05-17 14:47:21 +0200
commitdb51d522e8cec53523fb2b963c945bab09fcec0f (patch)
tree5662a4cbbd5775a0e1e0ea1776c17604f0999589 /src
parentCorrectly resolve upload.localhost for the test suite (diff)
Add support for REST API custom headers
ext_api_headers can be defined as a single string. Headers are separated by comma. Definition MUST NOT contain spaces. Example "X-MyHead:test,X-Token:082748"
Diffstat (limited to 'src')
-rw-r--r--src/rest.erl39
1 files changed, 29 insertions, 10 deletions
diff --git a/src/rest.erl b/src/rest.erl
index 0165d9fa7..6e48f0774 100644
--- a/src/rest.erl
+++ b/src/rest.erl
@@ -28,12 +28,14 @@
-behaviour(ejabberd_config).
-export([start/1, stop/1, get/2, get/3, post/4, delete/2,
- put/4, patch/4, request/6, with_retry/4, opt_type/1]).
+ put/4, patch/4, request/6, with_retry/4,
+ encode_json/1, opt_type/1]).
-include("logger.hrl").
-define(HTTP_TIMEOUT, 10000).
-define(CONNECT_TIMEOUT, 8000).
+-define(CONTENT_TYPE, "application/json").
start(Host) ->
application:start(inets),
@@ -58,31 +60,32 @@ with_retry(Method, Args, Retries, MaxRetries, Backoff) ->
end.
get(Server, Path) ->
- request(Server, get, Path, [], "application/json", <<>>).
+ request(Server, get, Path, [], ?CONTENT_TYPE, <<>>).
get(Server, Path, Params) ->
- request(Server, get, Path, Params, "application/json", <<>>).
+ request(Server, get, Path, Params, ?CONTENT_TYPE, <<>>).
delete(Server, Path) ->
- request(Server, delete, Path, [], "application/json", <<>>).
+ request(Server, delete, Path, [], ?CONTENT_TYPE, <<>>).
post(Server, Path, Params, Content) ->
Data = encode_json(Content),
- request(Server, post, Path, Params, "application/json", Data).
+ request(Server, post, Path, Params, ?CONTENT_TYPE, Data).
put(Server, Path, Params, Content) ->
Data = encode_json(Content),
- request(Server, put, Path, Params, "application/json", Data).
+ request(Server, put, Path, Params, ?CONTENT_TYPE, Data).
patch(Server, Path, Params, Content) ->
Data = encode_json(Content),
- request(Server, patch, Path, Params, "application/json", Data).
+ request(Server, patch, Path, Params, ?CONTENT_TYPE, Data).
request(Server, Method, Path, Params, Mime, Data) ->
URI = to_list(url(Server, Path, Params)),
Opts = [{connect_timeout, ?CONNECT_TIMEOUT},
{timeout, ?HTTP_TIMEOUT}],
Hdrs = [{"connection", "keep-alive"},
- {"User-Agent", "ejabberd"}],
+ {"User-Agent", "ejabberd"}]
+ ++ custom_headers(Server),
Req = if
(Method =:= post) orelse (Method =:= patch) orelse (Method =:= put) orelse (Method =:= delete) ->
{URI, Hdrs, to_list(Mime), Data};
@@ -149,7 +152,7 @@ request(Server, Method, Path, Params, Mime, Data) ->
to_list(V) when is_binary(V) ->
binary_to_list(V);
-to_list(V) ->
+to_list(V) when is_list(V) ->
V.
encode_json(Content) ->
@@ -164,6 +167,20 @@ encode_json(Content) ->
Encoded
end.
+custom_headers(Server) ->
+ case ejabberd_config:get_option({ext_api_headers, Server},
+ <<>>) of
+ <<>> ->
+ [];
+ Hdrs ->
+ lists:foldr(fun(Hdr, Acc) ->
+ case binary:split(Hdr, <<":">>) of
+ [K, V] -> [{binary_to_list(K), binary_to_list(V)}|Acc];
+ _ -> Acc
+ end
+ end, [], binary:split(Hdrs, <<",">>))
+ end.
+
base_url(Server, Path) ->
BPath = case iolist_to_binary(Path) of
<<$/, Ok/binary>> -> Ok;
@@ -209,4 +226,6 @@ opt_type(ext_api_http_pool_size) ->
fun (X) when is_integer(X), X > 0 -> X end;
opt_type(ext_api_url) ->
fun (X) -> iolist_to_binary(X) end;
-opt_type(_) -> [ext_api_http_pool_size, ext_api_url].
+opt_type(ext_api_headers) ->
+ fun (X) -> iolist_to_binary(X) end;
+opt_type(_) -> [ext_api_http_pool_size, ext_api_url, ext_api_headers].