Add flexibility on rest url config

This commit is contained in:
Christophe Romain 2018-04-18 13:16:08 +02:00
parent 332567693c
commit 265c7b62c7
1 changed files with 35 additions and 20 deletions

View File

@ -28,7 +28,7 @@
-behaviour(ejabberd_config). -behaviour(ejabberd_config).
-export([start/1, stop/1, get/2, get/3, post/4, delete/2, -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, opt_type/1]).
-include("logger.hrl"). -include("logger.hrl").
@ -124,16 +124,16 @@ request(Server, Method, Path, Params, Mime, Data) ->
End = os:timestamp(), End = os:timestamp(),
Elapsed = timer:now_diff(End, Begin) div 1000, %% time in ms Elapsed = timer:now_diff(End, Begin) div 1000, %% time in ms
ejabberd_hooks:run(backend_api_response_time, Server, ejabberd_hooks:run(backend_api_response_time, Server,
[Server, Method, Path, Elapsed]); [Server, Method, Path, Elapsed]);
{error, {http_error,{error,timeout}}} -> {error, {http_error,{error,timeout}}} ->
ejabberd_hooks:run(backend_api_timeout, Server, ejabberd_hooks:run(backend_api_timeout, Server,
[Server, Method, Path]); [Server, Method, Path]);
{error, {http_error,{error,connect_timeout}}} -> {error, {http_error,{error,connect_timeout}}} ->
ejabberd_hooks:run(backend_api_timeout, Server, ejabberd_hooks:run(backend_api_timeout, Server,
[Server, Method, Path]); [Server, Method, Path]);
{error, _} -> {error, _} ->
ejabberd_hooks:run(backend_api_error, Server, ejabberd_hooks:run(backend_api_error, Server,
[Server, Method, Path]) [Server, Method, Path])
end, end,
Result. Result.
@ -154,32 +154,47 @@ encode_json(Content) ->
end. end.
base_url(Server, Path) -> base_url(Server, Path) ->
Tail = case iolist_to_binary(Path) of BPath = case iolist_to_binary(Path) of
<<$/, Ok/binary>> -> Ok; <<$/, Ok/binary>> -> Ok;
Ok -> Ok Ok -> Ok
end, end,
case Tail of Url = case BPath of
<<"http", _Url/binary>> -> Tail; <<"http", _/binary>> -> BPath;
_ -> _ ->
Base = ejabberd_config:get_option({ext_api_url, Server}, Base = ejabberd_config:get_option({ext_api_url, Server},
fun(X) -> iolist_to_binary(X) end,
<<"http://localhost/api">>), <<"http://localhost/api">>),
<<Base/binary, "/", Tail/binary>> case binary:last(Base) of
47 -> <<Base/binary, BPath/binary>>;
_ -> <<Base/binary, "/", BPath/binary>>
end
end,
case binary:last(Url) of
47 -> binary_part(Url, 0, size(Url)-1);
_ -> Url
end. end.
url(Server, Path, []) -> url(Url, []) ->
binary_to_list(base_url(Server, Path)); Url;
url(Server, Path, Params) -> url(Url, Params) ->
Base = base_url(Server, Path), L = [<<"&", (iolist_to_binary(Key))/binary, "=",
[<<$&, ParHead/binary>> | ParTail] = (misc:url_encode(Value))/binary>>
[<<"&", (iolist_to_binary(Key))/binary, "=",
(misc:url_encode(Value))/binary>>
|| {Key, Value} <- Params], || {Key, Value} <- Params],
Tail = iolist_to_binary([ParHead | ParTail]), <<$&, Encoded/binary>> = iolist_to_binary(L),
binary_to_list(<<Base/binary, $?, Tail/binary>>). <<Url/binary, $?, Encoded/binary>>.
url(Server, Path, Params) ->
case binary:split(base_url(Server, Path), <<"?">>) of
[Url] ->
url(Url, Params);
[Url, Extra] ->
Custom = [list_to_tuple(binary:split(P, <<"=">>))
|| P <- binary:split(Extra, <<"&">>, [global])],
url(Url, Custom++Params)
end.
-spec opt_type(ext_api_http_pool_size) -> fun((pos_integer()) -> pos_integer()); -spec opt_type(ext_api_http_pool_size) -> fun((pos_integer()) -> pos_integer());
(ext_api_url) -> fun((binary()) -> binary()); (ext_api_url) -> fun((binary()) -> binary());
(atom()) -> [atom()]. (atom()) -> [atom()].
opt_type(ext_api_http_pool_size) -> opt_type(ext_api_http_pool_size) ->
fun (X) when is_integer(X), X > 0 -> X end; fun (X) when is_integer(X), X > 0 -> X end;
opt_type(ext_api_url) -> opt_type(ext_api_url) ->