class OnappMarket::API

Class

This is a simple base class for representing access to OnApp market APIs using simple credentials (akin to the security model employed by the control panel API). Access is likely to be REST based.

Assumptions

It is assumed any user of this class will have been assigned a user / API key pair. (Note dependant on final AAA solution implemented for the market - these credentials may change)

Constants

MARKET_PORT_DEFAULT

Market port

MARKET_URI_DEFAULT

Market URI Use host file in development.

Attributes

key[R]
  • key - The API key

user[R]
  • user - The user ID

Public Class Methods

new(user, key, *overrides) click to toggle source

Create a new instance of the API

Attributes

  • user - The user ID

  • key - The API key

  • overrides - Override :host or :port for market from defaults

# File lib/onapp_market/api.rb, line 71
def initialize(user, key, *overrides) 
  @user = user
  @key  = key
  host = MARKET_URI_DEFAULT
  port = MARKET_PORT_DEFAULT
  overrides.each do |h|
    if h.is_a? Hash
      port = h[:port] if h.has_key? :port
      host = h[:host] if h.has_key? :host
    end
  end
  # Create RestClient base resource for the market
  @resource = RestClient::Resource.new("#{host}:#{port.to_s}", :user => user, :password => key, :accept => :json, :content_type => :json )
end

Public Instance Methods

delete(endpoint, params = nil) click to toggle source

Issue DELETE request to the market

Attributes

  • endpoint The endpoint URI e.g. /test

  • params Hash of URL parameters

Returns

# File lib/onapp_market/api.rb, line 161
def delete(endpoint, params = nil)
  begin
    # Issue get request and parse - if OK, return response
    del_response = @resource[endpoint].delete(:params => HashConverter.encode(params), :accept => :json) {|response, request, result| response}
    json = JSON.parse(del_response, {:symbolize_names => true})
    API::Response.new(json[:ok], self.get_object_or_data(json[:data]), json[:errors], del_response.code)
  rescue
    # Problem - return 522
    API::Response.new(false, nil, {API::Response::TIMEOUT_ERROR => "Error accessing DELETE #{endpoint.to_s}"}, 522)
  end
end
get(endpoint, params = nil, deserialize_list = false) click to toggle source

Issue GET request to the market

Attributes

  • endpoint The endpoint URI e.g. /test

  • params Hash of URL parameters

Returns

# File lib/onapp_market/api.rb, line 103
def get(endpoint, params = nil, deserialize_list = false)
  begin
    # Issue get request and parse - if OK, return response
    get_response = @resource[endpoint].get(:params => HashConverter.encode(params), :accept => :json) {|response, request, result| response}
    json = JSON.parse(get_response, {:symbolize_names => true})
    data = self.get_object_or_data(json[:data])
    API::Response.new(json[:ok], data, json[:errors], get_response.code)
  rescue
    # Problem - return 522
    API::Response.new(false, nil, {API::Response::TIMEOUT_ERROR => "Error accessing GET #{endpoint.to_s}"}, 522)
  end
end
get_object_or_data(data) click to toggle source

Either get an object (de-serialized) or a raw hash, this will recursively deserialize any children

Attributes

  • data - The data from (e.g. from an HTTP request)

Returns

Hash or OnappMarket::API::Serializable

# File lib/onapp_market/api.rb, line 180
def get_object_or_data(data)
  return nil if data == nil
  # Try and serialize (if data exists)
  object = OnappMarket::API::Serialize.from_hash(data) if data != nil and data.is_a?(Hash) and data[:type] != nil
  # Set api if possible
  object.api = self if object != nil and object.respond_to? "api" 
  return object unless object == nil
  # Look for arrays and hashes and deserialize children
  data.each { |k,v| data[k] = get_object_or_data(v) } if data.is_a?(Hash)
  data = data.map { |n| get_object_or_data(n)} if data.is_a?(Array)
  return data
end
post(endpoint, data = nil) click to toggle source

Issue POST request to the market

Attributes

  • endpoint The endpoint URI e.g. /test

  • data Data to post

Returns

# File lib/onapp_market/api.rb, line 123
def post(endpoint,  data = nil)
  begin
    # Issue get request and parse - if OK, return response
    post_response = @resource[endpoint].post({:data => data}.to_json, :content_type => :json, :accept => :json) {|response, request, result| response}
    json = JSON.parse(post_response, {:symbolize_names => true})
    API::Response.new(json[:ok], self.get_object_or_data(json[:data]), json[:errors], post_response.code)
  rescue
    # Problem - return 522
    API::Response.new(false, nil, {API::Response::TIMEOUT_ERROR => "Error accessing POST #{endpoint.to_s}"}, 522)
  end
end
put(endpoint, data = nil) click to toggle source

Issue PUT request to the market

Attributes

  • endpoint The endpoint URI e.g. /test

  • data Data to post

Returns

# File lib/onapp_market/api.rb, line 142
def put(endpoint,  data = nil)
  begin
    # Issue get request and parse - if OK, return response
    put_response = @resource[endpoint].put({:data => data}.to_json, :content_type => :json, :accept => :json) {|response, request, result| response}
    json = JSON.parse(put_response, {:symbolize_names => true})
    API::Response.new(json[:ok], self.get_object_or_data(json[:data]), json[:errors], put_response.code)
  rescue
    # Problem - return 522
    API::Response.new(false, nil, {API::Response::TIMEOUT_ERROR => "Error accessing PUT #{endpoint.to_s}"}, 522)
  end
end
test_credentials() click to toggle source

Reponse Test to see if the supplied credentials will authenticate

Returns

# File lib/onapp_market/api.rb, line 92
def test_credentials
  get("/test")
end