Skip to content

Commit

Permalink
Create individual function for each HTTP method
Browse files Browse the repository at this point in the history
  • Loading branch information
chrzaszcz committed Aug 3, 2016
1 parent 88b0cca commit 8d409da
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
58 changes: 33 additions & 25 deletions apps/ejabberd/src/mod_http_client.erl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
-behaviour(gen_mod).

%% API
-export([start_pool/3, stop_pool/2, get_pool/2, make_request/5]).
-export([start_pool/3, stop_pool/2, get_pool/2, get/3, post/4]).

%% gen_mod callbacks
-export([start/2, stop/1]).
Expand Down Expand Up @@ -59,30 +59,15 @@ stop_pool(Host, Name) ->
ets:delete(tab_name(Host), Name),
ok.

-spec make_request(pool(), binary(), binary(), list(), binary()) ->
{ok, {binary(), binary()}} | {error, any()}.
make_request(Pool, Path, Method, Header, Query) ->
#pool{path_prefix = PathPrefix,
name = Name,
host = Host,
pool_timeout = PoolTimeout,
request_timeout = RequestTimeout} = Pool,
FullPath = <<PathPrefix/binary, Path/binary>>,
case catch poolboy:transaction(
pool_proc_name(Host, Name),
fun(WorkerPid) ->
fusco:request(WorkerPid, FullPath, Method, Header, Query, RequestTimeout)
end,
PoolTimeout) of
{'EXIT', {timeout, _}} ->
{error, pool_timeout};
{ok, {{Code, _Reason}, _RespHeaders, RespBody, _, _}} ->
{ok, {Code, RespBody}};
{error, timeout} ->
{error, request_timeout};
{error, Reason} ->
{error, Reason}
end.
-spec get(pool(), binary(), list()) ->
{ok, {binary(), binary()}} | {error, any()}.
get(Pool, Path, Headers) ->
make_request(Pool, Path, <<"GET">>, Headers, <<>>).

-spec post(pool(), binary(), list(), binary()) ->
{ok, {binary(), binary()}} | {error, any()}.
post(Pool, Path, Headers, Query) ->
make_request(Pool, Path, <<"POST">>, Headers, Query).

%%------------------------------------------------------------------------------
%% gen_mod callbacks
Expand Down Expand Up @@ -142,6 +127,29 @@ tab_name(Host) ->
%% NOTE: The naming scheme for processes is reused for ETS table
gen_mod:get_module_proc(Host, mod_http_client_pools).

make_request(Pool, Path, Method, Headers, Query) ->
#pool{path_prefix = PathPrefix,
name = Name,
host = Host,
pool_timeout = PoolTimeout,
request_timeout = RequestTimeout} = Pool,
FullPath = <<PathPrefix/binary, Path/binary>>,
case catch poolboy:transaction(
pool_proc_name(Host, Name),
fun(WorkerPid) ->
fusco:request(WorkerPid, FullPath, Method, Headers, Query, RequestTimeout)
end,
PoolTimeout) of
{'EXIT', {timeout, _}} ->
{error, pool_timeout};
{ok, {{Code, _Reason}, _RespHeaders, RespBody, _, _}} ->
{ok, {Code, RespBody}};
{error, timeout} ->
{error, request_timeout};
{error, Reason} ->
{error, Reason}
end.

start_supervisor(Host) ->
Proc = gen_mod:get_module_proc(Host, ejabberd_mod_http_client_sup),
ChildSpec =
Expand Down
2 changes: 1 addition & 1 deletion apps/ejabberd/src/mod_muc_room.erl
Original file line number Diff line number Diff line change
Expand Up @@ -1893,7 +1893,7 @@ make_http_auth_request(From, Nick, Packet, Role, RoomJid, RoomPid, Password, Poo
Path = <<"/check_password?from=", FromVal/binary,
"&to=", RoomJidVal/binary,
"&pass=", PassVal/binary>>,
Result = case mod_http_client:make_request(Pool, Path, <<"GET">>, [], <<>>) of
Result = case mod_http_client:get(Pool, Path, []) of
{ok, {<<"200">>, Body}} -> decode_http_auth_response(Body);
_ -> error
end,
Expand Down

0 comments on commit 8d409da

Please sign in to comment.