Commits

Anonymous committed aff55e7

Add support for alternative request API without URLs.

  • Participants
  • Parent commits 334a1ae

Comments (0)

Files changed (2)

 -module(lhttpc).
 -behaviour(application).
 
--export([request/4, request/5, request/6]).
+-export([request/4, request/5, request/6, request/9]).
 -export([start/2, stop/1]).
 
 -include("lhttpc_types.hrl").
 %%   ResponseBody = binary()
 %%   Reason = connection_closed | connect_timeout | timeout
 %% @doc Sends a request with a body.
+%% Would be the same as calling <pre>
+%% {Host, Port, Path, Ssl} = lhttpc_lib:parse_url(URL),
+%% request(Host, Port, Path, Ssl, Method, Hdrs, Body, Timeout, Options).
+%% </pre>
+%%
 %% `URL' is expected to be a valid URL: 
 %% `scheme://host[:port][/path]'.
+%% @end
+-spec request(string(), string() | atom(), headers(), iolist(),
+        pos_integer() | infinity, [option()]) -> result().
+request(URL, Method, Hdrs, Body, Timeout, Options) ->
+    {Host, Port, Path, Ssl} = lhttpc_lib:parse_url(URL),
+    request(Host, Port, Path, Ssl, Method, Hdrs, Body, Timeout, Options).
+
+%% @spec (Host, Port, Ssl, Path, Method, Hdrs, RequestBody, Timeout, Options) ->
+%%                                                                        Result
+%%   Host = string()
+%%   Port = integer()
+%%   Ssl = boolean()
+%%   Path = string()
+%%   Method = string() | atom()
+%%   Hdrs = [{Header, Value}]
+%%   Header = string() | binary() | atom()
+%%   Value = string() | binary()
+%%   RequestBody = iolist()
+%%   Timeout = integer() | infinity
+%%   Options = [Option]
+%%   Option = {connect_timeout, Milliseconds | infinity} |
+%%            {send_retry, integer()}
+%%   Milliseconds = integer()
+%%   Result = {ok, {{StatusCode, ReasonPhrase}, Hdrs, ResponseBody}}
+%%            | {error, Reason}
+%%   StatusCode = integer()
+%%   ReasonPhrase = string()
+%%   ResponseBody = binary()
+%%   Reason = connection_closed | connect_timeout | timeout
+%% @doc Sends a request with a body.
+%%
+%% Instead of building and parsing URLs the target server is specified with
+%% a host, port, weither SSL should be used or not and a path on the server.
+%% For instance, if you want to request http://example.com/foobar you would
+%% use the following:<br/>
+%% `Host' = `"example.com"'<br/>
+%% `Port' = `80'<br/>
+%% `Ssl' = `false'<br/>
+%% `Path' = `"/foobar"'<br/>
+%% `Path' must begin with a forward slash `/'.
+%% 
 %% `Method' is either a string, stating the HTTP method exactly as in the
 %% protocol, i.e: `"POST"' or `"GET"'. It could also be an atom, which is
 %% then made in to uppercase, if it isn't already.
 %% sending a request if the connection is closed after the data has been
 %% sent. The default value is 1.
 %% @end
--spec request(string(), string() | atom(), headers(), iolist(),
-        pos_integer() | infinity, [option()]) -> result().
-request(URL, Method, Hdrs, Body, Timeout, Options) ->
+request(Host, Port, Ssl, Path, Method, Hdrs, Body, Timeout, Options) ->
     verify_options(Options, []),
-    Args = [self(), URL, Method, Hdrs, Body, Options],
+    Args = [self(), Host, Port, Ssl, Path, Method, Hdrs, Body, Options],
     Pid = spawn_link(lhttpc_client, request, Args),
     receive
         {response, Pid, R} ->

src/lhttpc_client.erl

 %%% @end
 -module(lhttpc_client).
 
--export([request/6]).
+-export([request/9]).
 
 -include("lhttpc_types.hrl").
 
 -define(CONNECTION_HDR(HDRS, DEFAULT),
     string:to_lower(lhttpc_lib:header_value("connection", HDRS, DEFAULT))).
 
--spec request(pid(), string(), string() | atom(), headers(),
-        iolist(), [option()]) -> no_return().
-%% @spec (From, URL, Method, Hdrs, Body, Options) -> ok
+-spec request(pid(), string(), 1..65535, true | false, string(),
+        string() | atom(), headers(), iolist(), [option()]) -> no_return().
+%% @spec (From, Host, Port, Ssl, Path, Method, Hdrs, RequestBody, Options) -> ok
 %%    From = pid()
-%%    URL = string()
+%%    Host = string()
+%%    Port = integer()
+%%    Ssl = boolean()
 %%    Method = atom() | string()
 %%    Hdrs = [Header]
 %%    Header = {string() | atom(), string()}
 %%    Options = [Option]
 %%    Option = {connect_timeout, Milliseconds}
 %% @end
-request(From, URL, Method, Hdrs, Body, Options) ->
+request(From, Host, Port, Path, Ssl, Method, Hdrs, Body, Options) ->
     Result = try
-        execute(URL, Method, Hdrs, Body, Options)
+        execute(Host, Port, Path, Ssl, Method, Hdrs, Body, Options)
     catch 
         Reason ->
             {response, self(), {error, Reason}};
     From ! Result,
     ok.
 
-execute(URL, Method, Hdrs, Body, Options) ->
-    {Host, Port, Path, Ssl} = lhttpc_lib:parse_url(URL),
+execute(Host, Port, Path, Ssl, Method, Hdrs, Body, Options) ->
     NormalizedMethod = lhttpc_lib:normalize_method(Method),
     Request = lhttpc_lib:format_request(Path, NormalizedMethod, Hdrs, Host,
         Body),