module DomoscioAdmin
Constants
- VERSION
Attributes
Public Class Methods
# File lib/domoscio_admin.rb, line 37 def self.api_uri(url = '') URI(configuration.root_url + url) end
# File lib/domoscio_admin.rb, line 32 def self.configure self.configuration ||= Configuration.new yield configuration end
Actual HTTP call is performed here
# File lib/domoscio_admin.rb, line 103 def self.perform_call(uri, method, params, headers) Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https') do |http| req = Net::HTTP::const_get(method.capitalize).new(uri.request_uri, headers) req.body = DomoscioAdmin::JSON.dump(params) http.request req end end
This helper will check the response status and build the correcponding DomoscioAdmin::ResponseError
# File lib/domoscio_admin.rb, line 90 def self.raise_http_failure(uri, response, params) return if response.is_a? Net::HTTPSuccess raise ResponseError.new( uri, response.code.to_i, DomoscioAdmin::JSON.load((response.body.nil? ? '' : response.body), symbolize_keys: true), response.body, params ) end
-
method
: HTTP method; lowercase symbol, e.g. :get, :post etc. -
url
: the part afterConfiguration#root_url
-
params
: hash; entity data for creation, update etc.; will dump it byJSON
and assign to Net::HTTPRequest#body
Performs HTTP requests to Adaptive Engine On token issues, will try once to get a new token then will output a DomoscioAdmin::ReponseError with details
Raises DomoscioAdmin::ResponseError
on Adaptive Error
Status Raises DomoscioAdmin::ProcessingError
on Internal Error
# File lib/domoscio_admin.rb, line 52 def self.request(method, url, params = {}) store_tokens, headers = request_headers uri = api_uri(url) response = DomoscioAdmin.send_request(uri, method, params, headers) return response if response.is_a? DomoscioAdmin::ProcessingError begin raise_http_failure(uri, response, params) data = DomoscioAdmin::JSON.load(response.body.nil? ? '' : response.body) if store_tokens DomoscioAdmin::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] }) end rescue MultiJson::LoadError => e data = ProcessingError.new(uri, 500, e, response.body, params) rescue ResponseError => e data = e end data end
Process the token loading and analyze will return the processed headers and a token store flag
# File lib/domoscio_admin.rb, line 143 def self.request_headers begin auth_token = DomoscioAdmin::AuthorizationToken::Manager.get_token if auth_token && auth_token[:access_token] && auth_token[:refresh_token] [false, send_current_tokens(auth_token)] else [true, request_new_tokens] end rescue SyntaxError, StandardError [true, request_new_tokens] end end
If we cant find tokens of they are corrupted / expired, then we set headers to request new ones
# File lib/domoscio_admin.rb, line 169 def self.request_new_tokens { 'user_agent' => DomoscioAdmin.user_agent, 'ClientId' => DomoscioAdmin.configuration.client_id, 'Authorization' => "Token token=#{DomoscioAdmin.configuration.client_passphrase}", 'Content-Type' => 'application/json' } end
This method is called when AdaptiveEngine returns tokens errors Action on those errors is to retry and request new tokens, those new token are then stored
# File lib/domoscio_admin.rb, line 113 def self.retry_call_and_store_tokens(uri, method, params) headers = request_new_tokens response = perform_call(uri, method, params, headers) DomoscioAdmin::AuthorizationToken::Manager.storage.store({ access_token: response['Accesstoken'], refresh_token: response['Refreshtoken'] }) response end
If stored token successfully loaded we build the header with them
# File lib/domoscio_admin.rb, line 158 def self.send_current_tokens(auth_token) { 'user_agent' => DomoscioAdmin.user_agent, 'ClientId' => DomoscioAdmin.configuration.client_id, 'AccessToken' => auth_token[:access_token], 'RefreshToken' => auth_token[:refresh_token], 'Content-Type' => 'application/json' } end
This function catches usual Http errors during calls
# File lib/domoscio_admin.rb, line 79 def self.send_request(uri, method, params, headers) response = perform_call(uri, method, params, headers) response = retry_call_and_store_tokens(uri, method, params) if %w[401 403].include? response.code response rescue Timeout::Error, Errno::EINVAL, Errno::ECONNREFUSED, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e ProcessingError.new(uri, 500, e, response, params) end
# File lib/domoscio_admin.rb, line 134 def self.uname `uname -a 2>/dev/null` if RUBY_PLATFORM =~ /linux|darwin/i rescue Errno::ENOMEM 'uname lookup failed' end
# File lib/domoscio_admin.rb, line 123 def self.user_agent @uname ||= uname { bindings_version: DomoscioAdmin::VERSION, lang: 'ruby', lang_version: "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})", platform: RUBY_PLATFORM, uname: @uname }.to_s end