Skip to content

Commit

Permalink
Extract hn options building to private function
Browse files Browse the repository at this point in the history
  • Loading branch information
edgurgel committed Feb 11, 2015
1 parent 295efb2 commit e969267
Showing 1 changed file with 21 additions and 17 deletions.
38 changes: 21 additions & 17 deletions lib/httpoison/base.ex
Original file line number Diff line number Diff line change
Expand Up @@ -73,29 +73,15 @@ defmodule HTTPoison.Base do
@spec request(atom, binary, binary, headers, [{atom, any}]) :: {:ok, Response.t | AsyncResponse.t}
| {:error, Error.t}
def request(method, url, body \\ "", headers \\ [], options \\ []) do
timeout = Keyword.get options, :timeout, 5000
stream_to = Keyword.get options, :stream_to
proxy = Keyword.get options, :proxy
hn_options = [connect_timeout: timeout] ++ Keyword.get options, :hackney, []
hn_options = build_hackney_options(options)
body = process_request_body body

if Keyword.has_key?(options, :params) do
url = url <> "?" <> URI.encode_query(options[:params])
end

if stream_to do
hn_options = [:async, {:stream_to, spawn(__MODULE__, :transformer, [stream_to])}] ++ hn_options
end

if proxy do
hn_options = [proxy: proxy] ++ hn_options
end

case :hackney.request(method,
process_url(to_string(url)),
process_request_headers(headers),
body,
hn_options) do
case :hackney.request(method, process_url(to_string(url)), process_request_headers(headers),
body, hn_options) do
{:ok, status_code, headers, client} when status_code in [204, 304] ->
response(status_code, headers, "")
{:ok, status_code, headers} -> response(status_code, headers, "")
Expand All @@ -109,6 +95,24 @@ defmodule HTTPoison.Base do
end
end

defp build_hackney_options(options) do
timeout = Keyword.get options, :timeout, 5000
stream_to = Keyword.get options, :stream_to
proxy = Keyword.get options, :proxy

hn_options = [connect_timeout: timeout] ++ Keyword.get options, :hackney, []

if stream_to do
hn_options = [:async, {:stream_to, spawn(__MODULE__, :transformer, [stream_to])}] ++ hn_options
end

if proxy do
hn_options = [proxy: proxy] ++ hn_options
end

hn_options
end

@spec request!(atom, binary, binary, headers, [{atom, any}]) :: Response.t
def request!(method, url, body \\ "", headers \\ [], options \\ []) do
case request(method, url, body, headers, options) do
Expand Down

0 comments on commit e969267

Please sign in to comment.