Skip to content

Commit e363748

Browse files
committed
simplify connection module
1 parent 4b805ff commit e363748

File tree

2 files changed

+7
-182
lines changed

2 files changed

+7
-182
lines changed

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

+3-117
Original file line numberDiff line numberDiff line change
@@ -77,137 +77,23 @@ defmodule {{moduleName}}.Connection do
7777
defdelegate request(client, options), to: Tesla
7878

7979
@doc """
80-
Configure a client with no authentication.
81-
82-
### Returns
83-
84-
Tesla.Env.client
85-
"""
86-
@spec new() :: Tesla.Env.client()
87-
def new do
88-
Tesla.client(middleware(), adapter())
89-
end
90-
91-
@doc """
92-
Configure a client that may have authentication.
80+
Configure a {{moduleName}} client.
9381

9482
### Parameters
9583

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}}
105-
- `options`: a keyword list of {{moduleName}}.Connection.options.
84+
- `options`: an optional keyword list of {{moduleName}}.Connection.options.
10685

10786
### Returns
10887

10988
Tesla.Env.client
11089
"""
111-
{{#hasOAuthMethods}}
112-
@spec new(String.t() | token_fetcher | options) :: Tesla.Env.client()
113-
{{/hasOAuthMethods}}
114-
{{^hasOAuthMethods}}
11590
@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-
123-
def new(options) when is_list(options) do
91+
def new(options \\ []) do
12492
options
12593
|> middleware()
12694
|> Tesla.client(adapter())
12795
end
12896

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-
21197
@doc """
21298
Returns fully configured middleware for passing to Tesla.client/2.
21399
"""

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

+4-65
Original file line numberDiff line numberDiff line change
@@ -63,84 +63,23 @@ defmodule OpenapiPetstore.Connection do
6363
defdelegate request(client, options), to: Tesla
6464

6565
@doc """
66-
Configure a client with no authentication.
67-
68-
### Returns
69-
70-
Tesla.Env.client
71-
"""
72-
@spec new() :: Tesla.Env.client()
73-
def new do
74-
Tesla.client(middleware(), adapter())
75-
end
76-
77-
@doc """
78-
Configure a client that may have authentication.
66+
Configure a OpenapiPetstore client.
7967
8068
### Parameters
8169
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.
89-
- `options`: a keyword list of OpenapiPetstore.Connection.options.
70+
- `options`: an optional keyword list of OpenapiPetstore.Connection.options.
9071
9172
### Returns
9273
9374
Tesla.Env.client
9475
"""
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-
100-
def new(options) when is_list(options) do
76+
@spec new(options) :: Tesla.Env.client()
77+
def new(options \\ []) do
10178
options
10279
|> middleware()
10380
|> Tesla.client(adapter())
10481
end
10582

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-
14483
@doc """
14584
Returns fully configured middleware for passing to Tesla.client/2.
14685
"""

0 commit comments

Comments
 (0)