module RHC::Rest

Public Instance Methods

logger() click to toggle source

@@headers = {:accept => "application/json;version=#{RHC::Rest::VERSION}"}

# File lib/rhc/rest.rb, line 92
def logger
  Logger.new(STDOUT)
end
new_request(options) click to toggle source
# File lib/rhc/rest.rb, line 150
def new_request(options)
  # user specified timeout takes presidence
  options[:timeout] = $rest_timeout || options[:timeout]

  RestClient::Request.new options
end
parse_response(response) click to toggle source
# File lib/rhc/rest.rb, line 96
def parse_response(response)
  result = RHC::Json.decode(response)
  type = result['type']
  data = result['data']
  case type
  when 'domains'
    domains = Array.new
    data.each do |domain_json|
      domains.push(Domain.new(domain_json, @debug))
    end
    return domains
  when 'domain'
    return Domain.new(data, @debug)
  when 'applications'
    apps = Array.new
    data.each do |app_json|
      apps.push(Application.new(app_json, @debug))
    end
    return apps
  when 'application'
    app = Application.new(data, @debug)
    result['messages'].each do |message|
      app.add_message(message['text']) if message['field'].nil? or message['field'] == 'result'
    end
    return app
  when 'cartridges'
    carts = Array.new
    data.each do |cart_json|
      carts.push(Cartridge.new(cart_json, @debug))
    end
    return carts
  when 'cartridge'
    return Cartridge.new(data, @debug)
  when 'user'
    return User.new(data, @debug)
  when 'keys'
    keys = Array.new
    data.each do |key_json|
      keys.push(Key.new(key_json, @debug))
    end
    return keys
  when 'key'
    return Key.new(data, @debug)
  when 'gear_groups'
    gears = Array.new
    data.each do |gear_json|
      gears.push(GearGroup.new(gear_json, @debug))
    end
    return gears
  else
    data
  end
end
process_error_response(response) click to toggle source
# File lib/rhc/rest.rb, line 177
def process_error_response(response)
  messages = Array.new
  begin
    result = RHC::Json.decode(response)
    messages = result['messages']
  rescue => e
    logger.debug "Response did not include a message from server" if @mydebug
  end
  case response.code
  when 401
    raise UnAuthorizedException, "Not authenticated"
  when 403
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise RequestDeniedException, message['text']
      end
    end
  when 404
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ResourceNotFoundException, message['text']
      end
    end
  when 409
    messages.each do |message|
      if message['severity'] and message['severity'].upcase == "ERROR"
        raise ValidationException, message['text']
      end
    end
  when 422
    #puts response
    e = nil
    messages.each do |message|
      if e and e.field == message["field"]
        e.message << " #{message["text"]}"
      else
        e = ValidationException.new(message["text"], message["field"], message["code"])
      end
    end
    raise e
  when 400
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ClientErrorException, message['text']
      end
    end
  when 500
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ServerErrorException.new(message['text'], message["exit_code"] ? message["exit_code"].to_i : nil)
      end
    end
  when 503
    messages.each do |message|
      if message['severity'].upcase == "ERROR"
        raise ServiceUnavailableException, message['text']
      end
    end
  else
    raise ResourceAccessException, "Server returned error code with no output: #{response.code}"
  end
end
request(request) click to toggle source
# File lib/rhc/rest.rb, line 157
def request(request)
  begin
    response = request.execute
    #set cookie
    rh_sso = response.cookies['rh_sso']
    if not rh_sso.nil?
      @@headers["cookie"] = "rh_sso=#{rh_sso}"
    end
    return parse_response(response) unless response.nil? or response.code == 204
  rescue RestClient::RequestTimeout => e
    raise TimeoutException.new("Connection to server timed out. It is possible the operation finished without being able to report success. Use 'rhc domain show' or 'rhc app status' to check the status of your applications.") 
  rescue RestClient::ServerBrokeConnection => e
    raise ConnectionException.new("Connection to server got interrupted: #{e.message}")
  rescue RestClient::ExceptionWithResponse => e
    process_error_response(e.response)
  rescue => e
    raise ResourceAccessException.new("Failed to access resource: #{e.message}")
  end
end