Method: MarathonDeploy::HttpUtil.req

Defined in:
lib/marathon_deploy/http_util.rb

.req(method, url, payload = nil, errors_are_fatal = false) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/marathon_deploy/http_util.rb', line 13

def self.req(method, url, payload=nil, errors_are_fatal=false)
  uri = construct_uri url 
  begin
    http = Net::HTTP.new(uri.host, uri.port)
    http.use_ssl = uri.scheme == 'https'
    http.open_timeout = @@o_timeout
    http.read_timeout = @@r_timeout
    req = Net::HTTP.const_get(method).new(uri.request_uri)
    if MarathonDeploy::MarathonDefaults::marathon_username and MarathonDeploy::MarathonDefaults::marathon_password
      req.basic_auth(MarathonDeploy::MarathonDefaults::marathon_username, MarathonDeploy::MarathonDefaults::marathon_password)
    end
    if payload
      req.body = payload.to_json
    end
    req["Content-Type"] = "application/json"
    response = http.request(req)
  rescue Exception => e
    if errors_are_fatal
      $LOG.error("Error calling marathon api: #{e.message}")
      exit!
    else
      message = "Error calling marathon api: #{e.message}"
      $LOG.error(message)
      raise Error::MarathonError, message, caller
    end
  end
  return response
end