Unify with_decode and with_decode_opts. This has been overdue and will make the next bit easier.

This commit is contained in:
Simon MacMullen 2011-03-10 16:49:15 +00:00
parent f11a34884f
commit c383acd2d2
7 changed files with 22 additions and 38 deletions

View File

@ -19,7 +19,7 @@
-export([is_authorized/2, is_authorized_admin/2, vhost/1]).
-export([is_authorized_vhost/2, is_authorized/3, is_authorized_user/3]).
-export([bad_request/3, id/2, parse_bool/1, parse_int/1]).
-export([with_decode/4, with_decode_opts/4, not_found/3, amqp_request/4]).
-export([with_decode/4, not_found/3, amqp_request/4]).
-export([with_channel/4, with_channel/5]).
-export([props_to_method/2]).
-export([all_or_one_vhost/2, http_to_amqp/5, reply/3, filter_vhost/3]).
@ -197,44 +197,28 @@ id0(Key, ReqData) ->
error -> none
end.
%% TODO unify this with the function below after bug23384 is merged
with_decode_opts(Keys, ReqData, Context, Fun) ->
Body = wrq:req_body(ReqData),
case decode(Keys, Body) of
{error, Reason} -> bad_request(Reason, ReqData, Context);
_Values -> try
{ok, Obj0} = decode(Body),
Obj = [{list_to_atom(binary_to_list(K)), V} ||
{K, V} <- Obj0],
Fun(Obj)
catch {error, Error} ->
bad_request(Error, ReqData, Context)
end
end.
with_decode(Keys, ReqData, Context, Fun) ->
with_decode(Keys, wrq:req_body(ReqData), ReqData, Context, Fun).
with_decode(Keys, Body, ReqData, Context, Fun) ->
case decode(Keys, Body) of
{error, Reason} -> bad_request(Reason, ReqData, Context);
Values -> try
Fun(Values)
catch {error, Error} ->
bad_request(Error, ReqData, Context)
end
{error, Reason} -> bad_request(Reason, ReqData, Context);
{ok, Values, JSON} -> try
Fun(Values, JSON)
catch {error, Error} ->
bad_request(Error, ReqData, Context)
end
end.
decode(Keys, Body) ->
case decode(Body) of
{ok, J} -> Results =
[get_or_missing(list_to_binary(atom_to_list(K)), J) ||
K <- Keys],
case [E || E = {key_missing, _} <- Results] of
[] -> Results;
Errors -> {error, Errors}
end;
Else -> Else
{ok, J0} -> J = [{list_to_atom(binary_to_list(K)), V} || {K, V} <- J0],
Results = [get_or_missing(K, J) || K <- Keys],
case [E || E = {key_missing, _} <- Results] of
[] -> {ok, Results, J};
Errors -> {error, Errors}
end;
Else -> Else
end.
decode(Body) ->

View File

@ -99,7 +99,7 @@ accept(Body, ReqData, Context) ->
rabbit_mgmt_util:with_decode(
[users, vhosts, permissions, queues, exchanges, bindings],
Body, ReqData, Context,
fun([Users, VHosts, Permissions, Queues, Exchanges, Bindings]) ->
fun([Users, VHosts, Permissions, Queues, Exchanges, Bindings], _) ->
try
for_all(Users, fun add_user/1),
for_all(VHosts, fun add_vhost/1),

View File

@ -81,7 +81,7 @@ accept_content(ReqData, {_Mode, Context}) ->
{true, ReqData, Context2} ->
rabbit_mgmt_util:with_decode(
[routing_key, arguments], ReqData, Context,
fun([Key, Args]) ->
fun([Key, Args], _) ->
Loc = rabbit_mochiweb_util:relativise(
wrq:path(ReqData),
binary_to_list(

View File

@ -44,7 +44,7 @@ do_it(ReqData, Context) ->
X = rabbit_mgmt_util:id(exchange, ReqData),
rabbit_mgmt_util:with_decode(
[routing_key, properties, payload, payload_encoding], ReqData, Context,
fun([RoutingKey, Props0, Payload0, Enc]) ->
fun([RoutingKey, Props0, Payload0, Enc], _) ->
rabbit_mgmt_util:with_channel(
VHost, ReqData, Context,
fun (Ch) ->

View File

@ -57,7 +57,7 @@ accept_content(ReqData, Context) ->
VHost = rabbit_mgmt_util:id(vhost, ReqData),
rabbit_mgmt_util:with_decode(
[configure, write, read], ReqData, Context,
fun([Conf, Write, Read]) ->
fun([Conf, Write, Read], _) ->
rabbit_auth_backend_internal:set_permissions(
User, VHost, Conf, Write, Read),
{true, ReqData, Context}

View File

@ -45,7 +45,7 @@ do_it(ReqData, Context) ->
Q = rabbit_mgmt_util:id(queue, ReqData),
rabbit_mgmt_util:with_decode(
[requeue, count, encoding], ReqData, Context,
fun([RequeueBin, CountBin, EncBin]) ->
fun([RequeueBin, CountBin, EncBin], _) ->
rabbit_mgmt_util:with_channel(
VHost, ReqData, Context,
fun (Ch) ->

View File

@ -48,9 +48,9 @@ to_json(ReqData, Context) ->
accept_content(ReqData, Context) ->
Username = rabbit_mgmt_util:id(user, ReqData),
rabbit_mgmt_util:with_decode_opts(
[administrator], ReqData, Context,
fun(User) ->
rabbit_mgmt_util:with_decode(
[], ReqData, Context,
fun(_, User) ->
put_user([{name, Username} | User]),
{true, ReqData, Context}
end).