[Elixir] Update Tesla dependency to version 1.0 (#2326)

* Update Tesla dependency and replace Poison with Jason

* Use new Tesla method to set headers

* Fix jason dependency definition

* Use list for Headers instead of a map

* Rollback to Poison because Jason does not support 'as:' option to decode to arbitrary struct

* Use new return signature from Tesla 1.0 in decode function

* catch error when a struct is given as second parameter to RequestBuilder.decode

* Update modules/openapi-generator/src/main/resources/elixir/request_builder.ex.mustache

Co-Authored-By: yknx4 <yknx.4.b@gmail.com>

* Update modules/openapi-generator/src/main/resources/elixir/request_builder.ex.mustache

Co-Authored-By: yknx4 <yknx.4.b@gmail.com>
This commit is contained in:
Ale Figueroa 2019-03-19 21:28:19 -05:00 committed by William Cheng
parent 3320d3cbe1
commit bf7838cd81
6 changed files with 28 additions and 22 deletions

View File

@ -52,7 +52,7 @@ public class ElixirClientCodegen extends DefaultCodegen implements CodegenConfig
String supportedElixirVersion = "1.4";
List<String> extraApplications = Arrays.asList(":logger");
List<String> deps = Arrays.asList(
"{:tesla, \"~> 0.8\"}",
"{:tesla, \"~> 1.0.0\"}",
"{:poison, \">= 1.0.0\"}"
);

View File

@ -8,8 +8,8 @@ defmodule {{moduleName}}.Connection do
# Add any middleware here (authentication)
plug Tesla.Middleware.BaseUrl, "{{{basePath}}}"
plug Tesla.Middleware.Headers, %{"User-Agent" => "Elixir"}
plug Tesla.Middleware.EncodeJson
plug Tesla.Middleware.Headers, [{"user-agent", "Elixir"}]
plug Tesla.Middleware.EncodeJson, engine: Poison
{{#hasAuthMethods}}
{{#authMethods}}
@ -34,7 +34,7 @@ defmodule {{moduleName}}.Connection do
@spec new(String.t) :: Tesla.Env.client
def new(token) when is_binary(token) do
Tesla.build_client([
{Tesla.Middleware.Headers, %{"Authorization" => "Bearer #{token}"}}
{Tesla.Middleware.Headers, [{"authorization", "Bearer #{token}"}]}
])
end

View File

@ -87,7 +87,7 @@ defmodule {{moduleName}}.RequestBuilder do
end
def add_param(request, :headers, key, value) do
request
|> Map.update(:headers, %{key => value}, &(Map.put(&1, key, value)))
|> Tesla.put_header(key, value)
end
def add_param(request, :file, name, path) do
request
@ -107,7 +107,7 @@ defmodule {{moduleName}}.RequestBuilder do
## Parameters
- arg1 (Tesla.Env.t | term) - The response object
- arg1 ({:ok, Tesla.Env.t} | term) - The response object
- arg2 (:false | struct | [struct]) - The shape of the struct to deserialize into
## Returns
@ -115,12 +115,16 @@ defmodule {{moduleName}}.RequestBuilder do
{:ok, struct} on success
{:error, term} on failure
"""
@spec decode(Tesla.Env.t | term()) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode(%Tesla.Env{status: 200, body: body}), do: Poison.decode(body)
@spec decode({:ok, Tesla.Env.t} | term()) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode({:ok, %Tesla.Env{status: 200, body: body}}), do: Poison.decode(body)
def decode(response), do: {:error, response}
def decode({:error, _} = error), do: error
def decode(response), do: {:error, response}
@spec decode(Tesla.Env.t | term(), :false | struct() | [struct()]) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode(%Tesla.Env{status: 200} = env, false), do: {:ok, env}
def decode(%Tesla.Env{status: 200, body: body}, struct), do: Poison.decode(body, as: struct)
@spec decode({:ok, Tesla.Env.t} | term(), :false | struct() | [struct()]) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode({:ok, %Tesla.Env{status: 200}} = env, false), do: {:ok, env}
def decode({:ok, %Tesla.Env{status: 200, body: body}}, struct), do: Poison.decode(body, as: struct)
def decode({:ok, %Tesla.Env{} = response}, _struct), do: {:error, response}
def decode({:error, _} = error, _struct), do: error
def decode(response, _struct), do: {:error, response}
end

View File

@ -11,8 +11,8 @@ defmodule OpenapiPetstore.Connection do
# Add any middleware here (authentication)
plug Tesla.Middleware.BaseUrl, "http://petstore.swagger.io:80/v2"
plug Tesla.Middleware.Headers, %{"User-Agent" => "Elixir"}
plug Tesla.Middleware.EncodeJson
plug Tesla.Middleware.Headers, [{"user-agent", "Elixir"}]
plug Tesla.Middleware.EncodeJson, engine: Poison
@doc """
Configure a client connection using Basic authentication.
@ -51,7 +51,7 @@ defmodule OpenapiPetstore.Connection do
@spec new(String.t) :: Tesla.Env.client
def new(token) when is_binary(token) do
Tesla.build_client([
{Tesla.Middleware.Headers, %{"Authorization" => "Bearer #{token}"}}
{Tesla.Middleware.Headers, [{"authorization", "Bearer #{token}"}]}
])
end

View File

@ -90,7 +90,7 @@ defmodule OpenapiPetstore.RequestBuilder do
end
def add_param(request, :headers, key, value) do
request
|> Map.update(:headers, %{key => value}, &(Map.put(&1, key, value)))
|> Tesla.put_header(key, value)
end
def add_param(request, :file, name, path) do
request
@ -110,7 +110,7 @@ defmodule OpenapiPetstore.RequestBuilder do
## Parameters
- arg1 (Tesla.Env.t | term) - The response object
- arg1 ({:ok, Tesla.Env.t} | term) - The response object
- arg2 (:false | struct | [struct]) - The shape of the struct to deserialize into
## Returns
@ -118,12 +118,14 @@ defmodule OpenapiPetstore.RequestBuilder do
{:ok, struct} on success
{:error, term} on failure
"""
@spec decode(Tesla.Env.t | term()) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode(%Tesla.Env{status: 200, body: body}), do: Poison.decode(body)
@spec decode({:ok, Tesla.Env.t} | term()) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode({:ok, %Tesla.Env{status: 200, body: body}}), do: Poison.decode(body)
def decode(response), do: {:error, response}
def decode({:error, _} = error), do: error
@spec decode(Tesla.Env.t | term(), :false | struct() | [struct()]) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode(%Tesla.Env{status: 200} = env, false), do: {:ok, env}
def decode(%Tesla.Env{status: 200, body: body}, struct), do: Poison.decode(body, as: struct)
@spec decode({:ok, Tesla.Env.t} | term(), :false | struct() | [struct()]) :: {:ok, struct()} | {:error, Tesla.Env.t} | {:error, term()}
def decode({:ok, %Tesla.Env{status: 200}} = env, false), do: {:ok, env}
def decode({:ok, %Tesla.Env{status: 200, body: body}}, struct), do: Poison.decode(body, as: struct)
def decode({:error, _} = error, _struct), do: error
def decode(response, _struct), do: {:error, response}
end

View File

@ -29,7 +29,7 @@ defmodule OpenapiPetstore.Mixfile do
# Type "mix help deps" for more examples and options
defp deps do
[
{:tesla, "~> 0.8"},
{:tesla, "~> 1.0.0"},
{:poison, ">= 1.0.0"}
]
end