-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[erlang-client] Erlang request utils (#7257)
* [erlang-client] fix body param from being included path and base path to remove host * [erlang-client] move request logic out of api functions to utils module * [erlang-client] add support for passing http client configuration to requests * [erlang-client] update auth handling * [erlang-client] remove underscore2, replacing with original underscore + replaceAll
- Loading branch information
1 parent
01a076c
commit c731185
Showing
22 changed files
with
701 additions
and
570 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 4 additions & 3 deletions
7
modules/swagger-codegen/src/main/resources/erlang-client/app.src.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
modules/swagger-codegen/src/main/resources/erlang-client/rebar.config.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{erl_opts, [debug_info, warnings_as_errors, warn_untyped_record]}. | ||
|
||
{deps, [jsx, hackney]}. | ||
{deps, [ctx, jsx, hackney]}. | ||
|
||
{shell, [{apps, [{{packageName}}]}]}. |
94 changes: 94 additions & 0 deletions
94
modules/swagger-codegen/src/main/resources/erlang-client/utils.mustache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
-module({{packageName}}_utils). | ||
|
||
-export([request/8, | ||
select_header_content_type/1, | ||
optional_params/2]). | ||
|
||
-type response_info() :: #{status := integer(), | ||
headers := list()}. | ||
-export_type([response_info/0]). | ||
|
||
request(_Ctx, Method, Path, QS, Headers, Body, Opts, Cfg) -> | ||
{Headers1, QS1} = update_params_with_auth(Cfg, Headers, QS), | ||
Host = maps:get(host, Cfg, "localhost:8001"), | ||
Url = hackney_url:make_url(Host, Path, QS1), | ||
|
||
ConfigHackneyOpts = maps:get(hackney_opts, Cfg, []), | ||
Body1 = case lists:keyfind(<<"Content-Type">>, 1, Headers1) of | ||
{_, <<"application/json", _/binary>>} -> | ||
jsx:encode(Body); | ||
_ -> | ||
Body | ||
end, | ||
|
||
case hackney:request(Method, Url, Headers1, Body1, Opts++ConfigHackneyOpts) of | ||
{ok, ClientRef} -> | ||
%% return value if Opts includes `async` | ||
{ok, ClientRef}; | ||
{ok, Status, RespHeaders, ClientRef} when Status >= 200, | ||
Status =< 299 -> | ||
{ok, ResponseBody} = hackney:body(ClientRef), | ||
Resp = decode_response(RespHeaders, ResponseBody), | ||
{ok, Resp, #{status => Status, | ||
headers => RespHeaders}}; | ||
{ok, Status, RespHeaders, ClientRef} when Status >= 300 -> | ||
{ok, ResponseBody} = hackney:body(ClientRef), | ||
Resp = decode_response(RespHeaders, ResponseBody), | ||
{error, Resp, #{status => Status, | ||
headers => RespHeaders}} | ||
end. | ||
|
||
decode_response(Headers, Body) -> | ||
case lists:keyfind(<<"Content-Type">>, 1, Headers) of | ||
{_, <<"application/json", _/binary>>} -> | ||
jsx:decode(Body, [return_maps, {labels, atom}]); | ||
%% TODO: yml, protobuf, user defined function | ||
_ -> | ||
Body | ||
end. | ||
|
||
optional_params([], _Params) -> []; | ||
optional_params(Keys, Params) -> | ||
[{Key, maps:get(Key, Params)} || Key <- Keys, maps:is_key(Key, Params)]. | ||
|
||
select_header_content_type([]) -> | ||
[]; | ||
select_header_content_type(ContentTypes) -> | ||
case lists:member(<<"application/json">>, ContentTypes) orelse lists:member(<<"*/*">>, ContentTypes) of | ||
true -> | ||
[{<<"Content-Type">>, <<"application/json">>}]; | ||
false -> | ||
[{<<"Content-Type">>, hd(ContentTypes)}] | ||
end. | ||
|
||
auth_with_prefix(Cfg, Key, Token) -> | ||
Prefixes = maps:get(api_key_prefix, Cfg, #{}), | ||
case maps:get(Key, Prefixes, undefined) of | ||
undefined -> | ||
Token; | ||
Prefix -> | ||
<<Prefix/binary, " ", Token/binary>> | ||
end. | ||
|
||
update_params_with_auth(Cfg, Headers, QS) -> | ||
AuthSettings = maps:get(auth, Cfg, #{}), | ||
Auths = #{ {{#authMethods}}'{{name}}' => | ||
#{type => '{{type}}', | ||
key => <<"{{keyParamName}}">>, | ||
in => {{#isKeyInHeader}}header{{/isKeyInHeader}}{{#isKeyInQuery}}query{{/isKeyInQuery}}}{{#hasMore}}, {{/hasMore}}{{/authMethods}}}, | ||
|
||
maps:fold(fun(AuthName, #{type := _Type, | ||
in := In, | ||
key := Key}, {HeadersAcc, QSAcc}) -> | ||
case maps:get(AuthName, AuthSettings, undefined) of | ||
undefined -> | ||
{HeadersAcc, QSAcc}; | ||
Value -> | ||
case In of | ||
header -> | ||
{[{Key, auth_with_prefix(Cfg, Key, Value)} | HeadersAcc], QSAcc}; | ||
query -> | ||
{HeadersAcc, [{Key, auth_with_prefix(Cfg, Key, Value)} | QSAcc]} | ||
end | ||
end | ||
end, {Headers, QS}, Auths). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
2.3.0-SNAPSHOT | ||
2.3.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
{erl_opts, [debug_info, warnings_as_errors, warn_untyped_record]}. | ||
|
||
{deps, [jsx, hackney]}. | ||
{deps, [ctx, jsx, hackney]}. | ||
|
||
{shell, [{apps, [petstore]}]}. |
9 changes: 5 additions & 4 deletions
9
...etstore/erlang-client/src/swagger.app.src → ...tstore/erlang-client/src/petstore.app.src
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...rlang-client/src/swagger_api_response.erl → ...lang-client/src/petstore_api_response.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
...re/erlang-client/src/swagger_category.erl → ...e/erlang-client/src/petstore_category.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...store/erlang-client/src/swagger_order.erl → ...tore/erlang-client/src/petstore_order.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 4 additions & 4 deletions
8
...etstore/erlang-client/src/swagger_pet.erl → ...tstore/erlang-client/src/petstore_pet.erl
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.