Class: UU::OS::HTTP::HTTPClient

Inherits:
Object
  • Object
show all
Defined in:
uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb

Overview

HTTPClient is used for execution of common HTTP requests.

Examples:

Basic usage:

client = UU::OS::HTTP::HTTPClient.new('https://api.plus4u.net', 'c00', 'uu/os/Artifact')
get = client.prepare_get('getAttributes')
get.headers[:authorization] = 'Basic ...'
get.headers[:accept] = 'application/json'
result = get.execute
attributes_json = result.body

Constant Summary

Instance Method Summary (collapse)

Constructor Details

- (HTTPClient) initialize(base_url, *path)

Creates new instance of HTTPClient for given base URL and optional path.

Parameters:

  • base_url (String)

    Base URL to connect in format protocol://host[:port].

  • path (Array<String>)

    Optional path to be appended to base URL.



140
141
142
143
144
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 140

def initialize(base_url, *path)
  @base_url = base_url
  @path = concat_path(*path)
  @client = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args, &block) (private)

Allow to invoke other HTTP methods than POST and GET (needed e.g. for UDS which uses DELETE, but might be necessary for integration with thirdparty services).



442
443
444
445
446
447
448
449
450
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 442

def method_missing(method, *args, &block) #:nodoc:
  method_name = method.to_s
  if method_name =~ /^prepare_/
    method_type = method_name[8..-1].to_sym
    UU::OS::HTTP::HTTPMethod.new(self, concat_path(*args), method_type)
  else
    super
  end
end

Instance Method Details

- (Fixnum, Float) connect_timeout

Returns timeout for connection initialization.

Returns:

  • (Fixnum, Float)

    Connection timeout in seconds.



148
149
150
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 148

def connect_timeout
  client.connect_timeout
end

- (void) connect_timeout=(timeout)

This method returns an undefined value.

Sets timeout for connection initialization.

Parameters:

  • timeout (Fixnum, Float)

    Connection timeout in seconds.



155
156
157
158
159
160
161
162
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 155

def connect_timeout=(timeout)
  return if timeout.nil?
  # Do not create new instance if default value is not changed
  if client.connect_timeout != timeout
    @client = self.class.send(:__new_client__) if @client.nil?
    @client.connect_timeout = timeout
  end
end

- (void) keepalive_timeout=(timeout)

This method returns an undefined value.

Sets connection keepalive timeout (timeout within which are connections to same server reused). Defaults to 15 seconds.

Parameters:

  • timeout (Fixnum)

    Keepalive timeout (in seconds).



332
333
334
335
336
337
338
339
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 332

def keepalive_timeout=(timeout)
  return if timeout.nil?
  # Do not create new instance if default value is not changed
  if client.keep_alive_timeout != timeout.to_i
    @client = self.class.send(:__new_client__) if @client.nil?
    @client.keep_alive_timeout = timeout.to_i
  end
end

- (UU::OS::HTTP::HTTPGet) prepare_get(*path)

Prepares GET request for given optional path. In case path is not given request will be sent to URL defined by base_url and path given for HTTPClient initialization.

Parameters:

  • path (Array<String>)

    Optional path where to send request (value is appended to base_url and path given for HTTPClient initialization).

Returns:



367
368
369
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 367

def prepare_get(*path)
  UU::OS::HTTP::HTTPGet.new(self, concat_path(*path))
end

- (UU::OS::HTTP::HTTPPost) prepare_post(*path)

Prepares POST request for given optional path. In case path is not given request will be sent to URL defined by base_url and path given for HTTPClient initialization.

Parameters:

  • path (Array<String>)

    Optional path where to send request (value is appended to base_url and path given for HTTPClient initialization).

Returns:



376
377
378
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 376

def prepare_post(*path)
  UU::OS::HTTP::HTTPPost.new(self, concat_path(*path))
end

- (Fixnum, Float) request_timeout

Returns timeout in which request must be processed (sum of sending of request and receiving response).

Returns:

  • (Fixnum, Float)

    Request timeout in seconds.



167
168
169
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 167

def request_timeout
  client.receive_timeout
end

- (void) request_timeout=(timeout)

This method returns an undefined value.

Sets timeout in which request must be processed (sum of sending of request and receiving response).

Parameters:

  • timeout (Fixnum, Float)

    Request timeout in seconds.



175
176
177
178
179
180
181
182
183
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 175

def request_timeout=(timeout)
  return if timeout.nil?
  # Do not create new instance if default value is not changed
  if client.receive_timeout != timeout
    @client = self.class.send(:__new_client__) if @client.nil?
    @client.receive_timeout = timeout
    @client.send_timeout = timeout
  end
end

- (void) set_header(name, *value)

This method returns an undefined value.

Sets HTTP header to be used for each invoked request using this instance of HTTPClient.

Parameters:

  • name (Symbol, String)

    Header name (case insensitive).

  • value (Array<String>)

    Header value (multiple values can be given).



357
358
359
360
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 357

def set_header(name, *value)
  @headers = UU::OS::HTTP::HTTPHeaders.new(DEFAULT_HEADERS) if @headers.nil?
  @headers[name] = value
end

- (void) ssl_cert_store=(cert_store)

This method returns an undefined value.

Sets custom SSL certificate store to be able to modify default trusted CAs.

Parameters:

  • cert_store (String)

    Path to SSL certificate store. Path may point either to single file or directory containing multiple certificate files. PEM encoding is expected.



296
297
298
299
300
301
302
303
304
305
306
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 296

def ssl_cert_store=(cert_store)
  return if cert_store.nil?
  @client = self.class.send(:__new_client__) if @client.nil?
  store = OpenSSL::X509::Store.new
  if File.directory?(cert_store)
    store.add_path(cert_store)
  else
    store.add_file(cert_store)
  end
  client.ssl_config.cert_store = store
end

- (TrueClass, FalseClass) ssl_validation

Returns configuration of SSL validation.

Returns:

  • (TrueClass, FalseClass)

    Actual configuration.



218
219
220
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 218

def ssl_validation
  return client.ssl_config.verify_mode == SSL_VALIDATION_ENABLED
end

- (void) ssl_validation=(enabled)

This method returns an undefined value.

Enable or disable SSL validation. If enabled, server certificate is checked and exception is thrown in case it is not valid or is untrusted.

Parameters:

  • enabled (TrueClass, FalseClass)

    True to enable, False to disable.



226
227
228
229
230
231
232
233
234
235
236
237
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 226

def ssl_validation=(enabled)
  return if enabled.nil?
  # Do not create new instance if default value is not changed
  if (client.ssl_config.verify_mode == SSL_VALIDATION_ENABLED) != enabled
    @client = self.class.send(:__new_client__) if @client.nil?
    if enabled
      client.ssl_config.verify_mode = SSL_VALIDATION_ENABLED
    else
      client.ssl_config.verify_mode = SSL_VALIDATION_DISABLED
    end
  end
end

- (String, Symbol) ssl_version

Returns SSL version to be used for communication. Value :auto or nil means suitable version is automatically chosen based on negotiation with server.

Returns:

  • (String, Symbol)

    SSL version



261
262
263
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 261

def ssl_version
  return client.ssl_config.ssl_version
end

- (void) ssl_version=(version)

This method returns an undefined value.

Sets SSL version to be used for communication.

Parameters:

  • version (String, Symbol)

    Possible values are TLSv1_2, TLSv1_1, TLSv1, SSLv2, SSLv23, SSLv3 or :auto (or nil) to allow version negotiation.



269
270
271
272
273
274
275
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 269

def ssl_version=(version)
  # Do not create new instance if default value is not changed
  if (((client.ssl_config.ssl_version.nil?) || (client.ssl_config.ssl_version == :auto)) && !((version.nil?) || (version == :auto)))
    @client = self.class.send(:__new_client__) if @client.nil?
    @client.ssl_config.ssl_version = (version.nil? ? :auto : version)
  end
end

- (TrueClass, FalseClass) tranparent_gzip_decompression

Returns configuration of transparent GZip decompression.

Returns:

  • (TrueClass, FalseClass)

    Actual configuration.



187
188
189
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 187

def tranparent_gzip_decompression
  client.transparent_gzip_decompression
end

- (void) tranparent_gzip_decompression=(enabled)

This method returns an undefined value.

Enable or disable transparent GZip decompression. If enabled corresponding Accept header is automatically send with request.

Parameters:

  • enabled (TrueClass, FalseClass)

    True to enable, False to disable.



195
196
197
198
199
200
201
202
# File 'uu_os_connection-2.2.4/lib/uu/os/http/http_client.rb', line 195

def tranparent_gzip_decompression=(enabled)
  return if enabled.nil?
  # Do not create new instance if default value is not changed
  if client.gransparent_gzip_decompression != enabled
    @client = self.class.send(:__new_client__) if @client.nil?
    @client.transparent_gzip_decompression = enabled
  end
end