Skip to content

Instantly share code, notes, and snippets.

@zhongwencool
Created September 1, 2021 05:59
Show Gist options
  • Save zhongwencool/9a9db31f600bf5721290896cce4bef90 to your computer and use it in GitHub Desktop.
Save zhongwencool/9a9db31f600bf5721290896cce4bef90 to your computer and use it in GitHub Desktop.
-module(emqx_api_spec).
%% API
-include_lib("typerefl/include/types.hrl").
%% -import(emqx_schema, [t/1, t/3, t/4, ref/1]).
-export([api_spec/0
, spec/1]).
api_spec() -> ["/plain/style", "/schema/style"].
spec("/plain/style") ->
#{post => #{
tags => [dashboard],
description => <<"Dashboard Auth">>,
summary => <<"Dashboard Auth Summary">>,
requestBody => #{
<<"username">> => prop(string(), <<"username desc">>, <<"admin">>),
<<"password">> => prop(string(), <<"password desc">>, <<"public">>)
},
responses => #{
200 => #{
description => <<"Dashboard Auth successfully">>,
content => #{
<<"token">> => prop(string(), <<"JWT token">>, <<"token">>),
<<"version">> => prop(string(), <<"EMQX verison">>, <<"5.0.0">>),
<<"license">> => prop(#{<<"edition">> => prop(union(community, enterprise), <<"Edition Desc">>, community)},
<<"License Desc">>)
}},
401 =>
#{description => <<"Dashboard Auth failed">>,
content => #{
<<"code">> => prop(union('PASSWORD_ERROR', 'USERNAME_ERROR'), <<"password or username error">>, 'PASSWORD_ERROR'),
<<"message">> => prop(string(), <<"specific messages">>, <<"Password not match">>)}
}},
security => []
}};
spec("/schema/style/:id") ->
#{get => #{
description => "List authorization rules",
parameters => #{
path => [param(id, range(1, 100), #{description => "rule id", default => 20, example => 200})],
query => [param(username, schema(<<"username">>), #{required => false})],
header => [param('X-Request-ID', schema(<<"x-reqeust-id">>), #{required => false})]
},
responses => #{
200 => resp(list(schema(<<"rule">>)), #{description => "ok"}),
400 => resp(schema(<<"rule_400">>))}
},
put => #{
description => "update rule",
parameters => param(id, range(1, 100), #{description => "rule id", default => 20, example => 200}),
requestBody => req_body(schema(<<"rule">>), "rules"),
responses => #{
204 => <<"No Content">>,
404 => resp(schema("rule_404"), <<"Not Found">>),
400 => resp(schema("rule_400"), <<"Bad Request">>)
}
}
};
spec("/listeners") ->
#{get => #{
description => "list all listerners",
responses => #{
200 => resp(schema(<<"listeners">>), <<"List listeners successfully">>)
}
}};
spec(_) -> todo.
schema(<<"username">>) ->
#{type => string(), format => uuid, example => <<"CBF85834-930-11EC-9A31-5FBDE6068D72">>};
schema(<<"id">>) ->
#{type => range(1, 100), example => 50};
schema(<<"listeners">>) ->
emqx_schema:fields("listeners");
schema(<<"rule">>) ->
todo;
schema(_) ->
todo.
prop(Schema, Desc) ->
#{schema => Schema, description => Desc}.
prop(Schema, Desc, Default) ->
#{schema => Schema, description => Desc, default => Default}.
param(Name, Schema, Options) ->
Options#{name => Name, schema => Schema}.
req_body(Schema, Desc) ->
request_body(Schema, Desc, json).
%% todo json xml text/plain
request_body(Schema, Desc, Type) ->
#{description => Desc, content => #{schema => Schema, type => Type}}.
resp(Schema) -> resp(Schema, #{}).
resp(Schema, Options) when is_map(Options) -> Options#{schema => Schema};
resp(Schema, Desc) -> resp(Schema, #{description => Desc}).
@zmstone
Copy link

zmstone commented Sep 1, 2021

from_hocon_spec(SchemaModule, StructName).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment