emqx_dashboard_error_code_api.erl 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. %%--------------------------------------------------------------------
  2. %% Copyright (c) 2020-2022 EMQ Technologies Co., Ltd. All Rights Reserved.
  3. %%
  4. %% Licensed under the Apache License, Version 2.0 (the "License");
  5. %% you may not use this file except in compliance with the License.
  6. %% You may obtain a copy of the License at
  7. %%
  8. %% http://www.apache.org/licenses/LICENSE-2.0
  9. %%
  10. %% Unless required by applicable law or agreed to in writing, software
  11. %% distributed under the License is distributed on an "AS IS" BASIS,
  12. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. %% See the License for the specific language governing permissions and
  14. %% limitations under the License.
  15. %%--------------------------------------------------------------------
  16. -module(emqx_dashboard_error_code_api).
  17. -behaviour(minirest_api).
  18. -include_lib("emqx/include/http_api.hrl").
  19. -include("emqx_dashboard.hrl").
  20. -include_lib("typerefl/include/types.hrl").
  21. -export([
  22. api_spec/0,
  23. fields/1,
  24. paths/0,
  25. schema/1,
  26. namespace/0
  27. ]).
  28. -export([
  29. error_codes/2,
  30. error_code/2
  31. ]).
  32. namespace() -> "dashboard".
  33. api_spec() ->
  34. emqx_dashboard_swagger:spec(?MODULE, #{check_schema => true, translate_body => true}).
  35. paths() ->
  36. [
  37. "/error_codes",
  38. "/error_codes/:code"
  39. ].
  40. schema("/error_codes") ->
  41. #{
  42. 'operationId' => error_codes,
  43. get => #{
  44. security => [],
  45. description => <<"API Error Codes">>,
  46. tags => [<<"Error Codes">>],
  47. responses => #{
  48. 200 => hoconsc:array(hoconsc:ref(?MODULE, error_code))
  49. }
  50. }
  51. };
  52. schema("/error_codes/:code") ->
  53. #{
  54. 'operationId' => error_code,
  55. get => #{
  56. security => [],
  57. description => <<"API Error Codes">>,
  58. tags => [<<"Error Codes">>],
  59. parameters => [
  60. {code,
  61. hoconsc:mk(hoconsc:enum(emqx_dashboard_error_code:all()), #{
  62. desc => <<"API Error Codes">>,
  63. in => path,
  64. example => hd(emqx_dashboard_error_code:all())
  65. })}
  66. ],
  67. responses => #{
  68. 200 => hoconsc:ref(?MODULE, error_code)
  69. }
  70. }
  71. }.
  72. fields(error_code) ->
  73. [
  74. {code, hoconsc:mk(string(), #{desc => <<"Code Name">>})},
  75. {description, hoconsc:mk(string(), #{desc => <<"Description">>})}
  76. ].
  77. error_codes(_, _) ->
  78. {200, emqx_dashboard_error_code:list()}.
  79. error_code(_, #{bindings := #{code := Name}}) ->
  80. case emqx_dashboard_error_code:look_up(Name) of
  81. {ok, Code} ->
  82. {200, Code};
  83. {error, not_found} ->
  84. Message = list_to_binary(io_lib:format("Code name ~p not found", [Name])),
  85. {404, ?NOT_FOUND, Message}
  86. end.