mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-05-12 20:50:55 +00:00
[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
This commit is contained in:
parent
01a076c3fe
commit
c73118524c
@ -26,6 +26,6 @@ fi
|
|||||||
|
|
||||||
# if you've executed sbt assembly previously it will use that instead.
|
# if you've executed sbt assembly previously it will use that instead.
|
||||||
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
|
export JAVA_OPTS="${JAVA_OPTS} -XX:MaxPermSize=256M -Xmx1024M -DloggerPath=conf/log4j.properties"
|
||||||
ags="$@ generate -t modules/swagger-codegen/src/main/resources/erlang-client -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l erlang-client -o samples/client/petstore/erlang-client"
|
ags="$@ generate -t modules/swagger-codegen/src/main/resources/erlang-client -DpackageName=petstore -i modules/swagger-codegen/src/test/resources/2_0/petstore.yaml -l erlang-client -o samples/client/petstore/erlang-client"
|
||||||
|
|
||||||
java $JAVA_OPTS -jar $executable $ags
|
java $JAVA_OPTS -jar $executable $ags
|
||||||
|
@ -157,6 +157,7 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
|
|
||||||
supportingFiles.add(new SupportingFile("rebar.config.mustache","", "rebar.config"));
|
supportingFiles.add(new SupportingFile("rebar.config.mustache","", "rebar.config"));
|
||||||
supportingFiles.add(new SupportingFile("app.src.mustache", "", "src" + File.separator + this.packageName + ".app.src"));
|
supportingFiles.add(new SupportingFile("app.src.mustache", "", "src" + File.separator + this.packageName + ".app.src"));
|
||||||
|
supportingFiles.add(new SupportingFile("utils.mustache", "", "src" + File.separator + this.packageName + "_utils.erl"));
|
||||||
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
supportingFiles.add(new SupportingFile("README.mustache", "", "README.md"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -221,23 +222,24 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toModelName(String name) {
|
public String toModelName(String name) {
|
||||||
return this.packageName + "_" + underscore(name.replaceAll("-", "_"));
|
return this.packageName + "_" + underscore(name.replaceAll("-", "_").replaceAll("\\.", "_"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toApiName(String name) {
|
public String toApiName(String name) {
|
||||||
return this.packageName + "_" + underscore(name.replaceAll("-", "_"));
|
return this.packageName + "_" + underscore(name.replaceAll("-", "_").replaceAll("\\.", "_"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toModelFilename(String name) {
|
public String toModelFilename(String name) {
|
||||||
return this.packageName + "_" + underscore(name);
|
return this.packageName + "_" + underscore(name.replaceAll("\\.", "_"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toApiFilename(String name) {
|
public String toApiFilename(String name) {
|
||||||
// replace - with _ e.g. created-at => created_at
|
// replace - with _ e.g. created-at => created_at
|
||||||
name = name.replaceAll("-", "_"); // FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
// FIXME: a parameter should not be assigned. Also declare the methods parameters as 'final'.
|
||||||
|
name = name.replaceAll("-", "_").replaceAll("\\.", "_");
|
||||||
|
|
||||||
// e.g. PetApi.erl => pet_api.erl
|
// e.g. PetApi.erl => pet_api.erl
|
||||||
return this.packageName + "_" + underscore(name) + "_api";
|
return this.packageName + "_" + underscore(name) + "_api";
|
||||||
@ -247,11 +249,11 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
public String toOperationId(String operationId) {
|
public String toOperationId(String operationId) {
|
||||||
// method name cannot use reserved keyword, e.g. return
|
// method name cannot use reserved keyword, e.g. return
|
||||||
if (isReservedWord(operationId)) {
|
if (isReservedWord(operationId)) {
|
||||||
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + underscore(sanitizeName("call_" + operationId)));
|
LOGGER.warn(operationId + " (reserved word) cannot be used as method name. Renamed to " + underscore(sanitizeName("call_" + operationId)).replaceAll("\\.", "_"));
|
||||||
operationId = "call_" + operationId;
|
operationId = "call_" + operationId;
|
||||||
}
|
}
|
||||||
|
|
||||||
return underscore(operationId);
|
return underscore(operationId.replaceAll("\\.", "_"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -379,8 +381,8 @@ public class ErlangClientCodegen extends DefaultCodegen implements CodegenConfig
|
|||||||
this.produces = o.produces;
|
this.produces = o.produces;
|
||||||
this.bodyParam = o.bodyParam;
|
this.bodyParam = o.bodyParam;
|
||||||
this.allParams = o.allParams;
|
this.allParams = o.allParams;
|
||||||
this.arityRequired = Integer.toString(lengthRequired(o.allParams));
|
this.arityRequired = Integer.toString(lengthRequired(o.allParams)+1);
|
||||||
this.arityOptional = Integer.toString(lengthRequired(o.allParams)+1);
|
this.arityOptional = Integer.toString(lengthRequired(o.allParams)+2);
|
||||||
this.bodyParams = o.bodyParams;
|
this.bodyParams = o.bodyParams;
|
||||||
this.pathParams = o.pathParams;
|
this.pathParams = o.pathParams;
|
||||||
this.queryParams = o.queryParams;
|
this.queryParams = o.queryParams;
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
-export([{{#operations}}{{#operation}}{{^-first}},
|
-export([{{#operations}}{{#operation}}{{^-first}},
|
||||||
{{/-first}}{{operationId}}/{{arityRequired}}, {{operationId}}/{{arityOptional}}{{/operation}}{{/operations}}]).
|
{{/-first}}{{operationId}}/{{arityRequired}}, {{operationId}}/{{arityOptional}}{{/operation}}{{/operations}}]).
|
||||||
|
|
||||||
-define(BASE_URL, <<"{{{basePath}}}">>).
|
-define(BASE_URL, "{{{basePathWithoutHost}}}").
|
||||||
|
|
||||||
{{#operations}}
|
{{#operations}}
|
||||||
{{#operation}}
|
{{#operation}}
|
||||||
@ -11,41 +11,24 @@
|
|||||||
{{^notes.isEmpty}}
|
{{^notes.isEmpty}}
|
||||||
%% {{{notes}}}
|
%% {{{notes}}}
|
||||||
{{/notes.isEmpty}}
|
{{/notes.isEmpty}}
|
||||||
-spec {{operationId}}({{#allParams}}{{#required}}{{^-first}}, {{/-first}}{{{dataType}}}{{/required}}{{/allParams}}{{^bodyParams.isEmpty}}{{#bodyParams}}, term(){{/bodyParams}}{{/bodyParams.isEmpty}}) -> {{#returnType}}{ok, list(), {{{returnType}}}} | {error, string()}{{/returnType}}{{^returnType}}ok | {error, integer()}{{/returnType}}.
|
-spec {{operationId}}(ctx:ctx(){{#allParams}}{{#required}}, {{{dataType}}}{{/required}}{{/allParams}}) -> {ok, {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}[]{{/returnType}}, {{packageName}}_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), {{packageName}}_utils:response_info()}.
|
||||||
{{operationId}}({{#allParams}}{{#required}}{{^-first}}, {{/-first}}{{paramName}}{{/required}}{{/allParams}}{{^bodyParams.isEmpty}}{{#bodyParams}}, {{paramName}}{{/bodyParams}}{{/bodyParams.isEmpty}}) ->
|
{{operationId}}(Ctx{{#allParams}}{{#required}}, {{paramName}}{{/required}}{{/allParams}}) ->
|
||||||
{{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}, {{/bodyParams}}{{/bodyParams.isEmpty}}#{}).
|
{{operationId}}(Ctx{{#allParams}}{{#required}}, {{paramName}}{{/required}}{{/allParams}}, #{}).
|
||||||
|
|
||||||
|
-spec {{operationId}}(ctx:ctx(){{#allParams}}{{#required}}, {{{dataType}}}{{/required}}{{/allParams}}, maps:map()) -> {ok, {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}[]{{/returnType}}, {{packageName}}_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), {{packageName}}_utils:response_info()}.
|
||||||
|
{{operationId}}(Ctx{{#allParams}}{{#required}}, {{paramName}}{{/required}}{{/allParams}}, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
-spec {{operationId}}({{#allParams}}{{#required}}{{{dataType}}}, {{/required}}{{/allParams}}{{^bodyParams.isEmpty}}{{#bodyParams}}term(), {{/bodyParams}}{{/bodyParams.isEmpty}}maps:map()) -> {{#returnType}}{ok, list(), {{{returnType}}}} | {error, string()}{{/returnType}}{{^returnType}}ok | {error, integer()}{{/returnType}}.
|
|
||||||
{{operationId}}({{#allParams}}{{#required}}{{paramName}}, {{/required}}{{/allParams}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}{{/bodyParams}}, {{/bodyParams.isEmpty}}_Optional) ->
|
|
||||||
Method = {{httpMethod}},
|
Method = {{httpMethod}},
|
||||||
Path = ["{{{replacedPathName}}}"],
|
Path = ["{{{replacedPathName}}}"],
|
||||||
QS = {{#queryParams.isEmpty}}[]{{/queryParams.isEmpty}}{{^queryParams.isEmpty}}lists:flatten([{{#queryParams}}{{#required}}{{^-first}}, {{/-first}}{{#qsEncode}}{{this}}{{/qsEncode}}{{/required}}{{/queryParams}}])++[{X, maps:get(X, _Optional)} || X <- [{{#queryParams}}{{^required}}{{^-first}}, {{/-first}}'{{baseName}}'{{/required}}{{/queryParams}}], maps:is_key(X, _Optional)]{{/queryParams.isEmpty}},
|
QS = {{#queryParams.isEmpty}}[]{{/queryParams.isEmpty}}{{^queryParams.isEmpty}}lists:flatten([{{#queryParams}}{{#required}}{{^-first}}, {{/-first}}{{#qsEncode}}{{this}}{{/qsEncode}}{{/required}}{{/queryParams}}])++{{packageName}}_utils:optional_params([{{#queryParams}}{{^required}}{{^-first}}, {{/-first}}'{{baseName}}'{{/required}}{{/queryParams}}], _OptionalParams){{/queryParams.isEmpty}},
|
||||||
Headers = {{#headerParams.isEmpty}}[]{{/headerParams.isEmpty}}{{^headerParams.isEmpty}}[{{#headerParams}}{{#required}}{{^-first}}, {{/-first}}{<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/headerParams}}]++[{X, maps:get(X, _Optional)} || X <- [{{#headerParams}}{{^required}}{{^-first}}, {{/-first}}'{{baseName}}'{{/required}}{{/headerParams}}], maps:is_key(X, _Optional)]{{/headerParams.isEmpty}},
|
Headers = {{#headerParams.isEmpty}}[]{{/headerParams.isEmpty}}{{^headerParams.isEmpty}}[{{#headerParams}}{{#required}}{{^-first}}, {{/-first}}{<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/headerParams}}]++{{packageName}}_utils:optional_params([{{#headerParams}}{{^required}}{{^-first}}, {{/-first}}'{{baseName}}'{{/required}}{{/headerParams}}], _OptionalParams){{/headerParams.isEmpty}},
|
||||||
Body1 = {{^formParams.isEmpty}}{form, [{{#formParams}}{{#required}}{{^-first}}, {{/-first}}{<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/formParams}}]++[{X, maps:get(X, _Optional)} || X <- [{{#formParams}}{{^required}}{{^-first}}, {{/-first}}'{{baseName}}'{{/required}}{{/formParams}}], maps:is_key(X, _Optional)]}{{/formParams.isEmpty}}{{#formParams.isEmpty}}{{#bodyParams.isEmpty}}[]{{/bodyParams.isEmpty}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/bodyParams.isEmpty}}{{/formParams.isEmpty}},
|
Body1 = {{^formParams.isEmpty}}{form, [{{#formParams}}{{#required}}{{^-first}}, {{/-first}}{<<"{{baseName}}">>, {{paramName}}}{{/required}}{{/formParams}}]++{{packageName}}_utils:optional_params([{{#formParams}}{{^required}}{{^-first}}, {{/-first}}'{{baseName}}'{{/required}}{{/formParams}}], _OptionalParams)}{{/formParams.isEmpty}}{{#formParams.isEmpty}}{{#bodyParams.isEmpty}}[]{{/bodyParams.isEmpty}}{{^bodyParams.isEmpty}}{{#bodyParams}}{{paramName}}{{/bodyParams}}{{/bodyParams.isEmpty}}{{/formParams.isEmpty}},
|
||||||
Opts = [],
|
ContentTypeHeader = {{packageName}}_utils:select_header_content_type([{{#consumes}}{{^-first}}, {{/-first}}<<"{{mediaType}}">>{{/consumes}}]),
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
{{packageName}}_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
{{#returnType}}
|
|
||||||
{{#responses}}
|
|
||||||
{{#isDefault}}
|
|
||||||
{ok, {{code}}, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])}{{#hasMore}}; {{/hasMore}}
|
|
||||||
{{/isDefault}}
|
|
||||||
{{^isDefault}}
|
|
||||||
{ok, {{code}}, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "{{message}}"}{{#hasMore}}; {{/hasMore}}
|
|
||||||
{{/isDefault}}
|
|
||||||
{{/responses}}
|
|
||||||
{{/returnType}}
|
|
||||||
{{^returnType}}
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
{{/returnType}}
|
|
||||||
end.
|
|
||||||
|
|
||||||
{{/operation}}
|
{{/operation}}
|
||||||
|
|
||||||
|
@ -1,14 +1,15 @@
|
|||||||
{application, {{packageName}},
|
{application, {{packageName}},
|
||||||
[{description, {{#appDescription}}"{{appDescription}}"{{/appDescription}}{{^appDescription}}"Swagger client library"{{/appDescription}}},
|
[{description, {{#appDescription}}"{{appDescription}}"{{/appDescription}}{{^appDescription}}"Swagger client library"{{/appDescription}}},
|
||||||
{vsn, "{{apiVersion}}"},
|
{vsn, "{{#apiVersion}}{{apiVersion}}{{/apiVersion}}{{^apiVersion}}0.1.0{{/apiVersion}}"},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications,
|
{applications,
|
||||||
[kernel,
|
[kernel,
|
||||||
stdlib,
|
stdlib,
|
||||||
ssl,
|
ssl,
|
||||||
hackney
|
hackney,
|
||||||
|
ctx
|
||||||
]},
|
]},
|
||||||
{env, []},
|
{env, [{host, "{{#host}}{{{host}}}{{/host}}{{^host}}localhost{{/host}}"}]},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
|
|
||||||
{maintainers, []},
|
{maintainers, []},
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
{erl_opts, [debug_info, warnings_as_errors, warn_untyped_record]}.
|
{erl_opts, [debug_info, warnings_as_errors, warn_untyped_record]}.
|
||||||
|
|
||||||
{deps, [jsx, hackney]}.
|
{deps, [ctx, jsx, hackney]}.
|
||||||
|
|
||||||
|
{shell, [{apps, [{{packageName}}]}]}.
|
@ -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).
|
@ -1 +1 @@
|
|||||||
2.3.0-SNAPSHOT
|
2.3.0
|
@ -1,3 +1,5 @@
|
|||||||
{erl_opts, [debug_info, warnings_as_errors, warn_untyped_record]}.
|
{erl_opts, [debug_info, warnings_as_errors, warn_untyped_record]}.
|
||||||
|
|
||||||
{deps, [jsx, hackney]}.
|
{deps, [ctx, jsx, hackney]}.
|
||||||
|
|
||||||
|
{shell, [{apps, [petstore]}]}.
|
@ -1,14 +1,15 @@
|
|||||||
{application, swagger,
|
{application, petstore,
|
||||||
[{description, "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters."},
|
[{description, "This is a sample server Petstore server. You can find out more about Swagger at [http://swagger.io](http://swagger.io) or on [irc.freenode.net, #swagger](http://swagger.io/irc/). For this sample, you can use the api key `special-key` to test the authorization filters."},
|
||||||
{vsn, ""},
|
{vsn, "0.1.0"},
|
||||||
{registered, []},
|
{registered, []},
|
||||||
{applications,
|
{applications,
|
||||||
[kernel,
|
[kernel,
|
||||||
stdlib,
|
stdlib,
|
||||||
ssl,
|
ssl,
|
||||||
hackney
|
hackney,
|
||||||
|
ctx
|
||||||
]},
|
]},
|
||||||
{env, []},
|
{env, [{host, "petstore.swagger.io"}]},
|
||||||
{modules, []},
|
{modules, []},
|
||||||
|
|
||||||
{maintainers, []},
|
{maintainers, []},
|
@ -1,10 +1,10 @@
|
|||||||
-module(swagger_api_response).
|
-module(petstore_api_response).
|
||||||
|
|
||||||
-export([encode/1]).
|
-export([encode/1]).
|
||||||
|
|
||||||
-export_type([swagger_api_response/0]).
|
-export_type([petstore_api_response/0]).
|
||||||
|
|
||||||
-type swagger_api_response() ::
|
-type petstore_api_response() ::
|
||||||
#{ 'code' => integer(),
|
#{ 'code' => integer(),
|
||||||
'type' => binary(),
|
'type' => binary(),
|
||||||
'message' => binary()
|
'message' => binary()
|
@ -1,10 +1,10 @@
|
|||||||
-module(swagger_category).
|
-module(petstore_category).
|
||||||
|
|
||||||
-export([encode/1]).
|
-export([encode/1]).
|
||||||
|
|
||||||
-export_type([swagger_category/0]).
|
-export_type([petstore_category/0]).
|
||||||
|
|
||||||
-type swagger_category() ::
|
-type petstore_category() ::
|
||||||
#{ 'id' => integer(),
|
#{ 'id' => integer(),
|
||||||
'name' => binary()
|
'name' => binary()
|
||||||
}.
|
}.
|
@ -1,14 +1,14 @@
|
|||||||
-module(swagger_order).
|
-module(petstore_order).
|
||||||
|
|
||||||
-export([encode/1]).
|
-export([encode/1]).
|
||||||
|
|
||||||
-export_type([swagger_order/0]).
|
-export_type([petstore_order/0]).
|
||||||
|
|
||||||
-type swagger_order() ::
|
-type petstore_order() ::
|
||||||
#{ 'id' => integer(),
|
#{ 'id' => integer(),
|
||||||
'petId' => integer(),
|
'petId' => integer(),
|
||||||
'quantity' => integer(),
|
'quantity' => integer(),
|
||||||
'shipDate' => swagger_date_time:swagger_date_time(),
|
'shipDate' => petstore_date_time:petstore_date_time(),
|
||||||
'status' => binary(),
|
'status' => binary(),
|
||||||
'complete' => boolean()
|
'complete' => boolean()
|
||||||
}.
|
}.
|
@ -1,12 +1,12 @@
|
|||||||
-module(swagger_pet).
|
-module(petstore_pet).
|
||||||
|
|
||||||
-export([encode/1]).
|
-export([encode/1]).
|
||||||
|
|
||||||
-export_type([swagger_pet/0]).
|
-export_type([petstore_pet/0]).
|
||||||
|
|
||||||
-type swagger_pet() ::
|
-type petstore_pet() ::
|
||||||
#{ 'id' => integer(),
|
#{ 'id' => integer(),
|
||||||
'category' => swagger_category:swagger_category(),
|
'category' => petstore_category:petstore_category(),
|
||||||
'name' := binary(),
|
'name' := binary(),
|
||||||
'photoUrls' := list(),
|
'photoUrls' := list(),
|
||||||
'tags' => list(),
|
'tags' => list(),
|
177
samples/client/petstore/erlang-client/src/petstore_pet_api.erl
Normal file
177
samples/client/petstore/erlang-client/src/petstore_pet_api.erl
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
-module(petstore_pet_api).
|
||||||
|
|
||||||
|
-export([add_pet/2, add_pet/3,
|
||||||
|
delete_pet/2, delete_pet/3,
|
||||||
|
find_pets_by_status/2, find_pets_by_status/3,
|
||||||
|
find_pets_by_tags/2, find_pets_by_tags/3,
|
||||||
|
get_pet_by_id/2, get_pet_by_id/3,
|
||||||
|
update_pet/2, update_pet/3,
|
||||||
|
update_pet_with_form/2, update_pet_with_form/3,
|
||||||
|
upload_file/2, upload_file/3]).
|
||||||
|
|
||||||
|
-define(BASE_URL, "/v2").
|
||||||
|
|
||||||
|
%% @doc Add a new pet to the store
|
||||||
|
-spec add_pet(ctx:ctx(), petstore_pet:petstore_pet()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
add_pet(Ctx, Body) ->
|
||||||
|
add_pet(Ctx, Body, #{}).
|
||||||
|
|
||||||
|
-spec add_pet(ctx:ctx(), petstore_pet:petstore_pet(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
add_pet(Ctx, Body, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = post,
|
||||||
|
Path = ["/pet"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = Body,
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>, <<"application/xml">>]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
delete_pet(Ctx, PetId) ->
|
||||||
|
delete_pet(Ctx, PetId, #{}).
|
||||||
|
|
||||||
|
-spec delete_pet(ctx:ctx(), integer(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
delete_pet(Ctx, PetId, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = delete,
|
||||||
|
Path = ["/pet/", PetId, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = []++petstore_utils:optional_params(['api_key'], _OptionalParams),
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Finds Pets by status
|
||||||
|
%% Multiple status values can be provided with comma separated strings
|
||||||
|
-spec find_pets_by_status(ctx:ctx(), list()) -> {ok, [petstore_pet:petstore_pet()], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
find_pets_by_status(Ctx, Status) ->
|
||||||
|
find_pets_by_status(Ctx, Status, #{}).
|
||||||
|
|
||||||
|
-spec find_pets_by_status(ctx:ctx(), list(), maps:map()) -> {ok, [petstore_pet:petstore_pet()], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
find_pets_by_status(Ctx, Status, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/pet/findByStatus"],
|
||||||
|
QS = lists:flatten([[{<<"status">>, X} || X <- Status]])++petstore_utils:optional_params([], _OptionalParams),
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Finds Pets by tags
|
||||||
|
%% Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
||||||
|
-spec find_pets_by_tags(ctx:ctx(), list()) -> {ok, [petstore_pet:petstore_pet()], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
find_pets_by_tags(Ctx, Tags) ->
|
||||||
|
find_pets_by_tags(Ctx, Tags, #{}).
|
||||||
|
|
||||||
|
-spec find_pets_by_tags(ctx:ctx(), list(), maps:map()) -> {ok, [petstore_pet:petstore_pet()], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
find_pets_by_tags(Ctx, Tags, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/pet/findByTags"],
|
||||||
|
QS = lists:flatten([[{<<"tags">>, X} || X <- Tags]])++petstore_utils:optional_params([], _OptionalParams),
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Find pet by ID
|
||||||
|
%% Returns a single pet
|
||||||
|
-spec get_pet_by_id(ctx:ctx(), integer()) -> {ok, petstore_pet:petstore_pet(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
get_pet_by_id(Ctx, PetId) ->
|
||||||
|
get_pet_by_id(Ctx, PetId, #{}).
|
||||||
|
|
||||||
|
-spec get_pet_by_id(ctx:ctx(), integer(), maps:map()) -> {ok, petstore_pet:petstore_pet(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
get_pet_by_id(Ctx, PetId, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/pet/", PetId, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Update an existing pet
|
||||||
|
-spec update_pet(ctx:ctx(), petstore_pet:petstore_pet()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
update_pet(Ctx, Body) ->
|
||||||
|
update_pet(Ctx, Body, #{}).
|
||||||
|
|
||||||
|
-spec update_pet(ctx:ctx(), petstore_pet:petstore_pet(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
update_pet(Ctx, Body, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = put,
|
||||||
|
Path = ["/pet"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = Body,
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/json">>, <<"application/xml">>]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
update_pet_with_form(Ctx, PetId) ->
|
||||||
|
update_pet_with_form(Ctx, PetId, #{}).
|
||||||
|
|
||||||
|
-spec update_pet_with_form(ctx:ctx(), integer(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
update_pet_with_form(Ctx, PetId, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = post,
|
||||||
|
Path = ["/pet/", PetId, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = {form, []++petstore_utils:optional_params(['name', 'status'], _OptionalParams)},
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"application/x-www-form-urlencoded">>]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
upload_file(Ctx, PetId) ->
|
||||||
|
upload_file(Ctx, PetId, #{}).
|
||||||
|
|
||||||
|
-spec upload_file(ctx:ctx(), integer(), maps:map()) -> {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, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = post,
|
||||||
|
Path = ["/pet/", PetId, "/uploadImage"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = {form, []++petstore_utils:optional_params(['additionalMetadata', 'file'], _OptionalParams)},
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([<<"multipart/form-data">>]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
|
@ -0,0 +1,93 @@
|
|||||||
|
-module(petstore_store_api).
|
||||||
|
|
||||||
|
-export([delete_order/2, delete_order/3,
|
||||||
|
get_inventory/1, get_inventory/2,
|
||||||
|
get_order_by_id/2, get_order_by_id/3,
|
||||||
|
place_order/2, place_order/3]).
|
||||||
|
|
||||||
|
-define(BASE_URL, "/v2").
|
||||||
|
|
||||||
|
%% @doc Delete purchase order by ID
|
||||||
|
%% For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
||||||
|
-spec delete_order(ctx:ctx(), binary()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
delete_order(Ctx, OrderId) ->
|
||||||
|
delete_order(Ctx, OrderId, #{}).
|
||||||
|
|
||||||
|
-spec delete_order(ctx:ctx(), binary(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
delete_order(Ctx, OrderId, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = delete,
|
||||||
|
Path = ["/store/order/", OrderId, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Returns pet inventories by status
|
||||||
|
%% Returns a map of status codes to quantities
|
||||||
|
-spec get_inventory(ctx:ctx()) -> {ok, maps:map(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
get_inventory(Ctx) ->
|
||||||
|
get_inventory(Ctx, #{}).
|
||||||
|
|
||||||
|
-spec get_inventory(ctx:ctx(), maps:map()) -> {ok, maps:map(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
get_inventory(Ctx, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/store/inventory"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Find purchase order by ID
|
||||||
|
%% For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
||||||
|
-spec get_order_by_id(ctx:ctx(), integer()) -> {ok, petstore_order:petstore_order(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
get_order_by_id(Ctx, OrderId) ->
|
||||||
|
get_order_by_id(Ctx, OrderId, #{}).
|
||||||
|
|
||||||
|
-spec get_order_by_id(ctx:ctx(), integer(), maps:map()) -> {ok, petstore_order:petstore_order(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
get_order_by_id(Ctx, OrderId, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/store/order/", OrderId, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
place_order(Ctx, Body) ->
|
||||||
|
place_order(Ctx, Body, #{}).
|
||||||
|
|
||||||
|
-spec place_order(ctx:ctx(), petstore_order:petstore_order(), maps:map()) -> {ok, petstore_order:petstore_order(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
place_order(Ctx, Body, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = post,
|
||||||
|
Path = ["/store/order"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = Body,
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
|
@ -1,10 +1,10 @@
|
|||||||
-module(swagger_tag).
|
-module(petstore_tag).
|
||||||
|
|
||||||
-export([encode/1]).
|
-export([encode/1]).
|
||||||
|
|
||||||
-export_type([swagger_tag/0]).
|
-export_type([petstore_tag/0]).
|
||||||
|
|
||||||
-type swagger_tag() ::
|
-type petstore_tag() ::
|
||||||
#{ 'id' => integer(),
|
#{ 'id' => integer(),
|
||||||
'name' => binary()
|
'name' => binary()
|
||||||
}.
|
}.
|
@ -1,10 +1,10 @@
|
|||||||
-module(swagger_user).
|
-module(petstore_user).
|
||||||
|
|
||||||
-export([encode/1]).
|
-export([encode/1]).
|
||||||
|
|
||||||
-export_type([swagger_user/0]).
|
-export_type([petstore_user/0]).
|
||||||
|
|
||||||
-type swagger_user() ::
|
-type petstore_user() ::
|
||||||
#{ 'id' => integer(),
|
#{ 'id' => integer(),
|
||||||
'username' => binary(),
|
'username' => binary(),
|
||||||
'firstName' => binary(),
|
'firstName' => binary(),
|
177
samples/client/petstore/erlang-client/src/petstore_user_api.erl
Normal file
177
samples/client/petstore/erlang-client/src/petstore_user_api.erl
Normal file
@ -0,0 +1,177 @@
|
|||||||
|
-module(petstore_user_api).
|
||||||
|
|
||||||
|
-export([create_user/2, create_user/3,
|
||||||
|
create_users_with_array_input/2, create_users_with_array_input/3,
|
||||||
|
create_users_with_list_input/2, create_users_with_list_input/3,
|
||||||
|
delete_user/2, delete_user/3,
|
||||||
|
get_user_by_name/2, get_user_by_name/3,
|
||||||
|
login_user/3, login_user/4,
|
||||||
|
logout_user/1, logout_user/2,
|
||||||
|
update_user/3, update_user/4]).
|
||||||
|
|
||||||
|
-define(BASE_URL, "/v2").
|
||||||
|
|
||||||
|
%% @doc Create user
|
||||||
|
%% This can only be done by the logged in user.
|
||||||
|
-spec create_user(ctx:ctx(), petstore_user:petstore_user()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
create_user(Ctx, Body) ->
|
||||||
|
create_user(Ctx, Body, #{}).
|
||||||
|
|
||||||
|
-spec create_user(ctx:ctx(), petstore_user:petstore_user(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
create_user(Ctx, Body, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = post,
|
||||||
|
Path = ["/user"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = Body,
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
create_users_with_array_input(Ctx, Body) ->
|
||||||
|
create_users_with_array_input(Ctx, Body, #{}).
|
||||||
|
|
||||||
|
-spec create_users_with_array_input(ctx:ctx(), list(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
create_users_with_array_input(Ctx, Body, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = post,
|
||||||
|
Path = ["/user/createWithArray"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = Body,
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
create_users_with_list_input(Ctx, Body) ->
|
||||||
|
create_users_with_list_input(Ctx, Body, #{}).
|
||||||
|
|
||||||
|
-spec create_users_with_list_input(ctx:ctx(), list(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
create_users_with_list_input(Ctx, Body, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = post,
|
||||||
|
Path = ["/user/createWithList"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = Body,
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Delete user
|
||||||
|
%% This can only be done by the logged in user.
|
||||||
|
-spec delete_user(ctx:ctx(), binary()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
delete_user(Ctx, Username) ->
|
||||||
|
delete_user(Ctx, Username, #{}).
|
||||||
|
|
||||||
|
-spec delete_user(ctx:ctx(), binary(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
delete_user(Ctx, Username, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = delete,
|
||||||
|
Path = ["/user/", Username, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
get_user_by_name(Ctx, Username) ->
|
||||||
|
get_user_by_name(Ctx, Username, #{}).
|
||||||
|
|
||||||
|
-spec get_user_by_name(ctx:ctx(), binary(), maps:map()) -> {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, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/user/", Username, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
login_user(Ctx, Username, Password) ->
|
||||||
|
login_user(Ctx, Username, Password, #{}).
|
||||||
|
|
||||||
|
-spec login_user(ctx:ctx(), binary(), binary(), maps:map()) -> {ok, binary(), petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
login_user(Ctx, Username, Password, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/user/login"],
|
||||||
|
QS = lists:flatten([{<<"username">>, Username}, {<<"password">>, Password}])++petstore_utils:optional_params([], _OptionalParams),
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @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()}.
|
||||||
|
logout_user(Ctx) ->
|
||||||
|
logout_user(Ctx, #{}).
|
||||||
|
|
||||||
|
-spec logout_user(ctx:ctx(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
logout_user(Ctx, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = get,
|
||||||
|
Path = ["/user/logout"],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = [],
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
%% @doc Updated user
|
||||||
|
%% This can only be done by the logged in user.
|
||||||
|
-spec update_user(ctx:ctx(), binary(), petstore_user:petstore_user()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
update_user(Ctx, Username, Body) ->
|
||||||
|
update_user(Ctx, Username, Body, #{}).
|
||||||
|
|
||||||
|
-spec update_user(ctx:ctx(), binary(), petstore_user:petstore_user(), maps:map()) -> {ok, [], petstore_utils:response_info()} | {ok, hackney:client_ref()} | {error, term(), petstore_utils:response_info()}.
|
||||||
|
update_user(Ctx, Username, Body, Optional) ->
|
||||||
|
_OptionalParams = maps:get(params, Optional, #{}),
|
||||||
|
Cfg = maps:get(cfg, Optional, application:get_env(kuberl, config, #{})),
|
||||||
|
|
||||||
|
Method = put,
|
||||||
|
Path = ["/user/", Username, ""],
|
||||||
|
QS = [],
|
||||||
|
Headers = [],
|
||||||
|
Body1 = Body,
|
||||||
|
ContentTypeHeader = petstore_utils:select_header_content_type([]),
|
||||||
|
Opts = maps:get(hackney_opts, Optional, []),
|
||||||
|
|
||||||
|
petstore_utils:request(Ctx, Method, [?BASE_URL, Path], QS, ContentTypeHeader++Headers, Body1, Opts, Cfg).
|
||||||
|
|
||||||
|
|
97
samples/client/petstore/erlang-client/src/petstore_utils.erl
Normal file
97
samples/client/petstore/erlang-client/src/petstore_utils.erl
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
-module(petstore_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 = #{ 'api_key' =>
|
||||||
|
#{type => 'apiKey',
|
||||||
|
key => <<"api_key">>,
|
||||||
|
in => header}, 'petstore_auth' =>
|
||||||
|
#{type => 'oauth2',
|
||||||
|
key => <<"">>,
|
||||||
|
in => }},
|
||||||
|
|
||||||
|
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).
|
@ -1,197 +0,0 @@
|
|||||||
-module(swagger_pet_api).
|
|
||||||
|
|
||||||
-export([add_pet/1, add_pet/2,
|
|
||||||
delete_pet/1, delete_pet/2,
|
|
||||||
find_pets_by_status/1, find_pets_by_status/2,
|
|
||||||
find_pets_by_tags/1, find_pets_by_tags/2,
|
|
||||||
get_pet_by_id/1, get_pet_by_id/2,
|
|
||||||
update_pet/1, update_pet/2,
|
|
||||||
update_pet_with_form/1, update_pet_with_form/2,
|
|
||||||
upload_file/1, upload_file/2]).
|
|
||||||
|
|
||||||
-define(BASE_URL, <<"http://petstore.swagger.io/v2">>).
|
|
||||||
|
|
||||||
%% @doc Add a new pet to the store
|
|
||||||
-spec add_pet(swagger_pet:swagger_pet(), term()) -> ok | {error, integer()}.
|
|
||||||
add_pet(Body) ->
|
|
||||||
add_pet(Body, Body, #{}).
|
|
||||||
|
|
||||||
-spec add_pet(swagger_pet:swagger_pet(), term(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
add_pet(Body, Body, _Optional) ->
|
|
||||||
Method = post,
|
|
||||||
Path = ["/pet"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = Body,
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Deletes a pet
|
|
||||||
-spec delete_pet(integer()) -> ok | {error, integer()}.
|
|
||||||
delete_pet(PetId) ->
|
|
||||||
delete_pet(PetId, , #{}).
|
|
||||||
|
|
||||||
-spec delete_pet(integer(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
delete_pet(PetId, _Optional) ->
|
|
||||||
Method = delete,
|
|
||||||
Path = ["/pet/", PetId, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = []++[{X, maps:get(X, _Optional)} || X <- ['api_key'], maps:is_key(X, _Optional)],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Finds Pets by status
|
|
||||||
%% Multiple status values can be provided with comma separated strings
|
|
||||||
-spec find_pets_by_status(list()) -> {ok, list(), [swagger_pet:swagger_pet()]} | {error, string()}.
|
|
||||||
find_pets_by_status(Status) ->
|
|
||||||
find_pets_by_status(Status, , #{}).
|
|
||||||
|
|
||||||
-spec find_pets_by_status(list(), maps:map()) -> {ok, list(), [swagger_pet:swagger_pet()]} | {error, string()}.
|
|
||||||
find_pets_by_status(Status, _Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/pet/findByStatus"],
|
|
||||||
QS = lists:flatten([[{<<"status">>, X} || X <- Status]])++[{X, maps:get(X, _Optional)} || X <- [], maps:is_key(X, _Optional)],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])};
|
|
||||||
{ok, 400, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Invalid status value"}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Finds Pets by tags
|
|
||||||
%% Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3 for testing.
|
|
||||||
-spec find_pets_by_tags(list()) -> {ok, list(), [swagger_pet:swagger_pet()]} | {error, string()}.
|
|
||||||
find_pets_by_tags(Tags) ->
|
|
||||||
find_pets_by_tags(Tags, , #{}).
|
|
||||||
|
|
||||||
-spec find_pets_by_tags(list(), maps:map()) -> {ok, list(), [swagger_pet:swagger_pet()]} | {error, string()}.
|
|
||||||
find_pets_by_tags(Tags, _Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/pet/findByTags"],
|
|
||||||
QS = lists:flatten([[{<<"tags">>, X} || X <- Tags]])++[{X, maps:get(X, _Optional)} || X <- [], maps:is_key(X, _Optional)],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])};
|
|
||||||
{ok, 400, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Invalid tag value"}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Find pet by ID
|
|
||||||
%% Returns a single pet
|
|
||||||
-spec get_pet_by_id(integer()) -> {ok, list(), swagger_pet:swagger_pet()} | {error, string()}.
|
|
||||||
get_pet_by_id(PetId) ->
|
|
||||||
get_pet_by_id(PetId, , #{}).
|
|
||||||
|
|
||||||
-spec get_pet_by_id(integer(), maps:map()) -> {ok, list(), swagger_pet:swagger_pet()} | {error, string()}.
|
|
||||||
get_pet_by_id(PetId, _Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/pet/", PetId, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])};
|
|
||||||
{ok, 400, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Invalid ID supplied"};
|
|
||||||
{ok, 404, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Pet not found"}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Update an existing pet
|
|
||||||
-spec update_pet(swagger_pet:swagger_pet(), term()) -> ok | {error, integer()}.
|
|
||||||
update_pet(Body) ->
|
|
||||||
update_pet(Body, Body, #{}).
|
|
||||||
|
|
||||||
-spec update_pet(swagger_pet:swagger_pet(), term(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
update_pet(Body, Body, _Optional) ->
|
|
||||||
Method = put,
|
|
||||||
Path = ["/pet"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = Body,
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Updates a pet in the store with form data
|
|
||||||
-spec update_pet_with_form(integer()) -> ok | {error, integer()}.
|
|
||||||
update_pet_with_form(PetId) ->
|
|
||||||
update_pet_with_form(PetId, , #{}).
|
|
||||||
|
|
||||||
-spec update_pet_with_form(integer(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
update_pet_with_form(PetId, _Optional) ->
|
|
||||||
Method = post,
|
|
||||||
Path = ["/pet/", PetId, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = {form, []++[{X, maps:get(X, _Optional)} || X <- ['name', 'status'], maps:is_key(X, _Optional)]},
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc uploads an image
|
|
||||||
-spec upload_file(integer()) -> {ok, list(), swagger_api_response:swagger_api_response()} | {error, string()}.
|
|
||||||
upload_file(PetId) ->
|
|
||||||
upload_file(PetId, , #{}).
|
|
||||||
|
|
||||||
-spec upload_file(integer(), maps:map()) -> {ok, list(), swagger_api_response:swagger_api_response()} | {error, string()}.
|
|
||||||
upload_file(PetId, _Optional) ->
|
|
||||||
Method = post,
|
|
||||||
Path = ["/pet/", PetId, "/uploadImage"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = {form, []++[{X, maps:get(X, _Optional)} || X <- ['additionalMetadata', 'file'], maps:is_key(X, _Optional)]},
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])}
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
@ -1,104 +0,0 @@
|
|||||||
-module(swagger_store_api).
|
|
||||||
|
|
||||||
-export([delete_order/1, delete_order/2,
|
|
||||||
get_inventory/0, get_inventory/1,
|
|
||||||
get_order_by_id/1, get_order_by_id/2,
|
|
||||||
place_order/1, place_order/2]).
|
|
||||||
|
|
||||||
-define(BASE_URL, <<"http://petstore.swagger.io/v2">>).
|
|
||||||
|
|
||||||
%% @doc Delete purchase order by ID
|
|
||||||
%% For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
|
|
||||||
-spec delete_order(binary()) -> ok | {error, integer()}.
|
|
||||||
delete_order(OrderId) ->
|
|
||||||
delete_order(OrderId, , #{}).
|
|
||||||
|
|
||||||
-spec delete_order(binary(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
delete_order(OrderId, _Optional) ->
|
|
||||||
Method = delete,
|
|
||||||
Path = ["/store/order/", OrderId, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Returns pet inventories by status
|
|
||||||
%% Returns a map of status codes to quantities
|
|
||||||
-spec get_inventory() -> {ok, list(), maps:map()} | {error, string()}.
|
|
||||||
get_inventory() ->
|
|
||||||
get_inventory(, #{}).
|
|
||||||
|
|
||||||
-spec get_inventory(maps:map()) -> {ok, list(), maps:map()} | {error, string()}.
|
|
||||||
get_inventory(_Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/store/inventory"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Find purchase order by ID
|
|
||||||
%% For valid response try integer IDs with value <= 5 or > 10. Other values will generated exceptions
|
|
||||||
-spec get_order_by_id(integer()) -> {ok, list(), swagger_order:swagger_order()} | {error, string()}.
|
|
||||||
get_order_by_id(OrderId) ->
|
|
||||||
get_order_by_id(OrderId, , #{}).
|
|
||||||
|
|
||||||
-spec get_order_by_id(integer(), maps:map()) -> {ok, list(), swagger_order:swagger_order()} | {error, string()}.
|
|
||||||
get_order_by_id(OrderId, _Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/store/order/", OrderId, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])};
|
|
||||||
{ok, 400, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Invalid ID supplied"};
|
|
||||||
{ok, 404, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Order not found"}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Place an order for a pet
|
|
||||||
-spec place_order(swagger_order:swagger_order(), term()) -> {ok, list(), swagger_order:swagger_order()} | {error, string()}.
|
|
||||||
place_order(Body) ->
|
|
||||||
place_order(Body, Body, #{}).
|
|
||||||
|
|
||||||
-spec place_order(swagger_order:swagger_order(), term(), maps:map()) -> {ok, list(), swagger_order:swagger_order()} | {error, string()}.
|
|
||||||
place_order(Body, Body, _Optional) ->
|
|
||||||
Method = post,
|
|
||||||
Path = ["/store/order"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = Body,
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])};
|
|
||||||
{ok, 400, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Invalid Order"}
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
@ -1,197 +0,0 @@
|
|||||||
-module(swagger_user_api).
|
|
||||||
|
|
||||||
-export([create_user/1, create_user/2,
|
|
||||||
create_users_with_array_input/1, create_users_with_array_input/2,
|
|
||||||
create_users_with_list_input/1, create_users_with_list_input/2,
|
|
||||||
delete_user/1, delete_user/2,
|
|
||||||
get_user_by_name/1, get_user_by_name/2,
|
|
||||||
login_user/2, login_user/3,
|
|
||||||
logout_user/0, logout_user/1,
|
|
||||||
update_user/2, update_user/3]).
|
|
||||||
|
|
||||||
-define(BASE_URL, <<"http://petstore.swagger.io/v2">>).
|
|
||||||
|
|
||||||
%% @doc Create user
|
|
||||||
%% This can only be done by the logged in user.
|
|
||||||
-spec create_user(swagger_user:swagger_user(), term()) -> ok | {error, integer()}.
|
|
||||||
create_user(Body) ->
|
|
||||||
create_user(Body, Body, #{}).
|
|
||||||
|
|
||||||
-spec create_user(swagger_user:swagger_user(), term(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
create_user(Body, Body, _Optional) ->
|
|
||||||
Method = post,
|
|
||||||
Path = ["/user"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = Body,
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Creates list of users with given input array
|
|
||||||
-spec create_users_with_array_input(list(), term()) -> ok | {error, integer()}.
|
|
||||||
create_users_with_array_input(Body) ->
|
|
||||||
create_users_with_array_input(Body, Body, #{}).
|
|
||||||
|
|
||||||
-spec create_users_with_array_input(list(), term(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
create_users_with_array_input(Body, Body, _Optional) ->
|
|
||||||
Method = post,
|
|
||||||
Path = ["/user/createWithArray"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = Body,
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Creates list of users with given input array
|
|
||||||
-spec create_users_with_list_input(list(), term()) -> ok | {error, integer()}.
|
|
||||||
create_users_with_list_input(Body) ->
|
|
||||||
create_users_with_list_input(Body, Body, #{}).
|
|
||||||
|
|
||||||
-spec create_users_with_list_input(list(), term(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
create_users_with_list_input(Body, Body, _Optional) ->
|
|
||||||
Method = post,
|
|
||||||
Path = ["/user/createWithList"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = Body,
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Delete user
|
|
||||||
%% This can only be done by the logged in user.
|
|
||||||
-spec delete_user(binary()) -> ok | {error, integer()}.
|
|
||||||
delete_user(Username) ->
|
|
||||||
delete_user(Username, , #{}).
|
|
||||||
|
|
||||||
-spec delete_user(binary(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
delete_user(Username, _Optional) ->
|
|
||||||
Method = delete,
|
|
||||||
Path = ["/user/", Username, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Get user by user name
|
|
||||||
-spec get_user_by_name(binary()) -> {ok, list(), swagger_user:swagger_user()} | {error, string()}.
|
|
||||||
get_user_by_name(Username) ->
|
|
||||||
get_user_by_name(Username, , #{}).
|
|
||||||
|
|
||||||
-spec get_user_by_name(binary(), maps:map()) -> {ok, list(), swagger_user:swagger_user()} | {error, string()}.
|
|
||||||
get_user_by_name(Username, _Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/user/", Username, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])};
|
|
||||||
{ok, 400, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Invalid username supplied"};
|
|
||||||
{ok, 404, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "User not found"}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Logs user into the system
|
|
||||||
-spec login_user(binary(), binary()) -> {ok, list(), binary()} | {error, string()}.
|
|
||||||
login_user(Username, Password) ->
|
|
||||||
login_user(Username, Password, , #{}).
|
|
||||||
|
|
||||||
-spec login_user(binary(), binary(), maps:map()) -> {ok, list(), binary()} | {error, string()}.
|
|
||||||
login_user(Username, Password, _Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/user/login"],
|
|
||||||
QS = lists:flatten([{<<"username">>, Username}, {<<"password">>, Password}])++[{X, maps:get(X, _Optional)} || X <- [], maps:is_key(X, _Optional)],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, RespHeaders, ClientRef} ->
|
|
||||||
{ok, ResponseBody} = hackney:body(ClientRef),
|
|
||||||
{ok, RespHeaders, jsx:decode(ResponseBody, [return_maps])};
|
|
||||||
{ok, 400, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, "Invalid username/password supplied"}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Logs out current logged in user session
|
|
||||||
-spec logout_user() -> ok | {error, integer()}.
|
|
||||||
logout_user() ->
|
|
||||||
logout_user(, #{}).
|
|
||||||
|
|
||||||
-spec logout_user(maps:map()) -> ok | {error, integer()}.
|
|
||||||
logout_user(_Optional) ->
|
|
||||||
Method = get,
|
|
||||||
Path = ["/user/logout"],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = [],
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
%% @doc Updated user
|
|
||||||
%% This can only be done by the logged in user.
|
|
||||||
-spec update_user(binary(), swagger_user:swagger_user(), term()) -> ok | {error, integer()}.
|
|
||||||
update_user(Username, Body) ->
|
|
||||||
update_user(Username, Body, Body, #{}).
|
|
||||||
|
|
||||||
-spec update_user(binary(), swagger_user:swagger_user(), term(), maps:map()) -> ok | {error, integer()}.
|
|
||||||
update_user(Username, Body, Body, _Optional) ->
|
|
||||||
Method = put,
|
|
||||||
Path = ["/user/", Username, ""],
|
|
||||||
QS = [],
|
|
||||||
Headers = [],
|
|
||||||
Body1 = Body,
|
|
||||||
Opts = [],
|
|
||||||
Url = hackney_url:make_url(?BASE_URL, Path, QS),
|
|
||||||
|
|
||||||
case hackney:request(Method, Url, Headers, Body1, Opts) of
|
|
||||||
{ok, 200, _RespHeaders, _ClientRef} ->
|
|
||||||
ok;
|
|
||||||
{ok, Status, _RespHeaders, _ClientRef} ->
|
|
||||||
{error, Status}
|
|
||||||
end.
|
|
||||||
|
|
||||||
|
|
Loading…
x
Reference in New Issue
Block a user