Class: UU::OS::REST::RemoteClient

Inherits:
Object
  • Object
show all
Defined in:
uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb

Overview

Client for invocation of REST uuAPI.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (RemoteClient) initialize(base_url, service_path = nil)

Creates new instance of REST client.

Parameters:

  • base_url (String, Class)

    URL of UU server or Service class (URL will be read from configuration based on naming conventions)

  • service_path (String) (defaults to: nil)

    Service path (may be nil in case base_url is service class with defined PATH constant)



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 249

def initialize(base_url, service_path = nil)
  if (base_url.kind_of?Class) || (base_url.kind_of?Module)
    @service = base_url
    # Get service path
    service_path = @service::PATH if service_path.nil?
    # Load base_url and path_prefix from configuration file.
    base_url = get_config_param(service_path, @@SERVER_URL_PARAM)
    base_url = RemoteClient.default_base_url if base_url.nil?
    path_prefix = get_config_param(service_path, @@SERVER_PATH_PARAM)
    path_prefix = RemoteClient.default_path_prefix if path_prefix.nil?
    url = build_service_url(base_url, path_prefix, service_path)
  else
    url = build_service_url(base_url, nil, service_path)
  end
  @resource = RestClient::Resource.new url
  @resource.options[:timeout] = @@DEFAULT_TIMEOUT
  @resource.options[:ssl_version] = get_config_param(service_path, @@SERVER_SSL_VERSION_PARAM)
  @params = {:accept => @@DEFAULT_CONTENT_TYPE, :content-type' => @@DEFAULT_CONTENT_TYPE, :params => {}}
end

Class Method Details

+ (String) default_base_url

Returns default base URL (this URL is used in case no URL is found in configuration file for a particular service application or system).

Returns:

  • (String)

    Default base URL



211
212
213
214
215
216
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 211

def self.default_base_url
  if !Thread.current[@@BASE_URL_PARAM]
    RemoteClient.default_base_url=@@DEFAULT_BASE_URL
  end
  Thread.current[@@BASE_URL_PARAM]
end

+ (Object) default_base_url=(base_url)

Stores default base URL (this URL is used in case no URL is found in configuration file for a particular service application or system).

Parameters:

  • base_url (String)

    Default base URL



222
223
224
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 222

def self.default_base_url=(base_url)
  Thread.current[@@BASE_URL_PARAM] = base_url
end

+ (String) default_path_prefix

Returns default path prefix to be injected between default_base_url and PATH constant defined on UU service classes.

Returns:

  • (String)

    Default path prefix



230
231
232
233
234
235
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 230

def self.default_path_prefix
  if !Thread.current[@@PATH_PREFIX_PARAM]
    RemoteClient.default_path_prefix=@@DEFAULT_PATH_PREFIX
  end
  Thread.current[@@PATH_PREFIX_PARAM]
end

+ (Object) default_path_prefix=(path_prefix)

Stores default path prefix to be injected between default_base_url and PATH constant defined on UU service classes.

Parameters:

  • path_prefix (String)

    Default path prefix



241
242
243
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 241

def self.default_path_prefix=(path_prefix)
  Thread.current[@@PATH_PREFIX_PARAM] = path_prefix
end

Instance Method Details

- (Object) add_header(name, value)

Adds HTTP header. This header will be used in request for each command invoked with this client instance.

Parameters:

  • name (String, Symbol)

    Name of header

  • value (String)

    Header value



281
282
283
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 281

def add_header(name, value)
  @params[name.to_s.downcase.to_sym] = value
end

- (Object) add_parameter(name, value)

Adds HTTP query parameter. This parameter will be used in request for each command invoked with this client instance.

Parameters:

  • name (String, Symbol)

    Name of parameter

  • value (String)

    Parameter value



289
290
291
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 289

def add_parameter(name, value)
  @params[:params][name.to_sym] = value
end

- (Object) auth_token=(token)

Sets HTTP authorization token to use for authentication and authorization. This token will be used in request header for each command invoked with this client instance.

Parameters:

  • token (String)

    HTTP authorization token



273
274
275
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 273

def auth_token=(token)
  @params[:authorization] = token
end

- (String) delete(method, uesuri = nil)

Invokes HTTP DELETE and checks incoming response for errors.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

Returns:

  • (String)

    Response body (or throws exception)



454
455
456
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 454

def delete(method, uesuri = nil)
  process_result(raw_delete(method, uesuri))
end

- (String) get(method, uesuri = nil)

Invokes HTTP GET and checks incoming response for errors.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

Returns:

  • (String)

    Response body (or throws exception)



394
395
396
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 394

def get(method, uesuri = nil)
  process_result(raw_get(method, uesuri))
end

- (BinaryValue) get_binary(method, uesuri = nil)

Invokes HTTP GET and checks incoming response for errors. Response body is handed as binary value containing response body stream. Caller is responsible for closing this file after it is processed.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

Returns:

  • (BinaryValue)

    Binary value with response body stream (or throws exception)



407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 407

def get_binary(method, uesuri = nil)
  response = raw_get_binary(method, uesuri)
  process_result(response)
  # Get binary data info from header which should be send along with stream from server.
  info = response[2][:uu_os_binary_data_info]
  if ((info.nil?) && (response[1].size == 0))
    return nil
  end
  info = Base64.decode64(info).force_encoding(@@DEFAULT_ENCODING) if info
  result = UU::OS::REST::BinaryValue.new(info, true)
  if result.name.nil?
    # If name was not provided via meta-data header, try to read it from regular header
    cdh = response[2][:content_disposition]
    result.name = cdh.split('filename=')[1].split(';')[0] if cdh && cdh['filename=']
    result.name = result.name.split('"')[1] if result.name && result.name[/^".*"$/]
  end
  if result.content_type.nil?
    # If content-type was not provided via meta-data header, try to read it from regular header
    cth = response[2][:content_type]
    result.content_type = cth if cth
  end
  if result.size.nil?
    # If size was not provided via meta-data header, try to read it from regular header
    clh = response[2][:content_length]
    result.size = clh.to_i if clh
  end
  result.data = response[1]
  return result
end

- (String) post(method, uesuri = nil, data = nil)

Invokes HTTP POST and checks incoming response for errors.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

  • data (String, Hash) (defaults to: nil)

    Data payload (optional; Hash object will be sent as multipart/form-data)

Returns:

  • (String)

    Response body (or throws exception)



444
445
446
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 444

def post(method, uesuri = nil, data = nil)
  process_result(raw_post(method, uesuri, data))
end

- (Array<Numeric, String, Hash>) raw_delete(method, uesuri = nil)

Invokes HTTP DELETE method without touching the incoming response.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

Returns:

  • (Array<Numeric, String, Hash>)

    response status code, response body, response headers



375
376
377
378
379
380
381
382
383
384
385
386
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 375

def raw_delete(method, uesuri = nil)
  @params[:params][:uesuri] = UU::OS::UESURI.new(uesuri)
  if !@params[:authorization]
    @params[:authorization] = UU::OS::Security::Session.send(:get_authn_token)
  end
  if (!@params[@@PROCESS_HEADER_PARAM]) && (UU::OS::Env::Process.active_process_uri)
    @params[@@PROCESS_HEADER_PARAM] = UU::OS::Env::Process.active_process_uri.to_s
  end
  @resource[method].delete(@params) do |response, request, result|
    [response.code, response.body ? response.body.force_encoding(@@DEFAULT_ENCODING) : nil, response.headers]
  end
end

- (Array<Numeric, String, Hash>) raw_get(method, uesuri = nil)

Invokes HTTP GET method without touching the incoming response.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

Returns:

  • (Array<Numeric, String, Hash>)

    response status code, response body, response headers



306
307
308
309
310
311
312
313
314
315
316
317
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 306

def raw_get(method, uesuri = nil)
  @params[:params][:uesuri] = UU::OS::UESURI.new(uesuri)
  if !@params[:authorization]
    @params[:authorization] = UU::OS::Security::Session.send(:get_authn_token)
  end
  if (!@params[@@PROCESS_HEADER_PARAM]) && (UU::OS::Env::Process.active_process_uri)
    @params[@@PROCESS_HEADER_PARAM] = UU::OS::Env::Process.active_process_uri.to_s
  end
  @resource[method].get(@params) do |response, request, result|
    [response.code, response.body ? response.body.force_encoding(@@DEFAULT_ENCODING) : nil, response.headers]
  end
end

- (Array<Numeric, IO, Hash>) raw_get_binary(method, uesuri = nil)

Invokes HTTP GET method without touching the incoming response. Response body is handed as binary read only stream. Caller is responsible for closing this file after it is processed.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

Returns:

  • (Array<Numeric, IO, Hash>)

    response status code, response body, response headers



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 327

def raw_get_binary(method, uesuri = nil)
  @params[:params][:uesuri] = UU::OS::UESURI.new(uesuri)
  if !@params[:authorization]
    @params[:authorization] = UU::OS::Security::Session.send(:get_authn_token)
  end
  if (!@params[@@PROCESS_HEADER_PARAM]) && (UU::OS::Env::Process.active_process_uri)
    @params[@@PROCESS_HEADER_PARAM] = UU::OS::Env::Process.active_process_uri.to_s
  end
  # Use default streaming content type if default content type value was not overridden.
  @params[:accept] = @@STREAM_CONTENT_TYPE if @params[:accept] == @@DEFAULT_CONTENT_TYPE
  @resource.options[:raw_response] = true
  @resource[method].get(@params) do |response, request, result|
    # Return response body as raw binary read only stream
    stream = TempfileIO.new(response.file)
    [response.code, stream, response.headers]
  end
end

- (Array<Numeric, String, Hash>) raw_post(method, uesuri = nil, data = nil)

Invokes HTTP POST method without touching the incoming response.

Parameters:

  • method (String, Symbol)

    Service method to invoke

  • uesuri (String, UU::OS::UESURI) (defaults to: nil)

    Entity URI (optional)

  • data (String, Hash) (defaults to: nil)

    Data payload (optional; Hash object will be sent as multipart/form-data)

Returns:

  • (Array<Numeric, String, Hash>)

    response status code, response body, response headers



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 352

def raw_post(method, uesuri = nil, data = nil)
  @params[:params][:uesuri] = UU::OS::UESURI.new(uesuri)
  if !@params[:authorization]
    @params[:authorization] = UU::OS::Security::Session.send(:get_authn_token)
  end
  if (!@params[@@PROCESS_HEADER_PARAM]) && (UU::OS::Env::Process.active_process_uri)
    @params[@@PROCESS_HEADER_PARAM] = UU::OS::Env::Process.active_process_uri.to_s
  end
  if data.kind_of?Hash
    @params[:content-type'] = @@MULTIPART_CONTENT_TYPE
    data[:multipart] = true
  end
  @resource[method].post(data, @params)  do |response, request, result|
    [response.code, response.body ? response.body.force_encoding(@@DEFAULT_ENCODING) : nil, response.headers]
  end
end

- (Object) timeout=(timeout)

Sets request timeout (default value is 60 seconds)

Parameters:

  • timeout (Numeric)

    Request timeout (-1 for infinite timeout)



296
297
298
# File 'uu_os_framework-0.10.6/lib/uu/os/rest/remote_client.rb', line 296

def timeout=(timeout)
  @resource.options[:timeout] = timeout
end