Merge pull request #7538 from DDDHuang/api_hocon_last2

fix: publish api support hocon & params check
This commit is contained in:
DDDHuang 2022-04-08 09:19:53 +08:00 committed by GitHub
commit 1b63f8fcef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 32 deletions

View File

@ -14,53 +14,80 @@
%% limitations under the License. %% limitations under the License.
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
-module(emqx_mgmt_api_publish). -module(emqx_mgmt_api_publish).
%% API
-include_lib("emqx/include/emqx.hrl"). -include_lib("emqx/include/emqx.hrl").
-include_lib("typerefl/include/types.hrl").
-behaviour(minirest_api). -behaviour(minirest_api).
-import(emqx_mgmt_util, [ object_schema/1 -export([ api_spec/0
, object_schema/2 , paths/0
, object_array_schema/1 , schema/1
, object_array_schema/2 , fields/1
, properties/1
]). ]).
-export([api_spec/0]).
-export([ publish/2 -export([ publish/2
, publish_batch/2]). , publish_batch/2]).
api_spec() -> api_spec() ->
{[publish_api(), publish_bulk_api()], []}. emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}).
publish_api() -> paths() ->
MeteData = #{ ["/publish", "/publish/bulk"].
schema("/publish") ->
#{
'operationId' => publish,
post => #{ post => #{
description => <<"Publish">>, description => <<"Publish Message">>,
'requestBody' => object_schema(maps:without([id], properties())), 'requestBody' => hoconsc:mk(hoconsc:ref(?MODULE, publish_message)),
responses => #{ responses => #{
<<"200">> => object_schema(properties(), <<"publish ok">>)}}}, 200 => hoconsc:mk(hoconsc:ref(?MODULE, publish_message_info))
{"/publish", MeteData, publish}. }
}
};
publish_bulk_api() -> schema("/publish/bulk") ->
MeteData = #{ #{
'operationId' => publish_batch,
post => #{ post => #{
description => <<"publish">>, description => <<"Publish Messages">>,
'requestBody' => object_array_schema(maps:without([id], properties())), 'requestBody' => hoconsc:mk(hoconsc:array(hoconsc:ref(?MODULE, publish_message)), #{}),
responses => #{ responses => #{
<<"200">> => object_array_schema(properties(), <<"publish ok">>)}}}, 200 => hoconsc:mk(hoconsc:array(hoconsc:ref(?MODULE, publish_message_info)), #{})
{"/publish/bulk", MeteData, publish_batch}. }
}
}.
properties() -> fields(publish_message) ->
properties([ [
{id, string, <<"Message Id">>}, {topic, hoconsc:mk(binary(), #{
{topic, string, <<"Topic">>}, desc => <<"Topic Name">>,
{qos, integer, <<"QoS">>, [0, 1, 2]}, required => true,
{payload, string, <<"Topic">>}, example => <<"api/example/topic">>})},
{from, string, <<"Message from">>}, {qos, hoconsc:mk(emqx_schema:qos(), #{
{retain, boolean, <<"Retain message flag, nullable, default false">>} desc => <<"MQTT QoS">>,
]). required => false,
default => 0})},
{from, hoconsc:mk(binary(), #{
desc => <<"From client ID">>,
required => false,
example => <<"api_example_client">>})},
{payload, hoconsc:mk(binary(), #{
desc => <<"MQTT Payload">>,
required => true,
example => <<"hello emqx api">>})},
{retain, hoconsc:mk(boolean(), #{
desc => <<"MQTT Retain Message">>,
required => false,
default => false})}
];
fields(publish_message_info) ->
[
{id, hoconsc:mk(binary(), #{
desc => <<"Internal Message ID">>})}
] ++ fields(publish_message).
publish(post, #{body := Body}) -> publish(post, #{body := Body}) ->
Message = message(Body), Message = message(Body),