simplify connection module (#21158)

This commit is contained in:
Enrique Fernández 2025-05-04 16:51:35 +02:00 committed by GitHub
parent f3af25b10c
commit 0389a99cec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 7 additions and 182 deletions

View File

@ -77,137 +77,23 @@ defmodule {{moduleName}}.Connection do
defdelegate request(client, options), to: Tesla defdelegate request(client, options), to: Tesla
@doc """ @doc """
Configure a client with no authentication. Configure a {{moduleName}} client.
### 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 ### Parameters
{{#hasOAuthMethods}} - `options`: an optional keyword list of {{moduleName}}.Connection.options.
The first parameter *may* be a `token` (a string, a token fetcher class,
or a module/function tuple) or a keyword list of `options`. They are
documented separately, but only *one* of them will be passed.
- `token`: a String or a function of arity one. This value, or the result
of the function call, will be set as a bearer token in the
`authorization` header.
{{/hasOAuthMethods}}
- `options`: a keyword list of {{moduleName}}.Connection.options.
### Returns ### Returns
Tesla.Env.client Tesla.Env.client
""" """
{{#hasOAuthMethods}}
@spec new(String.t() | token_fetcher | options) :: Tesla.Env.client()
{{/hasOAuthMethods}}
{{^hasOAuthMethods}}
@spec new(options) :: Tesla.Env.client() @spec new(options) :: Tesla.Env.client()
{{/hasOAuthMethods}} def new(options \\ []) do
{{#hasOAuthMethods}}
def new(token) when is_binary(token) or is_function(token, 1) or is_tuple(token) do
new(token: token)
end
{{/hasOAuthMethods}}
def new(options) when is_list(options) do
options options
|> middleware() |> middleware()
|> Tesla.client(adapter()) |> Tesla.client(adapter())
end end
{{#hasOAuthMethods}}
{{#hasHttpBasicMethods}}
@doc """
Configure a client using bearer authentication with scopes, or with
username and password for basic authentication.
### Parameters
- `token_or_username`: a String representing a bearer token or a username,
depending on the type of the next parameter, or a function arity one
that returns a bearer token.
- `scopes_or_password`: a list of Strings represenging OAuth2 scopes, or
a single string that is the password for the username provided.
- `options`: a keyword list of {{moduleName}}.Connection.options.
### Returns
Tesla.Env.client
"""
@spec new(
token_or_username :: String.t() | token_fetcher,
scopes_or_password :: list(String.t()) | String.t(),
options
) :: Tesla.Env.client()
{{/hasHttpBasicMethods}}
{{^hasHttpBasicMethods}}
@doc """
Configure a client using bearer authentication with scopes.
### Parameters
- `token`: a String or a function of arity one. This value, or the result
of the function call, will be set as a bearer token in the
`authorization` header.
- `scopes`: a list of Strings represenging OAuth2 scopes.
- `options`: a keyword list of {{moduleName}}.Connection.options.
### Returns
Tesla.Env.client
"""
@spec new(String.t() | token_fetcher, list(String.t()), options) :: Tesla.Env.client()
{{/hasHttpBasicMethods}}
{{/hasOAuthMethods}}
{{^hasOAuthMethods}}
{{#hasHttpBasicMethods}}
@doc """
Configure a client using username and password for basic authentication.
### Parameters
- `username`: a String representing a username.
- `password`: a String representing a password.
- `options`: a keyword list of {{moduleName}}.Connection.options.
### Returns
Tesla.Env.client
"""
@spec new(String.t(), String.t()), options) :: Tesla.Env.client()
{{/hasHttpBasicMethods}}
{{/hasOAuthMethods}}
{{#hasOAuthMethods}}
def new(token_or_username, scopes_or_password, options \\ [])
def new(token, scopes, options)
when (is_binary(token) or is_function(token, 1) or is_tuple(token)) and is_list(scopes) do
options
|> Keyword.merge(token: token, token_scopes: scopes)
|> new()
end
{{/hasOAuthMethods}}
{{#hasHttpBasicMethods}}
def new(username, password, options) when is_binary(username) and is_binary(password) do
options
|> Keyword.merge(username: username, password: password)
|> new()
end
{{/hasHttpBasicMethods}}
@doc """ @doc """
Returns fully configured middleware for passing to Tesla.client/2. Returns fully configured middleware for passing to Tesla.client/2.
""" """

View File

@ -63,84 +63,23 @@ defmodule OpenapiPetstore.Connection do
defdelegate request(client, options), to: Tesla defdelegate request(client, options), to: Tesla
@doc """ @doc """
Configure a client with no authentication. Configure a OpenapiPetstore client.
### 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 ### Parameters
The first parameter *may* be a `token` (a string, a token fetcher class, - `options`: an optional keyword list of OpenapiPetstore.Connection.options.
or a module/function tuple) or a keyword list of `options`. They are
documented separately, but only *one* of them will be passed.
- `token`: a String or a function of arity one. This value, or the result
of the function call, will be set as a bearer token in the
`authorization` header.
- `options`: a keyword list of OpenapiPetstore.Connection.options.
### Returns ### Returns
Tesla.Env.client Tesla.Env.client
""" """
@spec new(String.t() | token_fetcher | options) :: Tesla.Env.client() @spec new(options) :: Tesla.Env.client()
def new(token) when is_binary(token) or is_function(token, 1) or is_tuple(token) do def new(options \\ []) do
new(token: token)
end
def new(options) when is_list(options) do
options options
|> middleware() |> middleware()
|> Tesla.client(adapter()) |> Tesla.client(adapter())
end end
@doc """
Configure a client using bearer authentication with scopes, or with
username and password for basic authentication.
### Parameters
- `token_or_username`: a String representing a bearer token or a username,
depending on the type of the next parameter, or a function arity one
that returns a bearer token.
- `scopes_or_password`: a list of Strings represenging OAuth2 scopes, or
a single string that is the password for the username provided.
- `options`: a keyword list of OpenapiPetstore.Connection.options.
### Returns
Tesla.Env.client
"""
@spec new(
token_or_username :: String.t() | token_fetcher,
scopes_or_password :: list(String.t()) | String.t(),
options
) :: Tesla.Env.client()
def new(token_or_username, scopes_or_password, options \\ [])
def new(token, scopes, options)
when (is_binary(token) or is_function(token, 1) or is_tuple(token)) and is_list(scopes) do
options
|> Keyword.merge(token: token, token_scopes: scopes)
|> new()
end
def new(username, password, options) when is_binary(username) and is_binary(password) do
options
|> Keyword.merge(username: username, password: password)
|> new()
end
@doc """ @doc """
Returns fully configured middleware for passing to Tesla.client/2. Returns fully configured middleware for passing to Tesla.client/2.
""" """