class Smartfocus::Api
This is where the communication with the API is made.
Constants
- ATTRIBUTES
- HTTP_VERBS
HTTP verbs allowed to trigger a call-chain
Public Class Methods
Initialize
@param [Hash] Instance attributes to assign @yield Freshly-created instance (optionnal)
# File lib/smartfocus/api.rb, line 26 def initialize(params = {}) yield(self) if block_given? assign_attributes(params) end
Public Instance Methods
Base uri
# File lib/smartfocus/api.rb, line 123 def base_uri "http://#{server_name}/#{endpoint}/services/rest/" end
Perform an API call
@param [Smartfocus::Request] Request
to perform
# File lib/smartfocus/api.rb, line 88 def call(request) # == Check presence of these essential attributes == unless server_name and endpoint raise Smartfocus::Exception, "Cannot make an API call without a server name and an endpoint !" end with_retries do logger.send "#{request.uri} with query : #{request.parameters} and body : #{request.body}" response = perform_request(request) Smartfocus::Response.new(response, logger).extract end end
Logout from Smartfocus
API
@return [Boolean] true if the connection has been destroyed
# File lib/smartfocus/api.rb, line 56 def close_connection if connected? get.connect.close.call else return false end rescue Smartfocus::Exception => e ensure invalidate_token! not connected? end
Check whether the connection has been established or not
@return [Boolean] true if the connection has been establshed
# File lib/smartfocus/api.rb, line 72 def connected? !token.nil? end
Change API endpoint. This will close the connection to the current endpoint
@param [String] new endpoint (apimember, apiccmd, apitransactional, …)
# File lib/smartfocus/api.rb, line 116 def endpoint=(value) close_connection @endpoint = value end
When a token is no longer valid, this method can be called. The connected?
method will return false
# File lib/smartfocus/api.rb, line 79 def invalidate_token! self.token = nil end
Login to Smartfocus
API
@return [Boolean] true if the connection has been established.
# File lib/smartfocus/api.rb, line 46 def open_connection return false if connected? self.token = get.connect.open.call :login => @login, :password => @password, :key => @key connected? end
Reset session
Useful when the session has expired.
# File lib/smartfocus/api.rb, line 37 def reset_session close_connection open_connection end
Set a new token
@param [String] new token
# File lib/smartfocus/api.rb, line 107 def token=(value) @token = value end
Private Instance Methods
# File lib/smartfocus/api.rb, line 144 def assign_attributes(parameters) parameters or return ATTRIBUTES.each do |attribute| public_send("#{attribute}=", (parameters[attribute] || self.class.public_send(attribute))) end end
# File lib/smartfocus/api.rb, line 136 def build_request(http_verb) Smartfocus::Request.new(http_verb, token, server_name, endpoint) end
# File lib/smartfocus/api.rb, line 164 def logger if @logger.nil? @logger = Smartfocus::Logger.new(STDOUT) @logger.level = (debug ? Logger::DEBUG : Logger::WARN) end @logger end
# File lib/smartfocus/api.rb, line 140 def perform_request(request) self.class.send request.http_verb, base_uri + request.uri, :query => request.parameters, :body => request.body, :timeout => 30 end
# File lib/smartfocus/api.rb, line 151 def with_retries retries = 3 begin yield rescue Errno::ECONNRESET, Timeout::Error => e if ((retries -= 1) > 0) retry else raise e end end end