Current section

Files

Jump to
ndc_ex_sdk lib ndc_ex.ex
Raw

lib/ndc_ex.ex

defmodule NDCEx do
require HTTPoison
require NDCLogger
use Timex
def request(method, data, config) do
request_start_time = Time.now
NDCEx.Message.Base.build_document(method, data, config)
|> rest_call_with_message(method, data, config, request_start_time)
end
defp is_ssl(ssl_config, _) when ssl_config == nil, do: []
defp is_ssl(ssl_config, provider) when ssl_config != nil do
file_name = "ndc_providers/#{String.downcase(provider)}/#{ssl_config[:crt]}"
path = if File.exists?(Path.expand(File.cwd! <> "../../../config/#{file_name}")) do
Path.expand(File.cwd! <> "../../../config/#{file_name}")
else
"test/#{file_name}"
end
[hackney: [ssl_options: [cacertfile: path]]]
end
def rest_call_with_message({:ok, request_xml}, method, data, config, request_start_time) do
url = NDCEx.Message.Base.build_url(config[:server][:url], method)
headers = get_headers(config[:rest][:headers], method)
processing_end = Time.now
#TODO Handle different exceptions during request and return different errors
# try do
case HTTPoison.post(url, [body: request_xml, headers: headers, timeout: 30000 ], is_ssl(config[:ssl], config[:label])) do
%HTTPoison.Response{body: response_xml, headers: _headers, status_code: 200 } ->
NDCNormalize.remove_soap_header(response_xml, config[:soap][:soap_header])
|> NDCNormalize.remove_soap_tags(config[:soap][:response_namespace])
|> NDCNormalize.remove_message_wrapper(config[:message])
|> NDCNormalize.check_ndc_errors ## This function returns a tuple including the xml and the atom :error or :ok depending if there are any error on the response or not.
|> process_response(method, request_xml, data, config, request_start_time)
%HTTPoison.Response{body: response_xml, headers: _headers, status_code: _status_code } ->
process_response({:error, NDCNormalize.remove_soap_tags(response_xml, config[:soap][:response_namespace])}, method, request_xml, data, config, request_start_time)
%HTTPoison.Response{status_code: 404} ->
{:error, error_message("NDC host/URL invalid or missing")}
_ ->
{:error, error_message("Unknown HTTP response error")}
end
# catch _ , _ ->
# {:error, error_message("Exception while HTTP request")}
# end
end
defp build_url(url, method), do: String.replace(url, "{{NDC-method}}", Atom.to_string(method))
defp error_message(message), do: "<Error><Message>#{message}</Message></Error>"
defp remove_tag(doc, tag), do: String.replace(doc, tag, "")
def get_mix_config(key) when is_atom(key), do: Application.get_env(:ndc_ex_sdk, key)
defp get_headers(headers, method) do
Enum.map(headers, fn ({key, value}) ->
{key, String.replace(value, "{{NDC-method}}", Atom.to_string(method))}
end)
end
defp process_response({status, response}, method, request, data, config, request_start_time) do
response_time = Time.diff(Time.now, request_start_time, :milliseconds)
request_xml = request
response_xml = "<!-- AG-Info: ProviderName: #{config[:label]} | NDCMethod: #{method} | ResponseTime: #{response_time} | ProcessingTime: - -->\n\n\n#{response}"
NDCLogger.event(config[:label], method, data, request_xml, response_xml)
{status, response_xml}
end
end