Skip to content

Commit

Permalink
Swap Exmerl package to old school :xmerl
Browse files Browse the repository at this point in the history
  • Loading branch information
programisti committed Jan 26, 2016
1 parent 9c8849b commit e062ca8
Showing 1 changed file with 15 additions and 20 deletions.
35 changes: 15 additions & 20 deletions lib/ndc_ex.ex
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
defmodule NDCEx do
require HTTPotion
require Logger
import SweetXml
@acceptable_ndc_methods [ AirShopping: ["AirShoppingRQ", "AirShoppingRS"],
FlightPrice: ["FlightPriceRQ", "FlightPriceRS"],
SeatAvailability: ["SeatAvailabilityRQ", "SeatAvailabilityRS"],
Expand All @@ -15,39 +14,35 @@ defmodule NDCEx do
]

def request(method, params) do
try do
[request_name, response_name] = @acceptable_ndc_methods[method]

:ndc
|> get_mix_config
|> NDCEx.Message.Base.build_document(request_name, params)
|> rest_call_with_message(get_mix_config(:rest))
rescue
e -> {:error, error_message(e.message)}
end
end

def rest_call_with_message(xml, rest_config) do
Logger.debug xml
try do
case HTTPotion.post rest_config[:url], [body: xml, headers: rest_config[:headers]] do
%HTTPotion.Response{body: body, headers: _headers, status_code: 200 } ->
{:ok, Exmerl.parse(body)}
%HTTPotion.Response{body: body, headers: _headers, status_code: _status_code } ->
{:error, Exmerl.parse(body)}
apply(NDCEx.Response.Error, :convert, [body])
%HTTPotion.Response{status_code: 404} ->
{:error, error_message("Request does not exist")}
_ ->
{:error, error_message("Unknown Error")}
end
rescue
e -> {:error, error_message(e.message)}
case HTTPotion.post rest_config[:url], [body: xml, headers: rest_config[:headers]] do
%HTTPotion.Response{body: body, headers: _headers, status_code: 200 } ->
{:ok, scan body}
%HTTPotion.Response{body: body, headers: _headers, status_code: _status_code } ->
{:error, scan body}
%HTTPotion.Response{status_code: 404} ->
{:error, error_message("Request does not exist")}
_ ->
{:error, error_message("Unknown Error")}
end
end

def scan(doc) do
{ response, _ } = doc |> String.to_char_list |> :xmerl_scan.string
response
end

defp error_message(message) do
Exmerl.parse "<error><message>#{message}</message></error>"
scan "<error><message>#{message}</message></error>"
end

def get_mix_config(key) when is_atom(key) do
Expand Down

0 comments on commit e062ca8

Please sign in to comment.