Class: IBMCloudSdkCore::BaseService

Inherits:
Object
  • Object
show all
Defined in:
lib/ibm_cloud_sdk_core/base_service.rb

Overview

Class for interacting with the API

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vars) ⇒ BaseService

Returns a new instance of BaseService.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 28

def initialize(vars)
  defaults = {
    authenticator: nil,
    disable_ssl_verification: false,
    service_name: nil
  }
  vars = defaults.merge(vars)
  @service_url = vars[:service_url]
  @authenticator = vars[:authenticator]
  @disable_ssl_verification = vars[:disable_ssl_verification]
  @service_name = vars[:service_name]

  raise ArgumentError.new("authenticator must be provided") if @authenticator.nil?

  @conn = HTTP::Client.new(
    headers: {}
  ).use normalize_uri: { normalizer: NORMALIZER }
  configure_service(@service_name)
  @temp_headers = {}
end

Instance Attribute Details

#authenticatorObject (readonly)

Returns the value of attribute authenticator.



27
28
29
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 27

def authenticator
  @authenticator
end

#connObject (readonly)

Returns the value of attribute conn.



27
28
29
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 27

def conn
  @conn
end

#disable_ssl_verificationObject

Returns the value of attribute disable_ssl_verification.



27
28
29
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 27

def disable_ssl_verification
  @disable_ssl_verification
end

#service_nameObject

Returns the value of attribute service_name.



26
27
28
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 26

def service_name
  @service_name
end

#service_urlObject

Returns the value of attribute service_url.



26
27
28
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 26

def service_url
  @service_url
end

Instance Method Details

#add_default_headers(headers: {}) ⇒ Object

Raises:

  • (TypeError)


53
54
55
56
57
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 53

def add_default_headers(headers: {})
  raise TypeError unless headers.instance_of?(Hash)

  headers.each_pair { |k, v| @conn.default_options.headers.add(k, v) }
end

#configure_http_client(proxy: {}, timeout: {}, disable_ssl_verification: false) ⇒ Object

Sets the http client config, currently works with timeout and proxies

Parameters:

  • proxy (Hash) (defaults to: {})

    The hash of proxy configurations

  • timeout (Hash) (defaults to: {})

    The hash for configuring timeouts. ‘per_operation` has priority over `global`

  • disable_ssl_verification (Boolean) (defaults to: false)

    Disable the SSL verification (Note that this has serious security implications - only do this if you really mean to!)

Options Hash (proxy:):

  • address (String)

    The address of the proxy

  • port (Integer)

    The port of the proxy

  • username (String)

    The username of the proxy, if authentication is needed

  • password (String)

    The password of the proxy, if authentication is needed

  • headers (Hash)

    The headers to be used with the proxy

Options Hash (timeout:):

  • per_operation (Hash)

    Timeouts per operation. Requires ‘read`, `write`, `connect`

  • global (Integer)

    Upper bound on total request time



140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 140

def configure_http_client(proxy: {}, timeout: {}, disable_ssl_verification: false)
  raise TypeError("proxy parameter must be a Hash") unless proxy.empty? || proxy.instance_of?(Hash)

  raise TypeError("timeout parameter must be a Hash") unless timeout.empty? || timeout.instance_of?(Hash)

  @disable_ssl_verification = disable_ssl_verification
  if disable_ssl_verification
    ssl_context = OpenSSL::SSL::SSLContext.new
    ssl_context.verify_mode = OpenSSL::SSL::VERIFY_NONE
    @conn.default_options = { ssl_context: ssl_context }
  end
  add_proxy(proxy) unless proxy.empty? || !proxy.dig(:address).is_a?(String) || !proxy.dig(:port).is_a?(Integer)
  add_timeout(timeout) unless timeout.empty? || (!timeout.key?(:per_operation) && !timeout.key?(:global))
end

#configure_service(service_name) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 59

def configure_service(service_name)
  config = get_service_properties(service_name) if service_name

  @service_url = config[:url] unless config.nil? || config[:url].nil?
  disable_ssl_verification = explicitly_true(config[:disable_ssl]) unless config.nil? || config[:disable_ssl].nil?
  # configure the http client if ssl is disabled
  configure_http_client(disable_ssl_verification: disable_ssl_verification) if disable_ssl_verification
end

#headers(headers) ⇒ self

Note:

Chainable

Parameters:

  • headers (Hash)

    Custom headers to be sent with the request

Returns:

  • (self)


121
122
123
124
125
126
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 121

def headers(headers)
  raise TypeError("Expected Hash type, received #{headers.class}") unless headers.instance_of?(Hash)

  @temp_headers = headers
  self
end

#request(args) ⇒ DetailedResponse

Returns:



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ibm_cloud_sdk_core/base_service.rb', line 69

def request(args)
  defaults = { method: nil, url: nil, accept_json: false, headers: nil, params: nil, json: {}, data: nil }
  args = defaults.merge(args)
  args[:data].delete_if { |_k, v| v.nil? } if args[:data].instance_of?(Hash)
  args[:json] = args[:data].merge(args[:json]) if args[:data].respond_to?(:merge)
  args[:json] = args[:data] if args[:json].empty? || (args[:data].instance_of?(String) && !args[:data].empty?)
  args[:json].delete_if { |_k, v| v.nil? } if args[:json].instance_of?(Hash)
  args[:headers]["Accept"] = "application/json" if args[:accept_json] && args[:headers]["Accept"].nil?
  args[:headers]["Content-Type"] = "application/json" unless args[:headers].key?("Content-Type")
  args[:json] = args[:json].to_json if args[:json].instance_of?(Hash)
  args[:headers].delete_if { |_k, v| v.nil? } if args[:headers].instance_of?(Hash)
  args[:params].delete_if { |_k, v| v.nil? } if args[:params].instance_of?(Hash)
  args[:form].delete_if { |_k, v| v.nil? } if args.key?(:form)
  args.delete_if { |_, v| v.nil? }
  args[:headers].delete("Content-Type") if args.key?(:form) || args[:json].nil?

  conn = @conn

  @authenticator.authenticate(args[:headers])
  args[:headers] = args[:headers].merge(@temp_headers) unless @temp_headers.nil?
  @temp_headers = {} unless @temp_headers.nil?

  raise ArgumentError.new("service_url must be provided") if @service_url.nil?
  raise ArgumentError.new('The service_url shouldn\'t start or end with curly brackets or quotes. Be sure to remove any {} and \" characters surrounding your username') if check_bad_first_or_last_char(@service_url)

  if args.key?(:form)
    response = conn.follow.request(
      args[:method],
      HTTP::URI.parse(@service_url + args[:url]),
      headers: conn.default_options.headers.merge(HTTP::Headers.coerce(args[:headers])),
      params: args[:params],
      form: args[:form]
    )
  else
    response = conn.follow.request(
      args[:method],
      HTTP::URI.parse(@service_url + args[:url]),
      headers: conn.default_options.headers.merge(HTTP::Headers.coerce(args[:headers])),
      body: args[:json],
      params: args[:params]
    )
  end
  return DetailedResponse.new(response: response) if (200..299).cover?(response.code)

  raise ApiException.new(response: response)
rescue OpenSSL::SSL::SSLError
  raise StandardError.new("The connection failed because the SSL certificate is not valid. To use a self-signed certificate, set the disable_ssl_verification parameter in configure_http_client.")
end