Class: RHC::Rest::Api

Inherits:
Base show all
Includes:
Helpers
Defined in:
lib/rhc/rest/api.rb

Direct Known Subclasses

Mock::MockRestApi

Constant Summary

Constants included from Helpers

Helpers::BOUND_WARNING, Helpers::PREFIX, Helpers::ROLES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#agree, #certificate_file, #client_from_options, #collect_env_vars, #color, #confirm_action, #date, #datetime_rfc3339, #debug, #debug?, #debug_error, #decode_json, #deprecated, #deprecated_command, #disable_deprecated?, #distance_of_time_in_words, #env_var_regex_pattern, #error, #exec, #host_exists?, #hosts_file_contains?, #human_size, #info, #interactive?, #jruby?, #mac?, #openshift_online_server?, #openshift_rest_endpoint, #openshift_server, #openshift_url, #pluralize, #results, #role_name, #run_with_tee, #ssh_string, #ssh_string_parts, #ssl_options, #success, #system_path, #table_heading, #to_host, #to_uri, #token_for_user, #unix?, #user_agent, #warn, #windows?, #with_tolerant_encoding

Methods included from OutputHelpers

#default_display_env_var, #display_app, #display_app_configurations, #display_authorization, #display_cart, #display_cart_storage_info, #display_cart_storage_list, #display_deployment, #display_deployment_list, #display_domain, #display_env_var_list, #display_key, #display_team, #format_cart_gears, #format_cart_header, #format_gear_info, #format_key_header, #format_scaling_info, #format_usage_message

Methods inherited from Base

#add_message, #has_param?, #link_href, #links, #rest_method, #supports?

Methods included from AttributesClass

#define_attr, #model_name

Methods included from Attributes

#attribute, #attributes, #attributes=, #clear_attribute

Constructor Details

#initialize(client, preferred_api_versions = []) ⇒ Api

Returns a new instance of Api.



6
7
8
9
10
11
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
# File 'lib/rhc/rest/api.rb', line 6

def initialize(client, preferred_api_versions=[])
  super(nil, client)

  # API version negotiation
  @server_api_versions = []
  debug "Client supports API versions #{preferred_api_versions.join(', ')}"
  @client_api_versions = preferred_api_versions
  @server_api_versions, @current_api_version, links = api_info({
    :url => client.url,
    :method => :get,
    :accept => :json,
    :lazy_auth => true,
  })
  debug "Server supports API versions #{@server_api_versions.join(', ')}"

  if api_version_negotiated
    debug "   Using API version #{api_version_negotiated}"
    unless client_api_version_current?
      debug "Client API version #{api_version_negotiated} is not current. Refetching API"
      # need to re-fetch API
      @server_api_versions, @current_api_version, links = api_info({
        :url => client.url,
        :method => :get,
        :accept => :json,
        :api_version => api_version_negotiated,
        :lazy_auth => true,
      })
    end
  else
    warn_about_api_versions
  end

  attributes['links'] = links

rescue RHC::Rest::ResourceNotFoundException => e
  raise ApiEndpointNotFound.new(
    "The OpenShift server is not responding correctly.  Check "\
    "that '#{client.url}' is the correct URL for your server. "\
    "The server may be offline or misconfigured.")
end

Instance Attribute Details

#client_api_versionsObject (readonly)

Returns the value of attribute client_api_versions.



4
5
6
# File 'lib/rhc/rest/api.rb', line 4

def client_api_versions
  @client_api_versions
end

#server_api_versionsObject (readonly)

Returns the value of attribute server_api_versions.



4
5
6
# File 'lib/rhc/rest/api.rb', line 4

def server_api_versions
  @server_api_versions
end

Instance Method Details

#api_version_match?Boolean

API version related methods

Returns:

  • (Boolean)


48
49
50
# File 'lib/rhc/rest/api.rb', line 48

def api_version_match?
  ! api_version_negotiated.nil?
end

#api_version_negotiatedObject

return the API version that the server and this client can agree on



53
54
55
56
# File 'lib/rhc/rest/api.rb', line 53

def api_version_negotiated
  client_api_versions.reverse. # choose the last API version listed
    detect { |v| @server_api_versions.include? v }
end

#client_api_version_current?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/rhc/rest/api.rb', line 58

def client_api_version_current?
  current_api_version == api_version_negotiated
end

#current_api_versionObject



62
63
64
# File 'lib/rhc/rest/api.rb', line 62

def current_api_version
  @current_api_version
end

#warn_about_api_versionsObject



66
67
68
69
70
71
72
# File 'lib/rhc/rest/api.rb', line 66

def warn_about_api_versions
  if !api_version_match?
    warn "WARNING: API version mismatch. This client supports #{client_api_versions.join(', ')} but
server at #{URI.parse(client.url).host} supports #{@server_api_versions.join(', ')}."
    warn "The client version may be outdated; please consider updating 'app'. We will continue, but you may encounter problems."
  end
end