have remote_gateway request use EM requests if available
This commit is contained in:
parent
e85c50bf62
commit
792d2ff1f2
1 changed files with 27 additions and 18 deletions
|
@ -12,18 +12,16 @@ module RocketAMF
|
|||
def fetch
|
||||
uri = @service.gateway.uri
|
||||
data = envelope.serialize
|
||||
|
||||
if EventMachine.reactor_running?
|
||||
req = EM::HttpRequest.new(uri).post :body => data
|
||||
response_body = req.response
|
||||
else
|
||||
req = Net::HTTP::Post.new(uri.path)
|
||||
req.body = data
|
||||
res = Net::HTTP.new(uri.host, uri.port).start { |http| http.request(req) }
|
||||
case res
|
||||
when Net::HTTPSuccess, Net::HTTPRedirection
|
||||
result = RocketAMF::Envelope.new.populate_from_stream(res.body)
|
||||
first_message_data = result.messages[0].data
|
||||
if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE
|
||||
raise AMFError.new(first_message_data)
|
||||
end
|
||||
return result
|
||||
when Net::HTTPSuccess
|
||||
response_body = res.body
|
||||
else
|
||||
error = nil
|
||||
begin
|
||||
|
@ -34,6 +32,17 @@ module RocketAMF
|
|||
raise ConnectionError, "Error connecting to gateway: #{error}"
|
||||
end
|
||||
end
|
||||
begin
|
||||
result = RocketAMF::Envelope.new.populate_from_stream(response_body)
|
||||
rescue Exception => e
|
||||
raise ConnectionError, "Error parsing gateway response: #{e.message}"
|
||||
end
|
||||
first_message_data = result.messages[0].data
|
||||
if first_message_data.respond_to?(:[]) && first_message_data[:code] == ERROR_CODE
|
||||
raise AMFError.new(first_message_data)
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
|
|
Loading…
Reference in a new issue