Current section
Files
Jump to
Current section
Files
lib/nomad_client/connection.ex
# NOTE: This file is auto generated by OpenAPI Generator 6.2.0 (https://openapi-generator.tech).
# Do not edit this file manually.
defmodule NomadClient.Connection do
@moduledoc """
Handle Tesla connections for NomadClient.
Additional middleware can be set in the compile-time or runtime configuration:
config :tesla, NomadClient.Connection,
base_url: "http://127.0.0.1:4646/v1",
adapter: Tesla.Adapter.Hackney
The default base URL can also be set as:
config :nomad_client,
:base_url, "http://127.0.0.1:4646/v1"
"""
@default_base_url Application.compile_env(
:nomad_client,
:base_url,
"http://127.0.0.1:4646/v1"
)
@typedoc """
The list of options that can be passed to new/1.
- `base_url`: Overrides the base URL on a per-client basis.
- `user_agent`: Overrides the User-Agent header.
"""
@type options :: [
{:base_url, String.t()},
{:user_agent, String.t()}
]
@doc "Forward requests to Tesla."
@spec request(Tesla.Client.t(), [Tesla.option()]) :: Tesla.Env.result()
defdelegate request(client, options), to: Tesla
@doc """
Configure a client with no authentication.
### Returns
Tesla.Env.client
"""
@spec new() :: Tesla.Env.client()
def new do
Tesla.client(middleware(), adapter())
end
@doc """
Configure a client that may have authentication.
### Parameters
- `options`: a keyword list of OpenAPIPetstore.Connection.options.
### Returns
Tesla.Env.client
"""
@spec new(options) :: Tesla.Env.client()
def new(options) when is_list(options) do
options
|> middleware()
|> Tesla.client(adapter())
end
def new(url, nil) do
new(base_url: url)
end
def new(url, token) do
middleware = middleware(base_url: url)
adapter = adapter()
auth = {Tesla.Middleware.Headers, [{"X-NOMAD-TOKEN", token}]}
middleware = [auth | middleware]
Tesla.client(middleware, adapter)
end
@doc """
Returns fully configured middleware for passing to Tesla.client/2.
"""
@spec middleware(options) :: [Tesla.Client.middleware()]
def middleware(options \\ []) do
base_url =
options
|> Keyword.get(
:base_url,
Application.get_env(:nomad_client, :base_url, @default_base_url)
)
|> URI.parse()
|> maybe_fix_path()
|> URI.to_string()
tesla_options = Application.get_env(:tesla, __MODULE__, [])
middleware = Keyword.get(tesla_options, :middleware, [])
json_engine = Keyword.get(tesla_options, :json, Poison)
default_ua =
Keyword.get(tesla_options, :user_agent, "openapi-generator - NomadClient 1.1.4 - elixir")
user_agent = Keyword.get(options, :user_agent, default_ua)
[
{Tesla.Middleware.BaseUrl, base_url},
{Tesla.Middleware.Headers, [{"user-agent", user_agent}]},
{Tesla.Middleware.EncodeJson, engine: json_engine}
| middleware
]
end
@doc """
Returns the default adapter for this API.
"""
def adapter do
:tesla
|> Application.get_env(__MODULE__, [])
|> Keyword.get(:adapter, nil)
end
defp maybe_fix_path(%{path: path} = uri) when path in [nil, "/", ""] do
%{uri | path: "/v1"}
end
defp maybe_fix_path(uri), do: uri
end