fix(api): auto subscribe api hocon support

This commit is contained in:
DDDHuang 2022-02-10 15:40:12 +08:00
parent 50859bbd83
commit 5d17bd8441
3 changed files with 27 additions and 41 deletions

View File

@ -18,7 +18,10 @@
-behaviour(minirest_api). -behaviour(minirest_api).
-export([api_spec/0]). -export([ api_spec/0
, paths/0
, schema/1
]).
-export([auto_subscribe/2]). -export([auto_subscribe/2]).
@ -29,54 +32,30 @@
-include_lib("emqx/include/emqx_placeholder.hrl"). -include_lib("emqx/include/emqx_placeholder.hrl").
api_spec() -> api_spec() ->
{[auto_subscribe_api()], []}. emqx_dashboard_swagger:spec(?MODULE).
schema() -> paths() ->
case emqx_mgmt_api_configs:gen_schema(emqx:get_config([auto_subscribe, topics])) of ["/mqtt/auto_subscribe"].
#{example := <<>>, type := string} ->
emqx_mgmt_util:schema(auto_subscribe_conf_example());
Example ->
emqx_mgmt_util:schema(Example)
end.
auto_subscribe_conf_example() -> schema("/mqtt/auto_subscribe") ->
#{ #{
type => array, 'operationId' => auto_subscribe,
items => #{
type => object,
properties =>#{
topic => #{
type => string,
example => <<
"/clientid/", ?PH_S_CLIENTID,
"/username/", ?PH_S_USERNAME,
"/host/", ?PH_S_HOST,
"/port/", ?PH_S_PORT>>},
qos => #{example => 0, type => number, enum => [0, 1, 2]},
rh => #{example => 0, type => number, enum => [0, 1, 2]},
nl => #{example => 0, type => number, enum => [0, 1]},
rap => #{example => 0, type => number, enum => [0, 1]}
}
}
}.
auto_subscribe_api() ->
Metadata = #{
get => #{ get => #{
description => <<"Auto subscribe list">>, description => <<"Auto subscribe list">>,
responses => #{ responses => #{
<<"200">> => schema()}}, 200 => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe")
}
},
put => #{ put => #{
description => <<"Update auto subscribe topic list">>, description => <<"Update auto subscribe topic list">>,
'requestBody' => schema(), 'requestBody' => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe"),
responses => #{ responses => #{
<<"200">> => schema(), 200 => hoconsc:ref(emqx_auto_subscribe_schema, "auto_subscribe"),
<<"400">> => emqx_mgmt_util:error_schema( 400 => emqx_mgmt_util:error_schema(
<<"Request body required">>, [?BAD_REQUEST]), <<"Request body required">>, [?BAD_REQUEST]),
<<"409">> => emqx_mgmt_util:error_schema( 409 => emqx_mgmt_util:error_schema(
<<"Auto Subscribe topics max limit">>, [?EXCEED_LIMIT])}} <<"Auto Subscribe topics max limit">>, [?EXCEED_LIMIT])}}
}, }.
{"/mqtt/auto_subscribe", Metadata, auto_subscribe}.
%%%============================================================================================== %%%==============================================================================================
%% api apply %% api apply

View File

@ -18,6 +18,7 @@
-behaviour(hocon_schema). -behaviour(hocon_schema).
-include_lib("typerefl/include/types.hrl"). -include_lib("typerefl/include/types.hrl").
-include_lib("emqx/include/emqx_placeholder.hrl").
-export([ namespace/0 -export([ namespace/0
, roots/0 , roots/0
@ -33,7 +34,7 @@ fields("auto_subscribe") ->
]; ];
fields("topic") -> fields("topic") ->
[ {topic, sc(binary(), #{})} [ {topic, sc(binary(), #{example => topic_example()})}
, {qos, sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1), typerefl:integer(2)]), , {qos, sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1), typerefl:integer(2)]),
#{default => 0})} #{default => 0})}
, {rh, sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1), typerefl:integer(2)]), , {rh, sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1), typerefl:integer(2)]),
@ -42,6 +43,12 @@ fields("topic") ->
, {nl, sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1)]), #{default => 0})} , {nl, sc(hoconsc:union([typerefl:integer(0), typerefl:integer(1)]), #{default => 0})}
]. ].
topic_example() ->
<<"/clientid/", ?PH_S_CLIENTID,
"/username/", ?PH_S_USERNAME,
"/host/", ?PH_S_HOST,
"/port/", ?PH_S_PORT>>.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Internal functions %% Internal functions
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------

View File

@ -369,13 +369,13 @@ fields(keepalive) ->
fields(subscribe) -> fields(subscribe) ->
[ [
{topic, hoconsc:mk(binary(), #{desc => <<"Access type">>})}, {topic, hoconsc:mk(binary(), #{desc => <<"Topic">>})},
{qos, hoconsc:mk(emqx_schema:qos(), #{desc => <<"QoS">>})} {qos, hoconsc:mk(emqx_schema:qos(), #{desc => <<"QoS">>})}
]; ];
fields(unsubscribe) -> fields(unsubscribe) ->
[ [
{topic, hoconsc:mk(binary(), #{desc => <<"Access type">>})} {topic, hoconsc:mk(binary(), #{desc => <<"Topic">>})}
]; ];
fields(meta) -> fields(meta) ->