From 265c7b62c735010bcdcaa39f17c00a2a07157551 Mon Sep 17 00:00:00 2001 From: Christophe Romain Date: Wed, 18 Apr 2018 13:16:08 +0200 Subject: [PATCH] Add flexibility on rest url config --- src/rest.erl | 55 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/rest.erl b/src/rest.erl index 7e509dec8..c954083aa 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, - 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"). @@ -124,16 +124,16 @@ request(Server, Method, Path, Params, Mime, Data) -> End = os:timestamp(), Elapsed = timer:now_diff(End, Begin) div 1000, %% time in ms ejabberd_hooks:run(backend_api_response_time, Server, - [Server, Method, Path, Elapsed]); + [Server, Method, Path, Elapsed]); {error, {http_error,{error,timeout}}} -> ejabberd_hooks:run(backend_api_timeout, Server, - [Server, Method, Path]); + [Server, Method, Path]); {error, {http_error,{error,connect_timeout}}} -> ejabberd_hooks:run(backend_api_timeout, Server, - [Server, Method, Path]); + [Server, Method, Path]); {error, _} -> ejabberd_hooks:run(backend_api_error, Server, - [Server, Method, Path]) + [Server, Method, Path]) end, Result. @@ -154,32 +154,47 @@ encode_json(Content) -> end. base_url(Server, Path) -> - Tail = case iolist_to_binary(Path) of + BPath = case iolist_to_binary(Path) of <<$/, Ok/binary>> -> Ok; Ok -> Ok end, - case Tail of - <<"http", _Url/binary>> -> Tail; + Url = case BPath of + <<"http", _/binary>> -> BPath; _ -> Base = ejabberd_config:get_option({ext_api_url, Server}, + fun(X) -> iolist_to_binary(X) end, <<"http://localhost/api">>), - <> + case binary:last(Base) of + 47 -> <>; + _ -> <> + end + end, + case binary:last(Url) of + 47 -> binary_part(Url, 0, size(Url)-1); + _ -> Url end. -url(Server, Path, []) -> - binary_to_list(base_url(Server, Path)); -url(Server, Path, Params) -> - Base = base_url(Server, Path), - [<<$&, ParHead/binary>> | ParTail] = - [<<"&", (iolist_to_binary(Key))/binary, "=", - (misc:url_encode(Value))/binary>> +url(Url, []) -> + Url; +url(Url, Params) -> + L = [<<"&", (iolist_to_binary(Key))/binary, "=", + (misc:url_encode(Value))/binary>> || {Key, Value} <- Params], - Tail = iolist_to_binary([ParHead | ParTail]), - binary_to_list(<>). + <<$&, Encoded/binary>> = iolist_to_binary(L), + <>. +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()); - (ext_api_url) -> fun((binary()) -> binary()); - (atom()) -> [atom()]. + (ext_api_url) -> fun((binary()) -> binary()); + (atom()) -> [atom()]. opt_type(ext_api_http_pool_size) -> fun (X) when is_integer(X), X > 0 -> X end; opt_type(ext_api_url) ->