From 2b12fa1c053908f7df4a0546d952f7eb7b86acc5 Mon Sep 17 00:00:00 2001 From: DDDHuang <904897578@qq.com> Date: Fri, 23 Jul 2021 14:43:10 +0800 Subject: [PATCH] chore: api code format; fix: atom refname; update minirest version --- .../src/emqx_mgmt_api_alarms.erl | 8 +- .../src/emqx_mgmt_api_apps.erl | 45 ++++----- .../src/emqx_mgmt_api_clients.erl | 96 +++++++++---------- .../src/emqx_mgmt_api_listeners.erl | 22 ++--- .../src/emqx_mgmt_api_metrics.erl | 9 +- .../src/emqx_mgmt_api_nodes.erl | 34 ++++--- .../src/emqx_mgmt_api_publish.erl | 17 ++-- .../src/emqx_mgmt_api_routes.erl | 21 ++-- .../src/emqx_mgmt_api_stats.erl | 9 +- .../src/emqx_mgmt_api_status.erl | 2 +- .../src/emqx_mgmt_api_subscriptions.erl | 6 +- apps/emqx_management/src/emqx_mgmt_util.erl | 14 ++- .../test/emqx_mgmt_clients_api_SUITE.erl | 1 + .../test/emqx_mgmt_publish_api_SUITE.erl | 4 +- rebar.config | 2 +- 15 files changed, 140 insertions(+), 150 deletions(-) diff --git a/apps/emqx_management/src/emqx_mgmt_api_alarms.erl b/apps/emqx_management/src/emqx_mgmt_api_alarms.erl index 834fdc0bb..36a0f3a5b 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_alarms.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_alarms.erl @@ -16,7 +16,7 @@ -module(emqx_mgmt_api_alarms). --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -58,7 +58,7 @@ alarm_schema() -> alarms_api() -> Metadata = #{ get => #{ - description => "EMQ X alarms", + description => <<"EMQ X alarms">>, parameters => [#{ name => activated, in => query, @@ -68,9 +68,9 @@ alarms_api() -> }], responses => #{ <<"200">> => - emqx_mgmt_util:response_array_schema(<<"List all alarms">>, <<"alarm">>)}}, + emqx_mgmt_util:response_array_schema(<<"List all alarms">>, alarm)}}, delete => #{ - description => "Remove all deactivated alarms", + description => <<"Remove all deactivated alarms">>, responses => #{ <<"200">> => emqx_mgmt_util:response_schema(<<"Remove all deactivated alarms ok">>)}}}, diff --git a/apps/emqx_management/src/emqx_mgmt_api_apps.erl b/apps/emqx_management/src/emqx_mgmt_api_apps.erl index 46dd34432..2a5f330c4 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_apps.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_apps.erl @@ -16,7 +16,7 @@ -module(emqx_mgmt_api_apps). --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -76,17 +76,17 @@ app_without_secret_schema() -> apps_api() -> Metadata = #{ get => #{ - description => "List EMQ X apps", + description => <<"List EMQ X apps">>, responses => #{ <<"200">> => emqx_mgmt_util:response_array_schema(<<"All apps">>, app_without_secret_schema())}}, post => #{ - description => "EMQ X create apps", + description => <<"EMQ X create apps">>, 'requestBody' => emqx_mgmt_util:request_body_schema(<<"app">>), responses => #{ <<"200">> => - emqx_mgmt_util:response_schema(<<"Create apps">>, <<"app_secret">>), + emqx_mgmt_util:response_schema(<<"Create apps">>, app_secret), <<"400">> => emqx_mgmt_util:response_error_schema(<<"App ID already exist">>, [?BAD_APP_ID])}}}, {"/apps", Metadata, apps}. @@ -94,30 +94,29 @@ apps_api() -> app_api() -> Metadata = #{ get => #{ - description => "EMQ X apps", + description => <<"EMQ X apps">>, parameters => [#{ name => app_id, in => path, required => true, - schema => #{type => string}, - example => <<"admin">>}], + schema => #{type => string}}], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"App id not found">>), <<"200">> => - emqx_mgmt_util:response_schema("Get App", app_without_secret_schema())}}, + emqx_mgmt_util:response_schema(<<"Get App">>, app_without_secret_schema())}}, delete => #{ - description => "EMQ X apps", + description => <<"EMQ X apps">>, parameters => [#{ name => app_id, in => path, required => true, - schema => #{type => string}, - example => <<"admin">>}], + schema => #{type => string} + }], responses => #{ - <<"200">> => emqx_mgmt_util:response_schema("Remove app ok")}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Remove app ok">>)}}, put => #{ - description => "EMQ X update apps", + description => <<"EMQ X update apps">>, parameters => [#{ name => app_id, in => path, @@ -175,23 +174,19 @@ app(put, Request) -> %%%============================================================================================== %% api apply list(_) -> - Data = [format_without_app_secret(Apps) || Apps <- emqx_mgmt_auth:list_apps()], - Response = emqx_json:encode(Data), - {200, Response}. + {200, [format_without_app_secret(Apps) || Apps <- emqx_mgmt_auth:list_apps()]}. create(#{app_id := AppID, name := Name, secret := Secret, desc := Desc, status := Status, expired := Expired}) -> case emqx_mgmt_auth:add_app(AppID, Name, Secret, Desc, Status, Expired) of {ok, AppSecret} -> - Response = emqx_json:encode(#{secret => AppSecret}), - {200, Response}; + {200, #{secret => AppSecret}}; {error, alread_existed} -> Message = list_to_binary(io_lib:format("appid ~p already existed", [AppID])), - {400, #{code => 'BAD_APP_ID', reason => Message}}; + {400, #{code => 'BAD_APP_ID', message => Message}}; {error, Reason} -> - Data = #{code => 'UNKNOW_ERROR', - reason => list_to_binary(io_lib:format("~p", [Reason]))}, - Response = emqx_json:encode(Data), + Response = #{code => 'UNKNOW_ERROR', + message => list_to_binary(io_lib:format("~p", [Reason]))}, {500, Response} end. @@ -200,8 +195,7 @@ lookup(#{app_id := AppID}) -> undefined -> {404, ?APP_ID_NOT_FOUND}; App -> - Data = format_with_app_secret(App), - Response = emqx_json:encode(Data), + Response = format_with_app_secret(App), {200, Response} end. @@ -216,8 +210,7 @@ update(App = #{app_id := AppID, name := Name, desc := Desc, status := Status, ex {error, not_found} -> {404, ?APP_ID_NOT_FOUND}; {error, Reason} -> - Data = #{code => 'UNKNOW_ERROR', reason => list_to_binary(io_lib:format("~p", [Reason]))}, - Response = emqx_json:encode(Data), + Response = #{code => 'UNKNOW_ERROR', message => list_to_binary(io_lib:format("~p", [Reason]))}, {500, Response} end. diff --git a/apps/emqx_management/src/emqx_mgmt_api_clients.erl b/apps/emqx_management/src/emqx_mgmt_api_clients.erl index bd73f381d..6e7b4f687 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_clients.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_clients.erl @@ -16,7 +16,7 @@ -module(emqx_mgmt_api_clients). --behavior(minirest_api). +-behaviour(minirest_api). -include_lib("emqx/include/emqx.hrl"). @@ -214,112 +214,106 @@ schemas() -> clients_api() -> Metadata = #{ get => #{ - description => "List clients", + description => <<"List clients">>, responses => #{ - <<"200">> => emqx_mgmt_util:response_array_schema(<<"List clients 200 OK">>, <<"client">>)}}}, + <<"200">> => emqx_mgmt_util:response_array_schema(<<"List clients 200 OK">>, client)}}}, {"/clients", Metadata, clients}. client_api() -> Metadata = #{ get => #{ - description => "Get clients info by client ID", + description => <<"Get clients info by client ID">>, parameters => [#{ name => clientid, in => path, schema => #{type => string}, - required => true, - example => 123456}], + required => true + }], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), - <<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, <<"client">>)}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, client)}}, delete => #{ - description => "Kick out client by client ID", + description => <<"Kick out client by client ID">>, parameters => [#{ name => clientid, in => path, schema => #{type => string}, - required => true, - example => 123456}], + required => true + }], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), - <<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, <<"client">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, client)}}}, {"/clients/:clientid", Metadata, client}. clients_acl_cache_api() -> Metadata = #{ get => #{ - description => "Get client acl cache", + description => <<"Get client acl cache">>, parameters => [#{ name => clientid, in => path, schema => #{type => string}, - required => true, - example => 123456}], + required => true + }], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), - <<"200">> => emqx_mgmt_util:response_schema(<<"List clients 200 OK">>, <<"acl_cache">>)}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Get client acl cache">>, acl_cache)}}, delete => #{ - description => "Clean client acl cache", + description => <<"Clean client acl cache">>, parameters => [#{ name => clientid, in => path, schema => #{type => string}, - required => true, - example => 123456}], + required => true + }], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), - <<"200">> => emqx_mgmt_util:response_schema(<<"Delete clients 200 OK">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Delete clients acl cache OK">>)}}}, {"/clients/:clientid/acl_cache", Metadata, acl_cache}. subscribe_api() -> Metadata = #{ post => #{ - description => "subscribe", - parameters => [ - #{ - name => clientid, - in => path, - schema => #{type => string}, - required => true, - example => 123456 - } - ], + description => <<"Subscribe">>, + parameters => [#{ + name => clientid, + in => path, + schema => #{type => string}, + required => true + }], 'requestBody' => emqx_mgmt_util:request_body_schema(#{ type => object, properties => #{ - <<"topic">> => #{ + topic => #{ type => string, - example => <<"topic_1">>, description => <<"Topic">>}, - <<"qos">> => #{ + qos => #{ type => integer, enum => [0, 1, 2], example => 0, description => <<"QoS">>}}}), responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), - <<"200">> => emqx_mgmt_util:response_schema(<<"subscribe ok">>)}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Subscribe ok">>)}}, delete => #{ - description => "unsubscribe", + description => <<"Unsubscribe">>, parameters => [ #{ name => clientid, in => path, schema => #{type => string}, - required => true, - example => 123456 + required => true }, #{ name => topic, in => query, schema => #{type => string}, - required => true, - example => <<"topic_1">> + required => true } ], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Client id not found">>), - <<"200">> => emqx_mgmt_util:response_schema(<<"unsubscribe ok">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Unsubscribe ok">>)}}}, {"/clients/:clientid/subscribe", Metadata, subscribe}. %%%============================================================================================== @@ -373,17 +367,15 @@ subscribe_batch(post, Request) -> %% api apply list(Params) -> - Data = emqx_mgmt_api:cluster_query(maps:to_list(Params), ?CLIENT_QS_SCHEMA, ?query_fun), - Body = emqx_json:encode(Data), - {200, Body}. + Response = emqx_mgmt_api:cluster_query(maps:to_list(Params), ?CLIENT_QS_SCHEMA, ?query_fun), + {200, Response}. lookup(#{clientid := ClientID}) -> case emqx_mgmt:lookup_client({clientid, ClientID}, ?format_fun) of [] -> {404, ?CLIENT_ID_NOT_FOUND}; ClientInfo -> - Response = emqx_json:encode(hd(ClientInfo)), - {200, Response} + {200, hd(ClientInfo)} end. kickout(#{clientid := ClientID}) -> @@ -395,9 +387,10 @@ get_acl_cache(#{clientid := ClientID})-> {error, not_found} -> {404, ?CLIENT_ID_NOT_FOUND}; {error, Reason} -> - {500, #{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}}; + Message = list_to_binary(io_lib:format("~p", [Reason])), + {500, #{code => <<"UNKNOW_ERROR">>, message => Message}}; Caches -> - Response = emqx_json:encode([format_acl_cache(Cache) || Cache <- Caches]), + Response = [format_acl_cache(Cache) || Cache <- Caches], {200, Response} end. @@ -408,7 +401,8 @@ clean_acl_cache(#{clientid := ClientID}) -> {error, not_found} -> {404, ?CLIENT_ID_NOT_FOUND}; {error, Reason} -> - {500, #{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}} + Message = list_to_binary(io_lib:format("~p", [Reason])), + {500, #{code => <<"UNKNOW_ERROR">>, message => Message}} end. subscribe(#{clientid := ClientID, topic := Topic, qos := Qos}) -> @@ -416,8 +410,8 @@ subscribe(#{clientid := ClientID, topic := Topic, qos := Qos}) -> {error, channel_not_found} -> {404, ?CLIENT_ID_NOT_FOUND}; {error, Reason} -> - Body = emqx_json:encode(#{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}), - {500, Body}; + Message = list_to_binary(io_lib:format("~p", [Reason])), + {500, #{code => <<"UNKNOW_ERROR">>, message => Message}}; ok -> {200} end. @@ -427,8 +421,8 @@ unsubscribe(#{clientid := ClientID, topic := Topic}) -> {error, channel_not_found} -> {404, ?CLIENT_ID_NOT_FOUND}; {error, Reason} -> - Body = emqx_json:encode(#{code => <<"UNKNOW_ERROR">>, reason => io_lib:format("~p", [Reason])}), - {500, Body}; + Message = list_to_binary(io_lib:format("~p", [Reason])), + {500, #{code => <<"UNKNOW_ERROR">>, message => Message}}; {unsubscribe, [{Topic, #{}}]} -> {200} end. diff --git a/apps/emqx_management/src/emqx_mgmt_api_listeners.erl b/apps/emqx_management/src/emqx_mgmt_api_listeners.erl index 34e16bc53..e845d2679 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_listeners.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_listeners.erl @@ -16,7 +16,7 @@ -module(emqx_mgmt_api_listeners). --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -78,28 +78,28 @@ listener_schema() -> listeners_api() -> Metadata = #{ get => #{ - description => "List listeners in cluster", + description => <<"List listeners in cluster">>, responses => #{ <<"200">> => - emqx_mgmt_util:response_array_schema(<<"List all listeners">>, <<"listener">>)}}}, + emqx_mgmt_util:response_array_schema(<<"List all listeners">>, listener)}}}, {"/listeners", Metadata, listeners}. restart_listeners_api() -> Metadata = #{ get => #{ - description => "List listeners by listener ID", + description => <<"List listeners by listener ID">>, parameters => [param_path_identifier()], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Listener id not found">>, ['BAD_LISTENER_ID']), <<"200">> => - emqx_mgmt_util:response_array_schema(<<"List listener info ok">>, <<"listener">>)}}}, + emqx_mgmt_util:response_array_schema(<<"List listener info ok">>, listener)}}}, {"/listeners/:identifier", Metadata, listener}. manage_listeners_api() -> Metadata = #{ get => #{ - description => "Restart listeners in cluster", + description => <<"Restart listeners in cluster">>, parameters => [ param_path_identifier(), param_path_operation()], @@ -119,7 +119,7 @@ manage_listeners_api() -> manage_nodes_listeners_api() -> Metadata = #{ get => #{ - description => "Restart listeners in cluster", + description => <<"Restart listeners in cluster">>, parameters => [ param_path_node(), param_path_identifier(), @@ -140,26 +140,26 @@ manage_nodes_listeners_api() -> nodes_listeners_api() -> Metadata = #{ get => #{ - description => "Get listener info in one node", + description => <<"Get listener info in one node">>, parameters => [param_path_node(), param_path_identifier()], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Node name or listener id not found">>, ['BAD_NODE_NAME', 'BAD_LISTENER_ID']), <<"200">> => - emqx_mgmt_util:response_schema(<<"Get listener info ok">>, <<"listener">>)}}}, + emqx_mgmt_util:response_schema(<<"Get listener info ok">>, listener)}}}, {"/nodes/:node/listeners/:identifier", Metadata, node_listener}. nodes_listener_api() -> Metadata = #{ get => #{ - description => "List listeners in one node", + description => <<"List listeners in one node">>, parameters => [param_path_node()], responses => #{ <<"404">> => emqx_mgmt_util:response_error_schema(<<"Listener id not found">>), <<"200">> => - emqx_mgmt_util:response_schema(<<"Get listener info ok">>, <<"listener">>)}}}, + emqx_mgmt_util:response_schema(<<"Get listener info ok">>, listener)}}}, {"/nodes/:node/listeners", Metadata, node_listeners}. %%%============================================================================================== %% parameters diff --git a/apps/emqx_management/src/emqx_mgmt_api_metrics.erl b/apps/emqx_management/src/emqx_mgmt_api_metrics.erl index ed9de428d..c4a34442d 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_metrics.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_metrics.erl @@ -16,7 +16,7 @@ -module(emqx_mgmt_api_metrics). --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -283,13 +283,12 @@ metrics_schema() -> metrics_api() -> Metadata = #{ get => #{ - description => "EMQ X metrics", + description => <<"EMQ X metrics">>, responses => #{ - <<"200">> => emqx_mgmt_util:response_schema(<<"List all metrics">>, <<"metrics">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"List all metrics">>, metrics)}}}, {"/metrics", Metadata, list}. %%%============================================================================================== %% api apply list(get, _) -> - Response = emqx_json:encode(emqx_mgmt:get_metrics()), - {200, Response}. + {200, emqx_mgmt:get_metrics()}. diff --git a/apps/emqx_management/src/emqx_mgmt_api_nodes.erl b/apps/emqx_management/src/emqx_mgmt_api_nodes.erl index bd92abf98..bd00b173f 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_nodes.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_nodes.erl @@ -15,7 +15,7 @@ %%-------------------------------------------------------------------- -module(emqx_mgmt_api_nodes). --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -103,15 +103,15 @@ node_schema() -> nodes_api() -> Metadata = #{ get => #{ - description => "List EMQ X nodes", + description => <<"List EMQ X nodes">>, responses => #{ - <<"200">> => emqx_mgmt_util:response_array_schema(<<"List EMQ X Nodes">>, <<"node">>)}}}, + <<"200">> => emqx_mgmt_util:response_array_schema(<<"List EMQ X Nodes">>, node)}}}, {"/nodes", Metadata, nodes}. node_api() -> Metadata = #{ get => #{ - description => "Get node info", + description => <<"Get node info">>, parameters => [#{ name => node_name, in => path, @@ -121,13 +121,13 @@ node_api() -> example => node()}], responses => #{ <<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']), - <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Nodes info by name">>, <<"node">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Nodes info by name">>, node)}}}, {"/nodes/:node_name", Metadata, node}. node_metrics_api() -> Metadata = #{ get => #{ - description => "Get node metrics", + description => <<"Get node metrics">>, parameters => [#{ name => node_name, in => path, @@ -137,13 +137,13 @@ node_metrics_api() -> example => node()}], responses => #{ <<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']), - <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Metrics">>, <<"metrics">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Metrics">>, metrics)}}}, {"/nodes/:node_name/metrics", Metadata, node_metrics}. node_stats_api() -> Metadata = #{ get => #{ - description => "Get node stats", + description => <<"Get node stats">>, parameters => [#{ name => node_name, in => path, @@ -153,7 +153,7 @@ node_stats_api() -> example => node()}], responses => #{ <<"400">> => emqx_mgmt_util:response_error_schema(<<"Node error">>, ['SOURCE_ERROR']), - <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Stats">>, <<"stats">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"Get EMQ X Node Stats">>, stats)}}}, {"/nodes/:node_name/stats", Metadata, node_metrics}. %%%============================================================================================== @@ -177,32 +177,30 @@ node_stats(get, Request) -> %% api apply list(#{}) -> NodesInfo = [format(Node, NodeInfo) || {Node, NodeInfo} <- emqx_mgmt:list_nodes()], - Response = emqx_json:encode(NodesInfo), - {200, Response}. + {200, NodesInfo}. get_node(#{node := Node}) -> case emqx_mgmt:lookup_node(Node) of #{node_status := 'ERROR'} -> - {400, emqx_json:encode(#{code => 'SOURCE_ERROR', reason => <<"rpc_failed">>})}; + {400, #{code => 'SOURCE_ERROR', message => <<"rpc_failed">>}}; NodeInfo -> - Response = emqx_json:encode(format(Node, NodeInfo)), - {200, Response} + {200, format(Node, NodeInfo)} end. get_metrics(#{node := Node}) -> case emqx_mgmt:get_metrics(Node) of {error, _} -> - {400, emqx_json:encode(#{code => 'SOURCE_ERROR', reason => <<"rpc_failed">>})}; + {400, #{code => 'SOURCE_ERROR', message => <<"rpc_failed">>}}; Metrics -> - {200, emqx_json:encode(Metrics)} + {200, Metrics} end. get_stats(#{node := Node}) -> case emqx_mgmt:get_stats(Node) of {error, _} -> - {400, emqx_json:encode(#{code => 'SOURCE_ERROR', reason => <<"rpc_failed">>})}; + {400, #{code => 'SOURCE_ERROR', message => <<"rpc_failed">>}}; Stats -> - {200, emqx_json:encode(Stats)} + {200, Stats} end. %%============================================================================================================ diff --git a/apps/emqx_management/src/emqx_mgmt_api_publish.erl b/apps/emqx_management/src/emqx_mgmt_api_publish.erl index 30fc9beab..29e162b11 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_publish.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_publish.erl @@ -17,7 +17,7 @@ %% API -include_lib("emqx/include/emqx.hrl"). --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -33,7 +33,7 @@ api_spec() -> publish_api() -> MeteData = #{ post => #{ - description => "publish", + description => <<"Publish">>, 'requestBody' => #{ content => #{ 'application/json' => #{ @@ -41,13 +41,13 @@ publish_api() -> type => object, properties => maps:with([id], message_properties())}}}}, responses => #{ - <<"200">> => emqx_mgmt_util:response_schema(<<"publish ok">>, <<"message">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"publish ok">>, message)}}}, {"/publish", MeteData, publish}. publish_batch_api() -> MeteData = #{ post => #{ - description => "publish", + description => <<"publish">>, 'requestBody' => #{ content => #{ 'application/json' => #{ @@ -57,8 +57,8 @@ publish_batch_api() -> type => object, properties => maps:with([id], message_properties())}}}}}, responses => #{ - <<"200">> => emqx_mgmt_util:response_array_schema(<<"publish ok">>, <<"message">>)}}}, - {"/publish_batch", MeteData, publish_batch}. + <<"200">> => emqx_mgmt_util:response_array_schema(<<"publish ok">>, message)}}}, + {"/publish/bulk", MeteData, publish_batch}. message_schema() -> #{ @@ -110,14 +110,13 @@ publish(post, Request) -> {ok, Body, _} = cowboy_req:read_body(Request), Message = message(emqx_json:decode(Body, [return_maps])), _ = emqx_mgmt:publish(Message), - {200, emqx_json:encode(format_message(Message))}. + {200, format_message(Message)}. publish_batch(post, Request) -> {ok, Body, _} = cowboy_req:read_body(Request), Messages = messages(emqx_json:decode(Body, [return_maps])), _ = [emqx_mgmt:publish(Message) || Message <- Messages], - ResponseBody = emqx_json:encode(format_message(Messages)), - {200, ResponseBody}. + {200, format_message(Messages)}. message(Map) -> From = maps:get(<<"from">>, Map, http_api), diff --git a/apps/emqx_management/src/emqx_mgmt_api_routes.erl b/apps/emqx_management/src/emqx_mgmt_api_routes.erl index 73c78b199..258680546 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_routes.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_routes.erl @@ -19,7 +19,7 @@ -include_lib("emqx/include/emqx.hrl"). %% API --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -48,7 +48,7 @@ route_schema() -> routes_api() -> Metadata = #{ get => #{ - description => "EMQ X routes", + description => <<"EMQ X routes">>, parameters => [ #{ name => page, @@ -64,13 +64,13 @@ routes_api() -> }], responses => #{ <<"200">> => - emqx_mgmt_util:response_array_schema("List route info", <<"route">>)}}}, + emqx_mgmt_util:response_array_schema("List route info", route)}}}, {"/routes", Metadata, routes}. route_api() -> Metadata = #{ get => #{ - description => "EMQ X routes", + description => <<"EMQ X routes">>, parameters => [#{ name => topic, in => path, @@ -80,7 +80,7 @@ route_api() -> }], responses => #{ <<"200">> => - emqx_mgmt_util:response_schema(<<"Route info">>, <<"route">>), + emqx_mgmt_util:response_schema(<<"Route info">>, route), <<"404">> => emqx_mgmt_util:response_error_schema(<<"Topic not found">>, [?TOPIC_NOT_FOUND]) }}}, @@ -99,20 +99,15 @@ route(get, Request) -> %%%============================================================================================== %% api apply list(Params) -> - Data = emqx_mgmt_api:paginate(emqx_route, Params, fun format/1), - Response = emqx_json:encode(Data), + Response = emqx_mgmt_api:paginate(emqx_route, Params, fun format/1), {200, Response}. lookup(#{topic := Topic}) -> case emqx_mgmt:lookup_routes(Topic) of [] -> - NotFound = #{code => ?TOPIC_NOT_FOUND, reason => <<"Topic not found">>}, - Response = emqx_json:encode(NotFound), - {404, Response}; + {404, #{code => ?TOPIC_NOT_FOUND, message => <<"Topic not found">>}}; [Route] -> - Data = format(Route), - Response = emqx_json:encode(Data), - {200, Response} + {200, format(Route)} end. %%%============================================================================================== diff --git a/apps/emqx_management/src/emqx_mgmt_api_stats.erl b/apps/emqx_management/src/emqx_mgmt_api_stats.erl index f06968f60..fa93fabfe 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_stats.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_stats.erl @@ -15,7 +15,7 @@ %%-------------------------------------------------------------------- -module(emqx_mgmt_api_stats). --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). @@ -96,13 +96,12 @@ stats_schema() -> stats_api() -> Metadata = #{ get => #{ - description => "EMQ X stats", + description => <<"EMQ X stats">>, responses => #{ - <<"200">> => emqx_mgmt_util:response_schema(<<"List stats ok">>, <<"stats">>)}}}, + <<"200">> => emqx_mgmt_util:response_schema(<<"List stats ok">>, stats)}}}, {"/stats", Metadata, list}. %%%============================================================================================== %% api apply list(get, _Request) -> - Response = emqx_json:encode(emqx_mgmt:get_stats()), - {200, Response}. + {200, emqx_mgmt:get_stats()}. diff --git a/apps/emqx_management/src/emqx_mgmt_api_status.erl b/apps/emqx_management/src/emqx_mgmt_api_status.erl index f7f013f20..fa46b1d25 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_status.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_status.erl @@ -15,7 +15,7 @@ %%-------------------------------------------------------------------- -module(emqx_mgmt_api_status). %% API --behavior(minirest_api). +-behaviour(minirest_api). -export([api_spec/0]). diff --git a/apps/emqx_management/src/emqx_mgmt_api_subscriptions.erl b/apps/emqx_management/src/emqx_mgmt_api_subscriptions.erl index ef1382caa..059327f4c 100644 --- a/apps/emqx_management/src/emqx_mgmt_api_subscriptions.erl +++ b/apps/emqx_management/src/emqx_mgmt_api_subscriptions.erl @@ -16,7 +16,7 @@ -module(emqx_mgmt_api_subscriptions). --behavior(minirest_api). +-behaviour(minirest_api). -include_lib("emqx/include/emqx.hrl"). @@ -47,7 +47,7 @@ api_spec() -> subscriptions_api() -> MetaData = #{ get => #{ - description => "List subscriptions", + description => <<"List subscriptions">>, parameters => [ #{ name => page, @@ -93,7 +93,7 @@ subscriptions_api() -> } ], responses => #{ - <<"200">> => emqx_mgmt_util:response_page_schema(<<"subscription">>)}}}, + <<"200">> => emqx_mgmt_util:response_page_schema(subscription)}}}, {"/subscriptions", MetaData, subscriptions}. subscription_schema() -> diff --git a/apps/emqx_management/src/emqx_mgmt_util.erl b/apps/emqx_management/src/emqx_mgmt_util.erl index 0220065c9..7d5f85cf2 100644 --- a/apps/emqx_management/src/emqx_mgmt_util.erl +++ b/apps/emqx_management/src/emqx_mgmt_util.erl @@ -93,16 +93,22 @@ urldecode(S) -> request_body_array_schema(Schema) when is_map(Schema) -> json_content_schema("", #{type => array, items => Schema}); +request_body_array_schema(Ref) when is_atom(Ref) -> + request_body_array_schema(atom_to_binary(Ref, utf8)); request_body_array_schema(Ref) when is_binary(Ref) -> json_content_schema("", #{type => array, items => minirest:ref(Ref)}). request_body_schema(Schema) when is_map(Schema) -> json_content_schema("", Schema); +request_body_schema(Ref) when is_atom(Ref) -> + request_body_schema(atom_to_binary(Ref)); request_body_schema(Ref) when is_binary(Ref) -> json_content_schema("", minirest:ref(Ref)). response_array_schema(Description, Schema) when is_map(Schema) -> json_content_schema(Description, #{type => array, items => Schema}); +response_array_schema(Description, Ref) when is_atom(Ref) -> + response_array_schema(Description, atom_to_binary(Ref, utf8)); response_array_schema(Description, Ref) when is_binary(Ref) -> json_content_schema(Description, #{type => array, items => minirest:ref(Ref)}). @@ -111,6 +117,8 @@ response_schema(Description) -> response_schema(Description, Schema) when is_map(Schema) -> json_content_schema(Description, Schema); +response_schema(Description, Ref) when is_atom(Ref) -> + response_schema(Description, atom_to_binary(Ref, utf8)); response_schema(Description, Ref) when is_binary(Ref) -> json_content_schema(Description, minirest:ref(Ref)). @@ -125,10 +133,12 @@ response_error_schema(Description, Enum) -> code => #{ type => string, enum => Enum}, - reason => #{ + message => #{ type => string}}}, json_content_schema(Description, Schema). +response_page_schema(Def) when is_atom(Def) -> + response_page_schema(atom_to_binary(Def, utf8)); response_page_schema(Def) when is_binary(Def) -> Schema = #{ type => object, @@ -147,6 +157,8 @@ response_page_schema(Def) when is_binary(Def) -> items => minirest:ref(Def)}}}, json_content_schema("", Schema). +response_batch_schema(DefName) when is_atom(DefName) -> + response_batch_schema(atom_to_binary(DefName, utf8)); response_batch_schema(DefName) when is_binary(DefName) -> Schema = #{ type => object, diff --git a/apps/emqx_management/test/emqx_mgmt_clients_api_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_clients_api_SUITE.erl index ac63db0fb..4bcc3ff7d 100644 --- a/apps/emqx_management/test/emqx_mgmt_clients_api_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_clients_api_SUITE.erl @@ -83,6 +83,7 @@ t_clients(_) -> %% delete /clients/:clientid kickout Client2Path = emqx_mgmt_api_test_util:api_path(["clients", binary_to_list(ClientId2)]), {ok, _} = emqx_mgmt_api_test_util:request_api(delete, Client2Path), + timer:sleep(300), AfterKickoutResponse = emqx_mgmt_api_test_util:request_api(get, Client2Path), ?assertEqual({error, {"HTTP/1.1", 404, "Not Found"}}, AfterKickoutResponse), diff --git a/apps/emqx_management/test/emqx_mgmt_publish_api_SUITE.erl b/apps/emqx_management/test/emqx_mgmt_publish_api_SUITE.erl index 9ecb1a11b..2f566e5f4 100644 --- a/apps/emqx_management/test/emqx_mgmt_publish_api_SUITE.erl +++ b/apps/emqx_management/test/emqx_mgmt_publish_api_SUITE.erl @@ -60,13 +60,13 @@ t_publish_api(_) -> ?assertEqual(receive_assert(?TOPIC1, 0, Payload), ok), emqtt:disconnect(Client). -t_publish_batch_api(_) -> +t_publish_bulk_api(_) -> {ok, Client} = emqtt:start_link(#{username => <<"api_username">>, clientid => <<"api_clientid">>}), {ok, _} = emqtt:connect(Client), {ok, _, [0]} = emqtt:subscribe(Client, ?TOPIC1), {ok, _, [0]} = emqtt:subscribe(Client, ?TOPIC2), Payload = <<"hello">>, - Path = emqx_mgmt_api_test_util:api_path(["publish_batch"]), + Path = emqx_mgmt_api_test_util:api_path(["publish", "bulk"]), Auth = emqx_mgmt_api_test_util:auth_header_(), Body =[#{topic => ?TOPIC1, payload => Payload}, #{topic => ?TOPIC2, payload => Payload}], {ok, Response} = emqx_mgmt_api_test_util:request_api(post, Path, "", Auth, Body), diff --git a/rebar.config b/rebar.config index 4be003e42..66d8b8f27 100644 --- a/rebar.config +++ b/rebar.config @@ -50,7 +50,7 @@ , {esockd, {git, "https://github.com/emqx/esockd", {tag, "5.8.2"}}} , {ekka, {git, "https://github.com/emqx/ekka", {tag, "0.10.4"}}} , {gen_rpc, {git, "https://github.com/emqx/gen_rpc", {tag, "2.5.1"}}} - , {minirest, {git, "https://github.com/emqx/minirest", {tag, "1.1.2"}}} + , {minirest, {git, "https://github.com/emqx/minirest", {tag, "1.1.4"}}} , {ecpool, {git, "https://github.com/emqx/ecpool", {tag, "0.5.1"}}} , {replayq, {git, "https://github.com/emqx/replayq", {tag, "0.3.2"}}} , {pbkdf2, {git, "https://github.com/emqx/erlang-pbkdf2.git", {tag, "2.0.4"}}}