fix(api): invalid params HTTP Code 400
This commit is contained in:
parent
cc36c7e48d
commit
40ab0064ae
|
@ -77,25 +77,21 @@ apis() ->
|
||||||
-define(format_fun, {?MODULE, format_channel_info}).
|
-define(format_fun, {?MODULE, format_channel_info}).
|
||||||
|
|
||||||
clients(get, #{ bindings := #{name := Name0}
|
clients(get, #{ bindings := #{name := Name0}
|
||||||
, query_string := Qs
|
, query_string := Params
|
||||||
}) ->
|
}) ->
|
||||||
with_gateway(Name0, fun(GwName, _) ->
|
with_gateway(Name0, fun(GwName, _) ->
|
||||||
TabName = emqx_gateway_cm:tabname(info, GwName),
|
TabName = emqx_gateway_cm:tabname(info, GwName),
|
||||||
case maps:get(<<"node">>, Qs, undefined) of
|
case maps:get(<<"node">>, Params, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
Response = emqx_mgmt_api:cluster_query(
|
Response = emqx_mgmt_api:cluster_query(Params, TabName,
|
||||||
Qs, TabName,
|
?CLIENT_QS_SCHEMA, ?query_fun),
|
||||||
?CLIENT_QS_SCHEMA, ?query_fun
|
emqx_mgmt_util:generate_response(Response);
|
||||||
),
|
|
||||||
{200, Response};
|
|
||||||
Node1 ->
|
Node1 ->
|
||||||
Node = binary_to_atom(Node1, utf8),
|
Node = binary_to_atom(Node1, utf8),
|
||||||
ParamsWithoutNode = maps:without([<<"node">>], Qs),
|
ParamsWithoutNode = maps:without([<<"node">>], Params),
|
||||||
Response = emqx_mgmt_api:node_query(
|
Response = emqx_mgmt_api:node_query(Node, ParamsWithoutNode,
|
||||||
Node, ParamsWithoutNode,
|
TabName, ?CLIENT_QS_SCHEMA, ?query_fun),
|
||||||
TabName, ?CLIENT_QS_SCHEMA, ?query_fun
|
emqx_mgmt_util:generate_response(Response)
|
||||||
),
|
|
||||||
{200, Response}
|
|
||||||
end
|
end
|
||||||
end).
|
end).
|
||||||
|
|
||||||
|
|
|
@ -120,14 +120,7 @@ node_query(Node, Params, Tab, QsSchema, QueryFun) ->
|
||||||
Limit = b2i(limit(Params)),
|
Limit = b2i(limit(Params)),
|
||||||
Page = b2i(page(Params)),
|
Page = b2i(page(Params)),
|
||||||
Meta = #{page => Page, limit => Limit, count => 0},
|
Meta = #{page => Page, limit => Limit, count => 0},
|
||||||
case Meta of
|
page_limit_check_query(Meta, {fun do_node_query/5, [Node, Tab, Qs, QueryFun, Meta]}).
|
||||||
#{page := Page, limit := Limit}
|
|
||||||
when Page < 1; Limit < 1 ->
|
|
||||||
{error, page_limit_invalid};
|
|
||||||
_ ->
|
|
||||||
do_node_query(Node, Tab, Qs, QueryFun, Meta)
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
do_node_query(Node, Tab, Qs, QueryFun, Meta) ->
|
do_node_query(Node, Tab, Qs, QueryFun, Meta) ->
|
||||||
|
@ -176,13 +169,7 @@ cluster_query(Params, Tab, QsSchema, QueryFun) ->
|
||||||
Page = b2i(page(Params)),
|
Page = b2i(page(Params)),
|
||||||
Nodes = ekka_mnesia:running_nodes(),
|
Nodes = ekka_mnesia:running_nodes(),
|
||||||
Meta = #{page => Page, limit => Limit, count => 0},
|
Meta = #{page => Page, limit => Limit, count => 0},
|
||||||
case Meta of
|
page_limit_check_query(Meta, {fun do_cluster_query/5, [Nodes, Tab, Qs, QueryFun, Meta]}).
|
||||||
#{page := Page, limit := Limit}
|
|
||||||
when Page < 1; Limit < 1 ->
|
|
||||||
{error, page_limit_invalid};
|
|
||||||
_ ->
|
|
||||||
do_cluster_query(Nodes, Tab, Qs, QueryFun, Meta)
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @private
|
%% @private
|
||||||
do_cluster_query(Nodes, Tab, Qs, QueryFun, Meta) ->
|
do_cluster_query(Nodes, Tab, Qs, QueryFun, Meta) ->
|
||||||
|
@ -380,6 +367,15 @@ rows_sub_params(Len, _Meta = #{page := Page, limit := Limit, count := Count}) ->
|
||||||
{_SubStart = 1, _NeedNowNum = Len}
|
{_SubStart = 1, _NeedNowNum = Len}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
page_limit_check_query(Meta, {F, A}) ->
|
||||||
|
case Meta of
|
||||||
|
#{page := Page, limit := Limit}
|
||||||
|
when Page < 1; Limit < 1 ->
|
||||||
|
{error, page_limit_invalid};
|
||||||
|
_ ->
|
||||||
|
erlang:apply(F, A)
|
||||||
|
end.
|
||||||
|
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
%% Types
|
%% Types
|
||||||
%%--------------------------------------------------------------------
|
%%--------------------------------------------------------------------
|
||||||
|
|
|
@ -79,7 +79,7 @@ alarms(get, #{query_string := Qs}) ->
|
||||||
<<"false">> -> ?DEACTIVATED_ALARM
|
<<"false">> -> ?DEACTIVATED_ALARM
|
||||||
end,
|
end,
|
||||||
Response = emqx_mgmt_api:cluster_query(Qs, Table, [], {?MODULE, query}),
|
Response = emqx_mgmt_api:cluster_query(Qs, Table, [], {?MODULE, query}),
|
||||||
{200, Response};
|
emqx_mgmt_util:generate_response(Response);
|
||||||
|
|
||||||
alarms(delete, _Params) ->
|
alarms(delete, _Params) ->
|
||||||
_ = emqx_mgmt:delete_all_deactivated_alarms(),
|
_ = emqx_mgmt:delete_all_deactivated_alarms(),
|
||||||
|
|
|
@ -264,7 +264,8 @@ clients_api() ->
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
responses => #{
|
responses => #{
|
||||||
<<"200">> => emqx_mgmt_util:array_schema(client, <<"List clients 200 OK">>)}}},
|
<<"200">> => emqx_mgmt_util:array_schema(client, <<"List clients 200 OK">>),
|
||||||
|
<<"400">> => emqx_mgmt_util:error_schema(<<"Invalid parameters">>, ['INVALID_PARAMETER'])}}},
|
||||||
{"/clients", Metadata, clients}.
|
{"/clients", Metadata, clients}.
|
||||||
|
|
||||||
client_api() ->
|
client_api() ->
|
||||||
|
@ -435,13 +436,13 @@ list(Params) ->
|
||||||
undefined ->
|
undefined ->
|
||||||
Response = emqx_mgmt_api:cluster_query(Params, Tab,
|
Response = emqx_mgmt_api:cluster_query(Params, Tab,
|
||||||
QuerySchema, ?query_fun),
|
QuerySchema, ?query_fun),
|
||||||
{200, Response};
|
emqx_mgmt_util:generate_response(Response);
|
||||||
Node1 ->
|
Node1 ->
|
||||||
Node = binary_to_atom(Node1, utf8),
|
Node = binary_to_atom(Node1, utf8),
|
||||||
ParamsWithoutNode = maps:without([<<"node">>], Params),
|
ParamsWithoutNode = maps:without([<<"node">>], Params),
|
||||||
Response = emqx_mgmt_api:node_query(Node, ParamsWithoutNode,
|
Response = emqx_mgmt_api:node_query(Node, ParamsWithoutNode,
|
||||||
Tab, QuerySchema, ?query_fun),
|
Tab, QuerySchema, ?query_fun),
|
||||||
{200, Response}
|
emqx_mgmt_util:generate_response(Response)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
lookup(#{clientid := ClientID}) ->
|
lookup(#{clientid := ClientID}) ->
|
||||||
|
|
|
@ -37,6 +37,7 @@
|
||||||
, error_schema/2
|
, error_schema/2
|
||||||
, properties/1
|
, properties/1
|
||||||
, page_params/0
|
, page_params/0
|
||||||
|
, generate_response/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
api_spec() ->
|
api_spec() ->
|
||||||
|
@ -54,7 +55,8 @@ routes_api() ->
|
||||||
description => <<"EMQ X routes">>,
|
description => <<"EMQ X routes">>,
|
||||||
parameters => [topic_param(query) , node_param()] ++ page_params(),
|
parameters => [topic_param(query) , node_param()] ++ page_params(),
|
||||||
responses => #{
|
responses => #{
|
||||||
<<"200">> => object_array_schema(properties(), <<"List route info">>)
|
<<"200">> => object_array_schema(properties(), <<"List route info">>),
|
||||||
|
<<"400">> => error_schema(<<"Invalid parameters">>, ['INVALID_PARAMETER'])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -87,7 +89,7 @@ route(get, #{bindings := Bindings}) ->
|
||||||
%% api apply
|
%% api apply
|
||||||
list(Params) ->
|
list(Params) ->
|
||||||
Response = emqx_mgmt_api:node_query(node(), Params, emqx_route, ?ROUTES_QS_SCHEMA, {?MODULE, query}),
|
Response = emqx_mgmt_api:node_query(node(), Params, emqx_route, ?ROUTES_QS_SCHEMA, {?MODULE, query}),
|
||||||
{200, Response}.
|
generate_response(Response).
|
||||||
|
|
||||||
lookup(#{topic := Topic}) ->
|
lookup(#{topic := Topic}) ->
|
||||||
case emqx_mgmt:lookup_routes(Topic) of
|
case emqx_mgmt:lookup_routes(Topic) of
|
||||||
|
|
|
@ -21,6 +21,7 @@
|
||||||
-include_lib("emqx/include/emqx.hrl").
|
-include_lib("emqx/include/emqx.hrl").
|
||||||
|
|
||||||
-import(emqx_mgmt_util, [ page_schema/1
|
-import(emqx_mgmt_util, [ page_schema/1
|
||||||
|
, error_schema/2
|
||||||
, properties/1
|
, properties/1
|
||||||
, page_params/0
|
, page_params/0
|
||||||
]).
|
]).
|
||||||
|
@ -53,7 +54,8 @@ subscriptions_api() ->
|
||||||
description => <<"List subscriptions">>,
|
description => <<"List subscriptions">>,
|
||||||
parameters => parameters(),
|
parameters => parameters(),
|
||||||
responses => #{
|
responses => #{
|
||||||
<<"200">> => page_schema(subscription)
|
<<"200">> => page_schema(subscription),
|
||||||
|
<<"400">> => error_schema(<<"Invalid parameters">>, ['INVALID_PARAMETER'])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -114,11 +116,13 @@ list(Params) ->
|
||||||
{Tab, QuerySchema} = ?SUBS_QS_SCHEMA,
|
{Tab, QuerySchema} = ?SUBS_QS_SCHEMA,
|
||||||
case maps:get(<<"node">>, Params, undefined) of
|
case maps:get(<<"node">>, Params, undefined) of
|
||||||
undefined ->
|
undefined ->
|
||||||
{200, emqx_mgmt_api:cluster_query(Params, Tab,
|
Response = emqx_mgmt_api:cluster_query(Params, Tab,
|
||||||
QuerySchema, ?query_fun)};
|
QuerySchema, ?query_fun),
|
||||||
|
emqx_mgmt_util:generate_response(Response);
|
||||||
Node ->
|
Node ->
|
||||||
{200, emqx_mgmt_api:node_query(binary_to_atom(Node, utf8), Params,
|
Response = emqx_mgmt_api:node_query(binary_to_atom(Node, utf8), Params,
|
||||||
Tab, QuerySchema, ?query_fun)}
|
Tab, QuerySchema, ?query_fun),
|
||||||
|
emqx_mgmt_util:generate_response(Response)
|
||||||
end.
|
end.
|
||||||
|
|
||||||
format(Items) when is_list(Items) ->
|
format(Items) when is_list(Items) ->
|
||||||
|
|
|
@ -43,6 +43,7 @@
|
||||||
, batch_schema/1
|
, batch_schema/1
|
||||||
]).
|
]).
|
||||||
|
|
||||||
|
-export([generate_response/1]).
|
||||||
|
|
||||||
|
|
||||||
-export([urldecode/1]).
|
-export([urldecode/1]).
|
||||||
|
@ -258,3 +259,13 @@ bad_request() ->
|
||||||
bad_request(Desc) ->
|
bad_request(Desc) ->
|
||||||
object_schema(properties([{message, string}, {code, string}]), Desc).
|
object_schema(properties([{message, string}, {code, string}]), Desc).
|
||||||
|
|
||||||
|
%%%==============================================================================================
|
||||||
|
%% Response util
|
||||||
|
|
||||||
|
generate_response(QueryResult) ->
|
||||||
|
case QueryResult of
|
||||||
|
{error, page_limit_invalid} ->
|
||||||
|
{400, #{code => <<"INVALID_PARAMETER">>, message => <<"page_limit_invalid">>}};
|
||||||
|
Response ->
|
||||||
|
{200, Response}
|
||||||
|
end.
|
||||||
|
|
Loading…
Reference in New Issue