Skip to content

Commit 304ba31

Browse files
committed
simplify connection module
1 parent 10fc9d0 commit 304ba31

File tree

2 files changed

+1
-152
lines changed

2 files changed

+1
-152
lines changed

modules/openapi-generator/src/main/resources/elixir/connection.ex.mustache

-102
Original file line numberDiff line numberDiff line change
@@ -93,121 +93,19 @@ defmodule {{moduleName}}.Connection do
9393

9494
### Parameters
9595

96-
{{#hasOAuthMethods}}
97-
The first parameter *may* be a `token` (a string, a token fetcher class,
98-
or a module/function tuple) or a keyword list of `options`. They are
99-
documented separately, but only *one* of them will be passed.
100-
101-
- `token`: a String or a function of arity one. This value, or the result
102-
of the function call, will be set as a bearer token in the
103-
`authorization` header.
104-
{{/hasOAuthMethods}}
10596
- `options`: a keyword list of {{moduleName}}.Connection.options.
10697

10798
### Returns
10899

109100
Tesla.Env.client
110101
"""
111-
{{#hasOAuthMethods}}
112-
@spec new(String.t() | token_fetcher | options) :: Tesla.Env.client()
113-
{{/hasOAuthMethods}}
114-
{{^hasOAuthMethods}}
115102
@spec new(options) :: Tesla.Env.client()
116-
{{/hasOAuthMethods}}
117-
{{#hasOAuthMethods}}
118-
def new(token) when is_binary(token) or is_function(token, 1) or is_tuple(token) do
119-
new(token: token)
120-
end
121-
{{/hasOAuthMethods}}
122-
123103
def new(options) when is_list(options) do
124104
options
125105
|> middleware()
126106
|> Tesla.client(adapter())
127107
end
128108

129-
{{#hasOAuthMethods}}
130-
{{#hasHttpBasicMethods}}
131-
@doc """
132-
Configure a client using bearer authentication with scopes, or with
133-
username and password for basic authentication.
134-
135-
### Parameters
136-
137-
- `token_or_username`: a String representing a bearer token or a username,
138-
depending on the type of the next parameter, or a function arity one
139-
that returns a bearer token.
140-
- `scopes_or_password`: a list of Strings represenging OAuth2 scopes, or
141-
a single string that is the password for the username provided.
142-
- `options`: a keyword list of {{moduleName}}.Connection.options.
143-
144-
### Returns
145-
146-
Tesla.Env.client
147-
"""
148-
@spec new(
149-
token_or_username :: String.t() | token_fetcher,
150-
scopes_or_password :: list(String.t()) | String.t(),
151-
options
152-
) :: Tesla.Env.client()
153-
{{/hasHttpBasicMethods}}
154-
{{^hasHttpBasicMethods}}
155-
@doc """
156-
Configure a client using bearer authentication with scopes.
157-
158-
### Parameters
159-
160-
- `token`: a String or a function of arity one. This value, or the result
161-
of the function call, will be set as a bearer token in the
162-
`authorization` header.
163-
- `scopes`: a list of Strings represenging OAuth2 scopes.
164-
- `options`: a keyword list of {{moduleName}}.Connection.options.
165-
166-
### Returns
167-
168-
Tesla.Env.client
169-
"""
170-
@spec new(String.t() | token_fetcher, list(String.t()), options) :: Tesla.Env.client()
171-
{{/hasHttpBasicMethods}}
172-
{{/hasOAuthMethods}}
173-
{{^hasOAuthMethods}}
174-
{{#hasHttpBasicMethods}}
175-
@doc """
176-
Configure a client using username and password for basic authentication.
177-
178-
### Parameters
179-
180-
- `username`: a String representing a username.
181-
- `password`: a String representing a password.
182-
- `options`: a keyword list of {{moduleName}}.Connection.options.
183-
184-
### Returns
185-
186-
Tesla.Env.client
187-
"""
188-
@spec new(String.t(), String.t()), options) :: Tesla.Env.client()
189-
{{/hasHttpBasicMethods}}
190-
{{/hasOAuthMethods}}
191-
192-
{{#hasOAuthMethods}}
193-
def new(token_or_username, scopes_or_password, options \\ [])
194-
195-
def new(token, scopes, options)
196-
when (is_binary(token) or is_function(token, 1) or is_tuple(token)) and is_list(scopes) do
197-
options
198-
|> Keyword.merge(token: token, token_scopes: scopes)
199-
|> new()
200-
end
201-
{{/hasOAuthMethods}}
202-
203-
{{#hasHttpBasicMethods}}
204-
def new(username, password, options) when is_binary(username) and is_binary(password) do
205-
options
206-
|> Keyword.merge(username: username, password: password)
207-
|> new()
208-
end
209-
{{/hasHttpBasicMethods}}
210-
211109
@doc """
212110
Returns fully configured middleware for passing to Tesla.client/2.
213111
"""

samples/client/petstore/elixir/lib/openapi_petstore/connection.ex

+1-50
Original file line numberDiff line numberDiff line change
@@ -79,68 +79,19 @@ defmodule OpenapiPetstore.Connection do
7979
8080
### Parameters
8181
82-
The first parameter *may* be a `token` (a string, a token fetcher class,
83-
or a module/function tuple) or a keyword list of `options`. They are
84-
documented separately, but only *one* of them will be passed.
85-
86-
- `token`: a String or a function of arity one. This value, or the result
87-
of the function call, will be set as a bearer token in the
88-
`authorization` header.
8982
- `options`: a keyword list of OpenapiPetstore.Connection.options.
9083
9184
### Returns
9285
9386
Tesla.Env.client
9487
"""
95-
@spec new(String.t() | token_fetcher | options) :: Tesla.Env.client()
96-
def new(token) when is_binary(token) or is_function(token, 1) or is_tuple(token) do
97-
new(token: token)
98-
end
99-
88+
@spec new(options) :: Tesla.Env.client()
10089
def new(options) when is_list(options) do
10190
options
10291
|> middleware()
10392
|> Tesla.client(adapter())
10493
end
10594

106-
@doc """
107-
Configure a client using bearer authentication with scopes, or with
108-
username and password for basic authentication.
109-
110-
### Parameters
111-
112-
- `token_or_username`: a String representing a bearer token or a username,
113-
depending on the type of the next parameter, or a function arity one
114-
that returns a bearer token.
115-
- `scopes_or_password`: a list of Strings represenging OAuth2 scopes, or
116-
a single string that is the password for the username provided.
117-
- `options`: a keyword list of OpenapiPetstore.Connection.options.
118-
119-
### Returns
120-
121-
Tesla.Env.client
122-
"""
123-
@spec new(
124-
token_or_username :: String.t() | token_fetcher,
125-
scopes_or_password :: list(String.t()) | String.t(),
126-
options
127-
) :: Tesla.Env.client()
128-
129-
def new(token_or_username, scopes_or_password, options \\ [])
130-
131-
def new(token, scopes, options)
132-
when (is_binary(token) or is_function(token, 1) or is_tuple(token)) and is_list(scopes) do
133-
options
134-
|> Keyword.merge(token: token, token_scopes: scopes)
135-
|> new()
136-
end
137-
138-
def new(username, password, options) when is_binary(username) and is_binary(password) do
139-
options
140-
|> Keyword.merge(username: username, password: password)
141-
|> new()
142-
end
143-
14495
@doc """
14596
Returns fully configured middleware for passing to Tesla.client/2.
14697
"""

0 commit comments

Comments
 (0)