Module: Maestrano::API::Operation::Base

Includes:
Preset
Defined in:
lib/maestrano/api/operation/base.rb

Class Method Summary collapse

Methods included from Preset

included

Class Method Details

.api_url(url = '') ⇒ Object



7
8
9
# File 'lib/maestrano/api/operation/base.rb', line 7

def self.api_url(url='')
  Maestrano[self.preset].param('api.host') + Maestrano[self.preset].param('api.base') + url
end

.request(method, url, api_token, params = {}, headers = {}) ⇒ Object

Perform remote request



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/maestrano/api/operation/base.rb', line 12

def self.request(method, url, api_token, params={}, headers={})
  unless api_token ||= Maestrano[self.preset].param('api_token')
    raise Maestrano::API::Error::AuthenticationError.new('No API key provided.')
  end

  request_opts = { :verify_ssl => false }

  if self.ssl_preflight_passed?
    request_opts.update(
      verify_ssl: OpenSSL::SSL::VERIFY_PEER,
      ssl_ca_file: Maestrano[self.preset].param('ssl_bundle_path')
    )
  end

  params = Util.objects_to_ids(params)
  url = api_url(url)

  case method.to_s.downcase.to_sym
  when :get, :head, :delete
    # Make params into GET parameters
    url += "#{URI.parse(url).query ? '&' : '?'}#{uri_encode(params)}" if params && params.any?
    payload = nil
  else
    payload = uri_encode(params)
  end

  request_opts.update(:headers => request_headers(api_token).update(headers),
                      :method => method, :open_timeout => 30,
                      :payload => payload, :url => url, :timeout => 80)

  begin
    response = execute_request(request_opts)
  rescue SocketError => e
    handle_restclient_error(e)
  rescue NoMethodError => e
    # Work around RestClient bug
    if e.message =~ /\WRequestFailed\W/
      e = Maestrano::API::Error::ConnectionError.new('Unexpected HTTP response code')
      handle_restclient_error(e)
    else
      raise
    end
  rescue RestClient::ExceptionWithResponse => e
    if rcode = e.http_code and rbody = e.http_body
      handle_api_error(rcode, rbody)
    else
      handle_restclient_error(e)
    end
  rescue RestClient::Exception, Errno::ECONNREFUSED => e
    handle_restclient_error(e)
  end

  [parse(response), api_token]
end