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.
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)
Market port
Market URI Use host file in development.
key - The API key
user - The user ID
Create a new instance of the API
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
Issue DELETE request to the market
endpoint The endpoint URI e.g. /test
params Hash of URL parameters
# 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
Issue GET request to the market
endpoint The endpoint URI e.g. /test
params Hash of URL parameters
# 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
Either get an object (de-serialized) or a raw hash, this will recursively deserialize any children
data - The data from (e.g. from an HTTP request)
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
Issue POST request to the market
endpoint The endpoint URI e.g. /test
data Data to post
# 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
Issue PUT request to the market
endpoint The endpoint URI e.g. /test
data Data to post
# 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
Reponse Test to see if the supplied credentials will authenticate
# File lib/onapp_market/api.rb, line 92 def test_credentials get("/test") end