|
35 | 35 | -dialyzer([{no_missing_return, [start/2]}]).
|
36 | 36 |
|
37 | 37 | %% catch X is used as a way to fire and forget, ignoring errors if there are any
|
38 |
| --elvis([{elvis_style, no_catch_expressions, disable}]). |
| 38 | +-elvis([{elvis_style, no_catch_expressions, disable}, |
| 39 | + {elvis_style, max_function_arity, #{max_arity => 9}}]). |
39 | 40 |
|
40 | 41 | -behaviour(application).
|
41 | 42 |
|
|
60 | 61 | request/9, send_body_part/2, send_body_part/3, send_trailers/2, send_trailers/3,
|
61 | 62 | get_body_part/1, get_body_part/2]).
|
62 | 63 |
|
63 |
| --include("lhttpc_types.hrl"). |
64 |
| - |
| 64 | +-type header() :: {string() | atom(), string()}. |
| 65 | +-type headers() :: [header()]. |
| 66 | +-type option() :: |
| 67 | + {connect_options, list()} | |
| 68 | + {connect_timeout, timeout()} | |
| 69 | + {connection_timeout, non_neg_integer() | infinity} | |
| 70 | + {max_connections, non_neg_integer()} | |
| 71 | + {send_retry, non_neg_integer()} | |
| 72 | + {stream_to, pid()} | |
| 73 | + {partial_upload, non_neg_integer() | infinity} | |
| 74 | + {partial_download, pid(), non_neg_integer() | infinity}. |
| 75 | +-type options() :: [option()]. |
| 76 | +-type window_size() :: non_neg_integer() | infinity. |
65 | 77 | -type result() ::
|
66 | 78 | {ok, {{pos_integer(), string()}, headers(), binary()}} | {error, atom()}.
|
67 | 79 |
|
| 80 | +-export_type([headers/0, options/0, result/0, window_size/0]). |
| 81 | + |
68 | 82 | %% @hidden
|
69 | 83 | -spec start(any(), any()) -> {ok, pid()}.
|
70 | 84 | start(_, Opts) when is_list(Opts) ->
|
@@ -190,25 +204,25 @@ request(URL, Method, Hdrs, Body, Timeout) ->
|
190 | 204 | %% Reason = connection_closed | connect_timeout | timeout
|
191 | 205 | %% @doc Sends a request with a body.
|
192 | 206 | %% Would be the same as calling <pre>
|
193 |
| -%% {Host, Port, Path, Ssl} = lhttpc_lib:parse_url(URL), |
194 |
| -%% request(Host, Port, Path, Ssl, Method, Hdrs, Body, Timeout, Options). |
| 207 | +%% {Host, Port, Path, SSL} = lhttpc_lib:parse_url(URL), |
| 208 | +%% request(Host, Port, Path, SSL, Method, Hdrs, Body, Timeout, Options). |
195 | 209 | %% </pre>
|
196 | 210 | %%
|
197 | 211 | %% `URL' is expected to be a valid URL:
|
198 | 212 | %% `scheme://host[:port][/path]'.
|
199 | 213 | %% @end
|
200 | 214 | %% @see request/9
|
201 |
| --spec request(string(), string() | atom(), headers(), iolist(), timeout(), [option()]) -> |
| 215 | +-spec request(string(), string() | atom(), headers(), iolist(), timeout(), options()) -> |
202 | 216 | result().
|
203 | 217 | request(URL, Method, Hdrs, Body, Timeout, Options) ->
|
204 |
| - {Host, Port, Path, Ssl} = lhttpc_lib:parse_url(URL), |
205 |
| - request(Host, Port, Ssl, Path, Method, Hdrs, Body, Timeout, Options). |
| 218 | + {Host, Port, Path, SSL} = lhttpc_lib:parse_url(URL), |
| 219 | + request(Host, Port, SSL, Path, Method, Hdrs, Body, Timeout, Options). |
206 | 220 |
|
207 |
| -%% @spec (Host, Port, Ssl, Path, Method, Hdrs, RequestBody, Timeout, Options) -> |
| 221 | +%% @spec (Host, Port, SSL, Path, Method, Hdrs, RequestBody, Timeout, Options) -> |
208 | 222 | %% Result
|
209 | 223 | %% Host = string()
|
210 | 224 | %% Port = integer()
|
211 |
| -%% Ssl = boolean() |
| 225 | +%% SSL = boolean() |
212 | 226 | %% Path = string()
|
213 | 227 | %% Method = string() | atom()
|
214 | 228 | %% Hdrs = [{Header, Value}]
|
@@ -243,7 +257,7 @@ request(URL, Method, Hdrs, Body, Timeout, Options) ->
|
243 | 257 | %% use the following:<br/>
|
244 | 258 | %% `Host' = `"example.com"'<br/>
|
245 | 259 | %% `Port' = `80'<br/>
|
246 |
| -%% `Ssl' = `false'<br/> |
| 260 | +%% `SSL' = `false'<br/> |
247 | 261 | %% `Path' = `"/foobar"'<br/>
|
248 | 262 | %% `Path' must begin with a forward slash `/'.
|
249 | 263 | %%
|
@@ -346,25 +360,33 @@ request(URL, Method, Hdrs, Body, Timeout, Options) ->
|
346 | 360 | headers(),
|
347 | 361 | iolist(),
|
348 | 362 | timeout(),
|
349 |
| - [option()]) -> |
| 363 | + options()) -> |
350 | 364 | result().
|
351 |
| -request(Host, Port, Ssl, Path, Method, Hdrs, Body, Timeout, Options) -> |
| 365 | +request(Host, Port, SSL, Path, Method, Hdrs, Body, Timeout, Options) -> |
352 | 366 | ok = verify_options(Options),
|
353 | 367 | ReqId = make_ref(),
|
354 |
| - BaseArgs = [Host, Port, Ssl, Path, Method, Hdrs, Body, Options], |
| 368 | + BaseArg = |
| 369 | + #{host => Host, |
| 370 | + port => Port, |
| 371 | + ssl => SSL, |
| 372 | + path => Path, |
| 373 | + method => Method, |
| 374 | + headers => Hdrs, |
| 375 | + body => Body, |
| 376 | + options => Options}, |
355 | 377 | case proplists:is_defined(stream_to, Options) of
|
356 | 378 | true ->
|
357 | 379 | StreamTo = proplists:get_value(stream_to, Options),
|
358 |
| - Args = [ReqId, StreamTo | BaseArgs], |
359 |
| - Pid = spawn(lhttpc_client, request, Args), |
| 380 | + Arg = BaseArg#{id => ReqId, from => StreamTo}, |
| 381 | + Pid = spawn(lhttpc_client, request, [Arg]), |
360 | 382 | spawn(fun() ->
|
361 | 383 | R = kill_client_after(Pid, Timeout),
|
362 | 384 | StreamTo ! {response, ReqId, Pid, R}
|
363 | 385 | end),
|
364 | 386 | {ReqId, Pid};
|
365 | 387 | false ->
|
366 |
| - Args = [ReqId, self() | BaseArgs], |
367 |
| - Pid = spawn_link(lhttpc_client, request, Args), |
| 388 | + Arg = BaseArg#{id => ReqId, from => self()}, |
| 389 | + Pid = spawn_link(lhttpc_client, request, [Arg]), |
368 | 390 | receive
|
369 | 391 | {response, ReqId, Pid, R} ->
|
370 | 392 | R;
|
|
0 commit comments