Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Further Elixir Client Improvements #12775

Merged
merged 6 commits into from Sep 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -2,33 +2,46 @@

{{appDescription}}

### Building
## Building

To install the required dependencies and to build the elixir project, run:
```

```console
mix local.hex --force
mix do deps.get, compile
```

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `{{#underscored}}{{packageName}}{{/underscored}}` to your list of dependencies in `mix.exs`:
If [available in Hex][], the package can be installed by adding `{{#underscored}}{{packageName}}{{/underscored}}` to
your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:{{#underscored}}{{packageName}}{{/underscored}}, "~> 0.1.0"}]
end
{{=<% %>=}}def deps do
[{<%#atom%><%#underscored%><%packageName%><%/underscored%><%/atom%>, "~> <%appVersion%>"}]
end<%={{ }}=%>
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/{{#underscored}}{{packageName}}{{/underscored}}](https://hexdocs.pm/{{#underscored}}{{packageName}}{{/underscored}}).

Documentation can be generated with [ExDoc][] and published on [HexDocs][]. Once published, the docs can be found at
[https://hexdocs.pm/{{#underscored}}{{packageName}}{{/underscored}}][docs].

## Configuration

You can override the URL of your server (e.g. if you have a separate development and production server in your configuration files.
You can override the URL of your server (e.g. if you have a separate development and production server in your
configuration files).

```elixir
config :{{#underscored}}{{packageName}}{{/underscored}}, base_url: "{{{basePath}}}"
```

halostatue marked this conversation as resolved.
Show resolved Hide resolved
Multiple clients for the same API with different URLs can be created passing different `base_url`s when calling
`{{moduleName}}.Connection.new/1`:

```elixir
config :{{#underscored}}{{appName}}{{/underscored}}, base_url: "{{{basePath}}}"
```
client = {{moduleName}}.Connection.new(base_url: "{{{basePath}}}")
```

[exdoc]: https://github.com/elixir-lang/ex_doc
[hexdocs]: https://hexdocs.pm
[available in hex]: https://hex.pm/docs/publish
[docs]: https://hexdocs.pm/{{#underscored}}{{packageName}}{{/underscored}}
44 changes: 24 additions & 20 deletions modules/openapi-generator/src/main/resources/elixir/api.mustache
Expand Up @@ -6,9 +6,8 @@ defmodule {{moduleName}}.Api.{{classname}} do

alias {{moduleName}}.Connection
import {{moduleName}}.RequestBuilder

{{#operations}}
{{#operation}}
{{#operation}}

halostatue marked this conversation as resolved.
Show resolved Hide resolved
@doc """
{{#summary}}
Expand All @@ -18,25 +17,26 @@ defmodule {{moduleName}}.Api.{{classname}} do
{{&notes}}
{{/notes}}

## Parameters
### Parameters

- connection ({{&moduleName}}.Connection): Connection to server
- `connection` ({{&moduleName}}.Connection): Connection to server
{{#requiredParams}}
- {{#underscored}}{{&paramName}}{{/underscored}} ({{&dataType}}): {{&description}}
- `{{#underscored}}{{&paramName}}{{/underscored}}` ({{&dataType}}): {{&description}}
{{/requiredParams}}
- opts (KeywordList): [optional] Optional parameters
- `opts` (keyword): Optional parameters
{{#optionalParams}}
{{#isBodyParam}}
- :body ({{dataType}}): {{&description}}
- `:body` ({{dataType}}): {{&description}}
{{/isBodyParam}}
{{^isBodyParam}}
- {{#underscored}}:{{&paramName}}{{/underscored}} ({{&dataType}}): {{&description}}
- `{{#atom}}{{#underscored}}{{&paramName}}{{/underscored}}{{/atom}}` ({{&dataType}}): {{&description}}
{{/isBodyParam}}
{{/optionalParams}}
## Returns

{:ok, {{#isArray}}[%{{&returnBaseType}}{}, ...]{{/isArray}}{{#isMap}}%{}{{/isMap}}{{^returnType}}nil{{/returnType}}{{#returnSimpleType}}{{#returnType}}{{{.}}}{{/returnType}}{{/returnSimpleType}}} on success
{:error, Tesla.Env.t} on failure
### Returns

- `{:ok, {{#isArray}}[%{{&returnBaseType}}{}, ...]{{/isArray}}{{#isMap}}%{}{{/isMap}}{{^returnType}}nil{{/returnType}}{{#returnSimpleType}}{{#returnType}}{{{.}}}{{/returnType}}{{/returnSimpleType}}}` on success
- `{:error, Tesla.Env.t}` on failure
"""
{{{typespec}}}
def {{{operationId}}}(connection, {{#requiredParams}}{{#underscored}}{{{paramName}}}{{/underscored}}, {{/requiredParams}}{{^hasOptionalParams}}_{{/hasOptionalParams}}opts \\ []) do
Expand All @@ -52,29 +52,33 @@ defmodule {{moduleName}}.Api.{{classname}} do
{{/isBodyParam}}
{{#-last}}
}

{{/-last}}
{{/optionalParams}}
%{}
|> method(:{{#underscored}}{{httpMethod}}{{/underscored}})
|> url("{{replacedPathName}}")
request =
%{}
|> method({{#atom}}{{#underscored}}{{httpMethod}}{{/underscored}}{{/atom}})
|> url("{{replacedPathName}}")
{{#requiredParams}}
{{^isPathParam}}
|> add_param({{#isBodyParam}}:body{{/isBodyParam}}{{#isFormParam}}{{#isMultipart}}{{#isFile}}:file{{/isFile}}{{^isFile}}:form{{/isFile}}{{/isMultipart}}{{^isMultipart}}:form{{/isMultipart}}{{/isFormParam}}{{#isQueryParam}}:query{{/isQueryParam}}{{#isHeaderParam}}:headers{{/isHeaderParam}}, {{#isBodyParam}}:body, {{/isBodyParam}}{{^isBodyParam}}{{#atom}}{{baseName}}{{/atom}}, {{/isBodyParam}}{{#underscored}}{{paramName}}{{/underscored}})
|> add_param({{#isBodyParam}}:body{{/isBodyParam}}{{#isFormParam}}{{#isMultipart}}{{#isFile}}:file{{/isFile}}{{^isFile}}:form{{/isFile}}{{/isMultipart}}{{^isMultipart}}:form{{/isMultipart}}{{/isFormParam}}{{#isQueryParam}}:query{{/isQueryParam}}{{#isHeaderParam}}:headers{{/isHeaderParam}}, {{#isBodyParam}}:body{{/isBodyParam}}{{^isBodyParam}}{{#atom}}{{baseName}}{{/atom}}{{/isBodyParam}}, {{#underscored}}{{paramName}}{{/underscored}})
{{/isPathParam}}
{{/requiredParams}}
{{#optionalParams}}
{{#-first}}
|> add_optional_params(optional_params, opts)
|> add_optional_params(optional_params, opts)
{{/-first}}
{{/optionalParams}}
{{#requiresHttpcWorkaround}}
|> ensure_body()
|> ensure_body()
{{/requiresHttpcWorkaround}}
|> Enum.into([])
|> (&Connection.request(connection, &1)).()
|> Enum.into([])

connection
|> Connection.request(request)
|> evaluate_response({{#responses}}{{#-first}}[
{{/-first}}
{ {{& codeMappingKey}}, {{decodedStruct}}}{{^-last}},{{/-last}}
{{=<% %>=}}{<%& codeMappingKey%>, <%decodedStruct%>}<%={{ }}=%>{{^-last}},{{/-last}}
{{#-last}} ]{{/-last}}{{/responses}})
end
{{/operation}}
Expand Down
Expand Up @@ -7,7 +7,7 @@
# General application configuration
import Config

config :{{#underscored}}{{appName}}{{/underscored}}, base_url: "{{{basePath}}}"
config {{#atom}}{{#underscored}}{{packageName}}{{/underscored}}{{/atom}}, base_url: "{{{basePath}}}"

# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
Expand Down