forked from loafle/openapi-generator-original
[erlang-client] fix URL paths (#14988)
* integers parameters in URL did not work as expected * so now, if the parameter is an integer, we convert it to binary before passing to `hackney_url:make_url/3`
This commit is contained in:
parent
849708dc0d
commit
fc91fca737
@ -21,14 +21,14 @@
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env({{packageName}}_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env({{packageName}}_api, config, #{})),
|
||||||
|
|
||||||
Method = {{httpMethod}},
|
Method = {{httpMethod}},
|
||||||
Path = ["{{{replacedPathName}}}"],
|
Path = [?BASE_URL, "{{{replacedPathName}}}"],
|
||||||
QS = {{#queryParams.isEmpty}}[]{{/queryParams.isEmpty}}{{^queryParams.isEmpty}}lists:flatten([{{#joinWithComma}}{{#queryParams}}{{#required}}{{#qsEncode}}{{this}}{{/qsEncode}} {{/required}}{{/queryParams}}{{/joinWithComma}}])++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#queryParams}}{{^required}} '{{baseName}}'{{/required}}{{/queryParams}}{{/joinWithComma}}], _OptionalParams){{/queryParams.isEmpty}},
|
QS = {{#queryParams.isEmpty}}[]{{/queryParams.isEmpty}}{{^queryParams.isEmpty}}lists:flatten([{{#joinWithComma}}{{#queryParams}}{{#required}}{{#qsEncode}}{{this}}{{/qsEncode}} {{/required}}{{/queryParams}}{{/joinWithComma}}])++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#queryParams}}{{^required}} '{{baseName}}'{{/required}}{{/queryParams}}{{/joinWithComma}}], _OptionalParams){{/queryParams.isEmpty}},
|
||||||
Headers = {{#headerParams.isEmpty}}[]{{/headerParams.isEmpty}}{{^headerParams.isEmpty}}[{{#headerParams}}{{#required}} {<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/headerParams}}]++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#headerParams}}{{^required}} '{{baseName}}'{{/required}}{{/headerParams}}{{/joinWithComma}}], _OptionalParams){{/headerParams.isEmpty}},
|
Headers = {{#headerParams.isEmpty}}[]{{/headerParams.isEmpty}}{{^headerParams.isEmpty}}[{{#headerParams}}{{#required}} {<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/headerParams}}]++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#headerParams}}{{^required}} '{{baseName}}'{{/required}}{{/headerParams}}{{/joinWithComma}}], _OptionalParams){{/headerParams.isEmpty}},
|
||||||
Body1 = {{^formParams.isEmpty}}{form, [{{#joinWithComma}}{{#formParams}}{{#required}} {<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/formParams}}{{/joinWithComma}}]++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#formParams}}{{^required}} '{{baseName}}'{{/required}}{{/formParams}}{{/joinWithComma}}], _OptionalParams)}{{/formParams.isEmpty}}{{#formParams.isEmpty}}{{#bodyParams.isEmpty}}[]{{/bodyParams.isEmpty}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/bodyParams.isEmpty}}{{/formParams.isEmpty}},
|
Body1 = {{^formParams.isEmpty}}{form, [{{#joinWithComma}}{{#formParams}}{{#required}} {<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/formParams}}{{/joinWithComma}}]++{{packageName}}_utils:optional_params([{{#joinWithComma}}{{#formParams}}{{^required}} '{{baseName}}'{{/required}}{{/formParams}}{{/joinWithComma}}], _OptionalParams)}{{/formParams.isEmpty}}{{#formParams.isEmpty}}{{#bodyParams.isEmpty}}[]{{/bodyParams.isEmpty}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/bodyParams.isEmpty}}{{/formParams.isEmpty}},
|
||||||
ContentTypeHeader = {{packageName}}_utils:select_header_content_type([{{#consumes}}{{^-first}}, {{/-first}}<<"{{mediaType}}">>{{/consumes}}]),
|
ContentTypeHeader = {{packageName}}_utils:select_header_content_type([{{#consumes}}{{^-first}}, {{/-first}}<<"{{mediaType}}">>{{/consumes}}]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
{{packageName}}_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
{{packageName}}_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
|
|
||||||
|
@ -11,7 +11,8 @@
|
|||||||
request(_Ctx, Method, Path, QS, Headers, Body, Opts, Cfg) ->
|
request(_Ctx, Method, Path, QS, Headers, Body, Opts, Cfg) ->
|
||||||
{Headers1, QS1} = update_params_with_auth(Cfg, Headers, QS),
|
{Headers1, QS1} = update_params_with_auth(Cfg, Headers, QS),
|
||||||
Host = maps:get(host, Cfg, "localhost:8001"),
|
Host = maps:get(host, Cfg, "localhost:8001"),
|
||||||
Url = hackney_url:make_url(Host, Path, QS1),
|
Path1 = prepare_path(Path),
|
||||||
|
Url = hackney_url:make_url(Host, Path1, QS1),
|
||||||
|
|
||||||
ConfigHackneyOpts = maps:get(hackney_opts, Cfg, []),
|
ConfigHackneyOpts = maps:get(hackney_opts, Cfg, []),
|
||||||
Body1 = case lists:keyfind(<<"Content-Type">>, 1, Headers1) of
|
Body1 = case lists:keyfind(<<"Content-Type">>, 1, Headers1) of
|
||||||
@ -38,6 +39,14 @@ request(_Ctx, Method, Path, QS, Headers, Body, Opts, Cfg) ->
|
|||||||
headers => RespHeaders}}
|
headers => RespHeaders}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
prepare_path(Path) ->
|
||||||
|
lists:map(fun convert/1, Path).
|
||||||
|
|
||||||
|
convert(PathPart) when is_integer(PathPart) ->
|
||||||
|
integer_to_binary(PathPart);
|
||||||
|
convert(PathPart) when is_list(PathPart) or is_binary(PathPart) ->
|
||||||
|
PathPart.
|
||||||
|
|
||||||
decode_response(Headers, Body) ->
|
decode_response(Headers, Body) ->
|
||||||
case lists:keyfind(<<"Content-Type">>, 1, Headers) of
|
case lists:keyfind(<<"Content-Type">>, 1, Headers) of
|
||||||
{_, <<"application/json", _/binary>>} ->
|
{_, <<"application/json", _/binary>>} ->
|
||||||
|
@ -1 +1 @@
|
|||||||
6.5.0-SNAPSHOT
|
5.3.0-SNAPSHOT
|
@ -12,6 +12,7 @@
|
|||||||
-define(BASE_URL, <<"/v2">>).
|
-define(BASE_URL, <<"/v2">>).
|
||||||
|
|
||||||
%% @doc Add a new pet to the store
|
%% @doc Add a new pet to the store
|
||||||
|
%%
|
||||||
-spec add_pet(ctx:ctx(), petstore_pet:petstore_pet()) -> {ok, petstore_pet:petstore_pet(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec add_pet(ctx:ctx(), petstore_pet:petstore_pet()) -> {ok, petstore_pet:petstore_pet(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
add_pet(Ctx, PetstorePet) ->
|
add_pet(Ctx, PetstorePet) ->
|
||||||
add_pet(Ctx, PetstorePet, #{}).
|
add_pet(Ctx, PetstorePet, #{}).
|
||||||
@ -22,16 +23,17 @@ add_pet(Ctx, PetstorePet, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = post,
|
Method = post,
|
||||||
Path = ["/pet"],
|
Path = [?BASE_URL, "/pet"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = PetstorePet,
|
Body1 = PetstorePet,
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>, <<"application/xml">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>, <<"application/xml">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Deletes a pet
|
%% @doc Deletes a pet
|
||||||
|
%%
|
||||||
-spec delete_pet(ctx:ctx(), integer()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec delete_pet(ctx:ctx(), integer()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
delete_pet(Ctx, PetId) ->
|
delete_pet(Ctx, PetId) ->
|
||||||
delete_pet(Ctx, PetId, #{}).
|
delete_pet(Ctx, PetId, #{}).
|
||||||
@ -42,14 +44,14 @@ delete_pet(Ctx, PetId, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = delete,
|
Method = delete,
|
||||||
Path = ["/pet/", PetId, ""],
|
Path = [?BASE_URL, "/pet/", PetId, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = []++petstore_utils:optional_params(['api_key'], _OptionalParams),
|
Headers = []++petstore_utils:optional_params(['api_key'], _OptionalParams),
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Finds Pets by status
|
%% @doc Finds Pets by status
|
||||||
%% Multiple status values can be provided with comma separated strings
|
%% Multiple status values can be provided with comma separated strings
|
||||||
@ -63,14 +65,14 @@ find_pets_by_status(Ctx, Status, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/pet/findByStatus"],
|
Path = [?BASE_URL, "/pet/findByStatus"],
|
||||||
QS = lists:flatten([[{<<"status">>, X} || X <- Status]])++petstore_utils:optional_params([], _OptionalParams),
|
QS = lists:flatten([[{<<"status">>, X} || X <- Status]])++petstore_utils:optional_params([], _OptionalParams),
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Finds Pets by tags
|
%% @doc Finds Pets by tags
|
||||||
%% Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
%% Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
||||||
@ -84,14 +86,14 @@ find_pets_by_tags(Ctx, Tags, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/pet/findByTags"],
|
Path = [?BASE_URL, "/pet/findByTags"],
|
||||||
QS = lists:flatten([[{<<"tags">>, X} || X <- Tags]])++petstore_utils:optional_params([], _OptionalParams),
|
QS = lists:flatten([[{<<"tags">>, X} || X <- Tags]])++petstore_utils:optional_params([], _OptionalParams),
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Find pet by ID
|
%% @doc Find pet by ID
|
||||||
%% Returns a single pet
|
%% Returns a single pet
|
||||||
@ -105,16 +107,17 @@ get_pet_by_id(Ctx, PetId, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/pet/", PetId, ""],
|
Path = [?BASE_URL, "/pet/", PetId, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Update an existing pet
|
%% @doc Update an existing pet
|
||||||
|
%%
|
||||||
-spec update_pet(ctx:ctx(), petstore_pet:petstore_pet()) -> {ok, petstore_pet:petstore_pet(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec update_pet(ctx:ctx(), petstore_pet:petstore_pet()) -> {ok, petstore_pet:petstore_pet(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
update_pet(Ctx, PetstorePet) ->
|
update_pet(Ctx, PetstorePet) ->
|
||||||
update_pet(Ctx, PetstorePet, #{}).
|
update_pet(Ctx, PetstorePet, #{}).
|
||||||
@ -125,16 +128,17 @@ update_pet(Ctx, PetstorePet, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = put,
|
Method = put,
|
||||||
Path = ["/pet"],
|
Path = [?BASE_URL, "/pet"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = PetstorePet,
|
Body1 = PetstorePet,
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>, <<"application/xml">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>, <<"application/xml">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Updates a pet in the store with form data
|
%% @doc Updates a pet in the store with form data
|
||||||
|
%%
|
||||||
-spec update_pet_with_form(ctx:ctx(), integer()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec update_pet_with_form(ctx:ctx(), integer()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
update_pet_with_form(Ctx, PetId) ->
|
update_pet_with_form(Ctx, PetId) ->
|
||||||
update_pet_with_form(Ctx, PetId, #{}).
|
update_pet_with_form(Ctx, PetId, #{}).
|
||||||
@ -145,16 +149,17 @@ update_pet_with_form(Ctx, PetId, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = post,
|
Method = post,
|
||||||
Path = ["/pet/", PetId, ""],
|
Path = [?BASE_URL, "/pet/", PetId, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = {form, []++petstore_utils:optional_params(['name', 'status'], _OptionalParams)},
|
Body1 = {form, []++petstore_utils:optional_params(['name', 'status'], _OptionalParams)},
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/x-www-form-urlencoded">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/x-www-form-urlencoded">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc uploads an image
|
%% @doc uploads an image
|
||||||
|
%%
|
||||||
-spec upload_file(ctx:ctx(), integer()) -> {ok, petstore_api_response:petstore_api_response(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec upload_file(ctx:ctx(), integer()) -> {ok, petstore_api_response:petstore_api_response(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
upload_file(Ctx, PetId) ->
|
upload_file(Ctx, PetId) ->
|
||||||
upload_file(Ctx, PetId, #{}).
|
upload_file(Ctx, PetId, #{}).
|
||||||
@ -165,13 +170,13 @@ upload_file(Ctx, PetId, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = post,
|
Method = post,
|
||||||
Path = ["/pet/", PetId, "/uploadImage"],
|
Path = [?BASE_URL, "/pet/", PetId, "/uploadImage"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = {form, []++petstore_utils:optional_params(['additionalMetadata', 'file'], _OptionalParams)},
|
Body1 = {form, []++petstore_utils:optional_params(['additionalMetadata', 'file'], _OptionalParams)},
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"multipart/form-data">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"multipart/form-data">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
|
||||||
|
@ -19,14 +19,14 @@ delete_order(Ctx, OrderId, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = delete,
|
Method = delete,
|
||||||
Path = ["/store/order/", OrderId, ""],
|
Path = [?BASE_URL, "/store/order/", OrderId, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Returns pet inventories by status
|
%% @doc Returns pet inventories by status
|
||||||
%% Returns a map of status codes to quantities
|
%% Returns a map of status codes to quantities
|
||||||
@ -40,14 +40,14 @@ get_inventory(Ctx, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/store/inventory"],
|
Path = [?BASE_URL, "/store/inventory"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Find purchase order by ID
|
%% @doc Find purchase order by ID
|
||||||
%% For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions
|
%% For valid response try integer IDs with value <= 5 or > 10. Other values will generate exceptions
|
||||||
@ -61,16 +61,17 @@ get_order_by_id(Ctx, OrderId, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/store/order/", OrderId, ""],
|
Path = [?BASE_URL, "/store/order/", OrderId, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Place an order for a pet
|
%% @doc Place an order for a pet
|
||||||
|
%%
|
||||||
-spec place_order(ctx:ctx(), petstore_order:petstore_order()) -> {ok, petstore_order:petstore_order(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec place_order(ctx:ctx(), petstore_order:petstore_order()) -> {ok, petstore_order:petstore_order(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
place_order(Ctx, PetstoreOrder) ->
|
place_order(Ctx, PetstoreOrder) ->
|
||||||
place_order(Ctx, PetstoreOrder, #{}).
|
place_order(Ctx, PetstoreOrder, #{}).
|
||||||
@ -81,13 +82,13 @@ place_order(Ctx, PetstoreOrder, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = post,
|
Method = post,
|
||||||
Path = ["/store/order"],
|
Path = [?BASE_URL, "/store/order"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = PetstoreOrder,
|
Body1 = PetstoreOrder,
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
|
||||||
|
@ -23,16 +23,17 @@ create_user(Ctx, PetstoreUser, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = post,
|
Method = post,
|
||||||
Path = ["/user"],
|
Path = [?BASE_URL, "/user"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = PetstoreUser,
|
Body1 = PetstoreUser,
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Creates list of users with given input array
|
%% @doc Creates list of users with given input array
|
||||||
|
%%
|
||||||
-spec create_users_with_array_input(ctx:ctx(), list()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec create_users_with_array_input(ctx:ctx(), list()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
create_users_with_array_input(Ctx, PetstoreUserArray) ->
|
create_users_with_array_input(Ctx, PetstoreUserArray) ->
|
||||||
create_users_with_array_input(Ctx, PetstoreUserArray, #{}).
|
create_users_with_array_input(Ctx, PetstoreUserArray, #{}).
|
||||||
@ -43,16 +44,17 @@ create_users_with_array_input(Ctx, PetstoreUserArray, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = post,
|
Method = post,
|
||||||
Path = ["/user/createWithArray"],
|
Path = [?BASE_URL, "/user/createWithArray"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = PetstoreUserArray,
|
Body1 = PetstoreUserArray,
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Creates list of users with given input array
|
%% @doc Creates list of users with given input array
|
||||||
|
%%
|
||||||
-spec create_users_with_list_input(ctx:ctx(), list()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec create_users_with_list_input(ctx:ctx(), list()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
create_users_with_list_input(Ctx, PetstoreUserArray) ->
|
create_users_with_list_input(Ctx, PetstoreUserArray) ->
|
||||||
create_users_with_list_input(Ctx, PetstoreUserArray, #{}).
|
create_users_with_list_input(Ctx, PetstoreUserArray, #{}).
|
||||||
@ -63,14 +65,14 @@ create_users_with_list_input(Ctx, PetstoreUserArray, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = post,
|
Method = post,
|
||||||
Path = ["/user/createWithList"],
|
Path = [?BASE_URL, "/user/createWithList"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = PetstoreUserArray,
|
Body1 = PetstoreUserArray,
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Delete user
|
%% @doc Delete user
|
||||||
%% This can only be done by the logged in user.
|
%% This can only be done by the logged in user.
|
||||||
@ -84,16 +86,17 @@ delete_user(Ctx, Username, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = delete,
|
Method = delete,
|
||||||
Path = ["/user/", Username, ""],
|
Path = [?BASE_URL, "/user/", Username, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Get user by user name
|
%% @doc Get user by user name
|
||||||
|
%%
|
||||||
-spec get_user_by_name(ctx:ctx(), binary()) -> {ok, petstore_user:petstore_user(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec get_user_by_name(ctx:ctx(), binary()) -> {ok, petstore_user:petstore_user(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
get_user_by_name(Ctx, Username) ->
|
get_user_by_name(Ctx, Username) ->
|
||||||
get_user_by_name(Ctx, Username, #{}).
|
get_user_by_name(Ctx, Username, #{}).
|
||||||
@ -104,16 +107,17 @@ get_user_by_name(Ctx, Username, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/user/", Username, ""],
|
Path = [?BASE_URL, "/user/", Username, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Logs user into the system
|
%% @doc Logs user into the system
|
||||||
|
%%
|
||||||
-spec login_user(ctx:ctx(), binary(), binary()) -> {ok, binary(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec login_user(ctx:ctx(), binary(), binary()) -> {ok, binary(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
login_user(Ctx, Username, Password) ->
|
login_user(Ctx, Username, Password) ->
|
||||||
login_user(Ctx, Username, Password, #{}).
|
login_user(Ctx, Username, Password, #{}).
|
||||||
@ -124,16 +128,17 @@ login_user(Ctx, Username, Password, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/user/login"],
|
Path = [?BASE_URL, "/user/login"],
|
||||||
QS = lists:flatten([{<<"username">>, Username}, {<<"password">>, Password}])++petstore_utils:optional_params([], _OptionalParams),
|
QS = lists:flatten([{<<"username">>, Username}, {<<"password">>, Password}])++petstore_utils:optional_params([], _OptionalParams),
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Logs out current logged in user session
|
%% @doc Logs out current logged in user session
|
||||||
|
%%
|
||||||
-spec logout_user(ctx:ctx()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
-spec logout_user(ctx:ctx()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
logout_user(Ctx) ->
|
logout_user(Ctx) ->
|
||||||
logout_user(Ctx, #{}).
|
logout_user(Ctx, #{}).
|
||||||
@ -144,14 +149,14 @@ logout_user(Ctx, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = get,
|
Method = get,
|
||||||
Path = ["/user/logout"],
|
Path = [?BASE_URL, "/user/logout"],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = [],
|
Body1 = [],
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
%% @doc Updated user
|
%% @doc Updated user
|
||||||
%% This can only be done by the logged in user.
|
%% This can only be done by the logged in user.
|
||||||
@ -165,13 +170,13 @@ update_user(Ctx, Username, PetstoreUser, Optional) ->
|
|||||||
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
Cfg = maps:get(cfg, Optional, application:get_env(petstore_api, config, #{})),
|
||||||
|
|
||||||
Method = put,
|
Method = put,
|
||||||
Path = ["/user/", Username, ""],
|
Path = [?BASE_URL, "/user/", Username, ""],
|
||||||
QS = [],
|
QS = [],
|
||||||
Headers = [],
|
Headers = [],
|
||||||
Body1 = PetstoreUser,
|
Body1 = PetstoreUser,
|
||||||
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>]),
|
||||||
Opts = maps:get(hackney_opts, Optional, []),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
petstore_utils:request(Ctx, Method, Path, QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
|
||||||
|
@ -11,7 +11,8 @@
|
|||||||
request(_Ctx, Method, Path, QS, Headers, Body, Opts, Cfg) ->
|
request(_Ctx, Method, Path, QS, Headers, Body, Opts, Cfg) ->
|
||||||
{Headers1, QS1} = update_params_with_auth(Cfg, Headers, QS),
|
{Headers1, QS1} = update_params_with_auth(Cfg, Headers, QS),
|
||||||
Host = maps:get(host, Cfg, "localhost:8001"),
|
Host = maps:get(host, Cfg, "localhost:8001"),
|
||||||
Url = hackney_url:make_url(Host, Path, QS1),
|
Path1 = prepare_path(Path),
|
||||||
|
Url = hackney_url:make_url(Host, Path1, QS1),
|
||||||
|
|
||||||
ConfigHackneyOpts = maps:get(hackney_opts, Cfg, []),
|
ConfigHackneyOpts = maps:get(hackney_opts, Cfg, []),
|
||||||
Body1 = case lists:keyfind(<<"Content-Type">>, 1, Headers1) of
|
Body1 = case lists:keyfind(<<"Content-Type">>, 1, Headers1) of
|
||||||
@ -38,6 +39,14 @@ request(_Ctx, Method, Path, QS, Headers, Body, Opts, Cfg) ->
|
|||||||
headers => RespHeaders}}
|
headers => RespHeaders}}
|
||||||
end.
|
end.
|
||||||
|
|
||||||
|
prepare_path(Path) ->
|
||||||
|
lists:map(fun convert/1, Path).
|
||||||
|
|
||||||
|
convert(PathPart) when is_integer(PathPart) ->
|
||||||
|
integer_to_binary(PathPart);
|
||||||
|
convert(PathPart) when is_list(PathPart) or is_binary(PathPart) ->
|
||||||
|
PathPart.
|
||||||
|
|
||||||
decode_response(Headers, Body) ->
|
decode_response(Headers, Body) ->
|
||||||
case lists:keyfind(<<"Content-Type">>, 1, Headers) of
|
case lists:keyfind(<<"Content-Type">>, 1, Headers) of
|
||||||
{_, <<"application/json", _/binary>>} ->
|
{_, <<"application/json", _/binary>>} ->
|
||||||
@ -72,12 +81,12 @@ auth_with_prefix(Cfg, Key, Token) ->
|
|||||||
|
|
||||||
update_params_with_auth(Cfg, Headers, QS) ->
|
update_params_with_auth(Cfg, Headers, QS) ->
|
||||||
AuthSettings = maps:get(auth, Cfg, #{}),
|
AuthSettings = maps:get(auth, Cfg, #{}),
|
||||||
Auths = #{ 'petstore_auth' =>
|
Auths = #{ 'api_key' =>
|
||||||
#{type => 'oauth2',
|
|
||||||
key => <<"Authorization">>,
|
|
||||||
in => header}, 'api_key' =>
|
|
||||||
#{type => 'apiKey',
|
#{type => 'apiKey',
|
||||||
key => <<"api_key">>,
|
key => <<"api_key">>,
|
||||||
|
in => header}, 'petstore_auth' =>
|
||||||
|
#{type => 'oauth2',
|
||||||
|
key => <<"Authorization">>,
|
||||||
in => header}},
|
in => header}},
|
||||||
|
|
||||||
maps:fold(fun(AuthName, #{type := _Type,
|
maps:fold(fun(AuthName, #{type := _Type,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user