123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- class Idoit
- def self.verify(api_token, endpoint, _client_id = nil)
- raise 'api_token required' if api_token.blank?
- raise 'endpoint required' if endpoint.blank?
- params = {
- apikey: api_token,
- }
- _query('cmdb.object_types', params, _url_cleanup(endpoint))
- end
- def self.query(method, filter = {})
- setting = Setting.get('idoit_config')
- raise __("The required field 'api_token' is missing from the config.") if setting[:api_token].blank?
- raise __("The required field 'endpoint' is missing from the config.") if setting[:endpoint].blank?
- params = {
- apikey: setting[:api_token],
- }
- if filter.present?
- params[:filter] = filter
- end
- _query(method, params, _url_cleanup(setting[:endpoint]))
- end
- def self._query(method, params, url)
- result = UserAgent.post(
- url,
- {
- method: method,
- params: params,
- version: '2.0',
-
-
-
- id: 42,
- },
- {
- json: true,
- open_timeout: 6,
- read_timeout: 16,
- log: {
- facility: 'idoit',
- },
- },
- )
- raise "Can't fetch objects from #{url}: Unable to parse response from server. Invalid JSON response." if !result.success? && result.error =~ %r{JSON::ParserError:.+?\s+unexpected\s+token\s+at\s+'<!DOCTYPE\s+html}i
- raise "Can't fetch objects from #{url}: #{result.error}" if !result.success?
-
- if result.data['result'].instance_of?(Array)
- result.data['result'].each do |item|
- next if !item['id']
- item['link'] = "#{_url_cleanup_baseurl(url)}/?objID=#{item['id']}"
- item['link'].gsub!(%r{([^:])//+}, '\\1/')
- end
- end
- result.data
- end
- def self._url_cleanup(url)
- url.strip!
- raise "Invalid endpoint '#{url}', need to start with http:// or https://" if !url.match?(%r{^http(s|)://}i)
- url = _url_cleanup_baseurl(url)
- url = "#{url}/src/jsonrpc.php"
- url.gsub(%r{([^:])//+}, '\\1/')
- end
- def self._url_cleanup_baseurl(url)
- url.strip!
- raise "Invalid endpoint '#{url}', need to start with http:// or https://" if !url.match?(%r{^http(s|)://}i)
- url.gsub!(%r{src/jsonrpc.php}, '')
- url.gsub(%r{([^:])//+}, '\\1/')
- end
- end
|