Module: Occi::Api::Client::Http::Helpers

Included in:
ClientHttp
Defined in:
lib/occi/api/client/http/helpers.rb

Instance Method Summary collapse

Instance Method Details

#configure_connection(options) ⇒ Object

See Also:


15
16
17
18
# File 'lib/occi/api/client/http/helpers.rb', line 15

def configure_connection(options)
  # timeout is the only global connection option at the moment
  self.class.default_timeout options[:timeout].to_i unless options[:timeout].blank?
end

#get_auth(auth_options, fallback = false) ⇒ Object

See Also:


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/occi/api/client/http/helpers.rb', line 21

def get_auth(auth_options, fallback = false)
  # select appropriate authN type
  case auth_options[:type]
  when "basic"
    @authn_plugin = Http::AuthnPlugins::Basic.new self, auth_options
  when "digest"
    @authn_plugin = Http::AuthnPlugins::Digest.new self, auth_options
  when "x509"
    @authn_plugin = Http::AuthnPlugins::X509.new self, auth_options
  when "keystone"
    raise ::Occi::Api::Client::Errors::AuthnError, "This authN method is for fallback only!" unless fallback
    @authn_plugin = Http::AuthnPlugins::Keystone.new self, auth_options
  when "none", nil
    @authn_plugin = Http::AuthnPlugins::Dummy.new self
  else
    raise ::Occi::Api::Client::Errors::AuthnError, "Unknown authN method [#{@auth_options[:type]}]!"
  end

  @authn_plugin.setup

  auth_options
end

#get_logger(log_options) ⇒ Object

See Also:


7
8
9
10
11
12
# File 'lib/occi/api/client/http/helpers.rb', line 7

def get_logger(log_options)
  logger = super(log_options)
  self.class.debug_output $stderr if logger.level == Occi::Api::Log::DEBUG

  logger
end

#get_media_type(force_type = nil) ⇒ Object

See Also:


65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/occi/api/client/http/helpers.rb', line 65

def get_media_type(force_type = nil)
  # force media_type if provided
  unless force_type.blank?
    self.class.headers 'Accept' => force_type
    media_type = force_type
  else
    media_types = self.class.head(@endpoint.to_s).headers['accept']

    Occi::Api::Log.debug("Available media types: #{media_types.inspect}")
    media_type = case media_types
    when /application\/occi\+json/
      'application/occi+json'
    when /text\/occi/
      'text/occi'
    else
      'text/plain'
    end
  end

  media_type
end

#preauthenticateObject

See Also:


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/occi/api/client/http/helpers.rb', line 45

def preauthenticate
  begin
    @authn_plugin.authenticate
  rescue ::Occi::Api::Client::Errors::AuthnError => e
    Occi::Api::Log.debug e.message

    if @authn_plugin.fallbacks.any?
      # TODO: multiple fallbacks
      @auth_options[:original_type] = @auth_options[:type]
      @auth_options[:type] = @authn_plugin.fallbacks.first

      @auth_options = get_auth(@auth_options, true)
      @authn_plugin.authenticate
    else
      raise e
    end
  end
end

#response_message(response) ⇒ String

Generates a human-readable response message based on the HTTP response code.

Examples:

response_message self.class.delete(path)
 # =>  'HTTP Response status: [200] OK'

Parameters:

  • response (HTTParty::Response)

    HTTParty response object

Returns:

  • (String)

    message


95
96
97
98
# File 'lib/occi/api/client/http/helpers.rb', line 95

def response_message(response)
  @last_response = response
  "HTTP Response status: [#{response.code.to_s}] #{reason_phrase(response.code)}"
end