Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward http options to httpc request #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/detergentex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ defmodule Detergentex do
Supervisor.start_link(children, opts)
end

def call(wsdl, method, params) do
Detergentex.Client.call_service(wsdl, method, params)
def call(wsdl, method, params, options \\ []) do
Detergentex.Client.call_service(wsdl, method, params, options)
end

def init_model(wsdl_url, prefix \\ 'p') do
Expand Down
30 changes: 28 additions & 2 deletions lib/detergentex/client.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,30 @@
defmodule Detergentex.Client do
use GenServer
require Record

def default_logger(_), do: :ok

original_record = Record.extract(:call_opts, from_lib: "detergent/include/detergent.hrl")
call_opts_record = Keyword.merge(original_record, request_logger: &__MODULE__.default_logger/1, response_logger: &__MODULE__.default_logger/1)
Record.defrecord :call_opts, call_opts_record

def start_link do
:inets.start()
GenServer.start_link(__MODULE__, {}, [name: :detergent_client])
end

def call_service(wsdl, method, params) do
def call_service(wsdl, method, params, options \\ []) do
if not is_wsdl(wsdl) do
wsdl = to_char_list wsdl
end
method_to_call = to_char_list(method)
detergent_params = convert_to_detergent_params(params)
:detergent.call(wsdl, method_to_call, detergent_params)

call_options = options
|> convert_to_detergent_params
|> call_opts_from_klist

:detergent.call(wsdl, method_to_call, detergent_params, call_options)
end

def init_model(wsdl_url, prefix \\ 'p') do
Expand Down Expand Up @@ -40,4 +52,18 @@ defmodule Detergentex.Client do
end
end)
end

defp call_opts_from_klist(keywords) do
call_options = call_opts()
Enum.reduce(keywords, call_options, fn ({k,v}, acc) ->
case k do
:url -> call_opts(acc, url: v)
:prefix -> call_opts(acc, prefix: v)
:http_headers -> call_opts(acc, http_headers: v)
:http_client_options -> call_opts(acc, http_client_options: v)
:request_logger -> call_opts(acc, request_logger: v)
:response_logger -> call_opts(acc, response_logger: v)
end
end)
end
end