refactor: fields `meta` ref to dashboard_swagger

This commit is contained in:
JimMoen 2022-04-08 16:39:35 +08:00
parent fb7c7dffb7
commit 93dd7d46c5
7 changed files with 21 additions and 41 deletions

View File

@ -24,7 +24,7 @@
-include_lib("emqx/include/logger.hrl").
-include_lib("emqx/include/emqx_authentication.hrl").
-import(hoconsc, [mk/2, ref/1]).
-import(hoconsc, [mk/2, ref/1, ref/2]).
-import(emqx_dashboard_swagger, [error_codes/2]).
-define(BAD_REQUEST, 'BAD_REQUEST').
@ -146,13 +146,7 @@ fields(response_user) ->
{is_superuser, mk(boolean(), #{default => false, required => false})}
];
fields(response_users) ->
paginated_list_type(ref(response_user));
fields(pagination_meta) ->
[
{page, pos_integer()},
{limit, pos_integer()},
{count, non_neg_integer()}
].
paginated_list_type(ref(response_user)).
schema("/authentication") ->
#{
@ -1316,7 +1310,7 @@ binfmt(Fmt, Args) -> iolist_to_binary(io_lib:format(Fmt, Args)).
paginated_list_type(Type) ->
[
{data, hoconsc:array(Type)},
{meta, ref(pagination_meta)}
{meta, ref(emqx_dashboard_swagger, meta)}
].
authenticator_array_example() ->

View File

@ -383,7 +383,7 @@ fields(rules_for_username) ->
fields(username_response_data) ->
[
{data, mk(array(ref(rules_for_username)), #{})},
{meta, ref(meta)}
{meta, ref(emqx_dashboard_swagger, meta)}
];
fields(rules_for_clientid) ->
fields(rules) ++
@ -391,14 +391,10 @@ fields(rules_for_clientid) ->
fields(clientid_response_data) ->
[
{data, mk(array(ref(rules_for_clientid)), #{})},
{meta, ref(meta)}
{meta, ref(emqx_dashboard_swagger, meta)}
];
fields(rules) ->
[{rules, mk(array(ref(rule_item)))}];
fields(meta) ->
emqx_dashboard_swagger:fields(page) ++
emqx_dashboard_swagger:fields(limit) ++
[{count, mk(integer(), #{example => 1})}].
[{rules, mk(array(ref(rule_item)))}].
%%--------------------------------------------------------------------
%% HTTP API

View File

@ -135,7 +135,12 @@ fields(limit) ->
<<")">>
]),
Meta = #{in => query, desc => Desc, default => ?DEFAULT_ROW, example => 50},
[{limit, hoconsc:mk(range(1, ?MAX_ROW_LIMIT), Meta)}].
[{limit, hoconsc:mk(range(1, ?MAX_ROW_LIMIT), Meta)}];
fields(count) ->
Meta = #{desc => <<"Results count.">>, required => true},
[{count, hoconsc:mk(range(0, inf), Meta)}];
fields(meta) ->
fields(page) ++ fields(limit) ++ fields(count).
-spec schema_with_example(hocon_schema:type(), term()) -> hocon_schema:field_schema_map().
schema_with_example(Type, Example) ->

View File

@ -53,7 +53,7 @@ schema("/alarms") ->
responses => #{
200 => [
{data, hoconsc:mk(hoconsc:array(hoconsc:ref(?MODULE, alarm)), #{})},
{meta, hoconsc:mk(hoconsc:ref(?MODULE, meta), #{})}
{meta, hoconsc:mk(hoconsc:ref(emqx_dashboard_swagger, meta), #{})}
]
}
},
@ -98,11 +98,8 @@ fields(alarm) ->
desc => ?DESC(deactivate_at),
example => <<"2021-10-31T10:52:52.548+08:00">>
})}
];
fields(meta) ->
emqx_dashboard_swagger:fields(page) ++
emqx_dashboard_swagger:fields(limit) ++
[{count, hoconsc:mk(integer(), #{example => 1})}].
].
%%%==============================================================================================
%% parameters trans
alarms(get, #{query_string := QString}) ->

View File

@ -62,7 +62,7 @@ schema("/banned") ->
responses => #{
200 => [
{data, hoconsc:mk(hoconsc:array(hoconsc:ref(ban)), #{})},
{meta, hoconsc:mk(hoconsc:ref(meta), #{})}
{meta, hoconsc:mk(hoconsc:ref(emqx_dashboard_swagger, meta), #{})}
]
}
},
@ -147,11 +147,7 @@ fields(ban) ->
required => false,
example => <<"2021-10-25T21:53:47+08:00">>
})}
];
fields(meta) ->
emqx_dashboard_swagger:fields(page) ++
emqx_dashboard_swagger:fields(limit) ++
[{count, hoconsc:mk(integer(), #{example => 1})}].
].
banned(get, #{query_string := Params}) ->
Response = emqx_mgmt_api:paginate(?TAB, Params, ?FORMAT_FUN),

View File

@ -203,7 +203,7 @@ schema("/clients") ->
responses => #{
200 => [
{data, hoconsc:mk(hoconsc:array(hoconsc:ref(?MODULE, client)), #{})},
{meta, hoconsc:mk(hoconsc:ref(?MODULE, meta), #{})}
{meta, hoconsc:mk(hoconsc:ref(emqx_dashboard_swagger, meta), #{})}
],
400 =>
emqx_dashboard_swagger:error_codes(
@ -518,11 +518,7 @@ fields(subscribe) ->
fields(unsubscribe) ->
[
{topic, hoconsc:mk(binary(), #{desc => <<"Topic">>})}
];
fields(meta) ->
emqx_dashboard_swagger:fields(page) ++
emqx_dashboard_swagger:fields(limit) ++
[{count, hoconsc:mk(integer(), #{example => 1})}].
].
%%%==============================================================================================
%% parameters trans

View File

@ -60,7 +60,7 @@ schema("/topics") ->
responses => #{
200 => [
{data, hoconsc:mk(hoconsc:array(hoconsc:ref(topic)), #{})},
{meta, hoconsc:mk(hoconsc:ref(meta), #{})}
{meta, hoconsc:mk(hoconsc:ref(emqx_dashboard_swagger, meta), #{})}
]
}
}
@ -91,11 +91,7 @@ fields(topic) ->
desc => <<"Node">>,
required => true
})}
];
fields(meta) ->
emqx_dashboard_swagger:fields(page) ++
emqx_dashboard_swagger:fields(limit) ++
[{count, hoconsc:mk(integer(), #{example => 1})}].
].
%%%==============================================================================================
%% parameters trans