24
1
mirror of https://github.com/processone/ejabberd.git synced 2024-06-02 21:17:12 +02:00

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

View File

@ -154,28 +154,43 @@ 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] =
[<<"&", (iolist_to_binary(Key))/binary, "=",
(misc:url_encode(Value))/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());