Class: VCAP::Services::Api::AsyncHttpRequest

Inherits:
Object
  • Object
show all
Defined in:
lib/services/api/async_requests.rb

Class Method Summary collapse

Class Method Details

.new(url, token, verb, timeout, msg = VCAP::Services::Api::EMPTY_REQUEST) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/services/api/async_requests.rb', line 19

def new(url, token, verb, timeout, msg=VCAP::Services::Api::EMPTY_REQUEST)

  req = {
    :head => {
      VCAP::Services::Api::GATEWAY_TOKEN_HEADER => token,
      'Content-Type' => 'application/json',
    },
    :body => msg.encode,
  }
  if timeout
    EM::HttpRequest.new(url, :inactivity_timeout => timeout).send(verb.to_sym, req)
  else
    EM::HttpRequest.new(url).send(verb.to_sym, req)
  end
end

.request(url, token, verb, timeout, msg = VCAP::Services::Api::EMPTY_REQUEST) ⇒ Object

Raises:

  • (UnexpectedResponse)


35
36
37
38
39
40
41
42
43
44
45
# File 'lib/services/api/async_requests.rb', line 35

def request(url, token, verb, timeout, msg=VCAP::Services::Api::EMPTY_REQUEST)
  req = new(url, token, verb, timeout, msg)
  f = Fiber.current
  req.callback { f.resume(req) }
  req.errback  { f.resume(req) }
  http = Fiber.yield
  raise UnexpectedResponse, "Error sending request #{msg.extract.to_json} to gateway #{@url}: #{http.error}" unless http.error.empty?
  code = http.response_header.status.to_i
  body = http.response
  [code, body]
end