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

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

Overview

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

Client for invocation of REST uuAPI.

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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)



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 134

def initialize(base_url, service_path = nil)
  options = {}
  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?
    base_url = nil
    # Load path_prefix from configuration file.
    path_prefix = get_config_param(service_path, @@SERVER_PATH_PARAM)
    path_prefix = self.class.default_path_prefix if path_prefix.nil?
  else
    path_prefix = ''
  end
  trust_hostname = get_config_param(service_path, @@TRUST_SERVER_HOSTNAME_PARAM)
  trust_cert = get_config_param(service_path, @@TRUST_SERVER_CERT_PARAM)
  if trust_hostname.nil? && trust_cert.nil?
    # Due to backward compatibility reasons, in case parameters are not defined,
    # SSL validation should be disabled (so unlike base URL and SSL version we
    # cannot leave configuration on CommandClient but need to force it).
    options[:validate_ssl] = false
  elsif trust_hostname == 'false' && trust_cert == 'false'
    options[:validate_ssl] = true
  else
    options[:validate_ssl] = false
  end
  options[:default_action] = :/'
  options[:session_handler] = self
  options[:command_handlers] = self
  options[:main_object_uri_parameter_name] = :uesuri
  options[:parameters_parameter_name] = ''
  options[:base_url] = base_url
  options[:default_base_url] = @@DEFAULT_BASE_URL
  options[:use_legacy_config] = true
  options[:append_routing_data] = false
  @base_path = concat_path(path_prefix, service_path)
  @client = UU::OS::CMD::CommandClient.new(@base_path, nil, options)
  @client.unwrap.set_header(:accept, @@DEFAULT_CONTENT_TYPE)
  @client.unwrap.set_header(:content-type', @@DEFAULT_CONTENT_TYPE)
  @default_query = UU::OS::HTTP::HTTPQuery.new
  self.timeout = @@DEFAULT_TIMEOUT
end

Class Method Details

+ (String) default_base_url

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



91
92
93
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 91

def self.default_base_url
  @@DEFAULT_BASE_URL
end

+ (Object) default_base_url=(base_url)

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



101
102
103
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 101

def self.default_base_url=(base_url)
  @@DEFAULT_BASE_URL = base_url
end

+ (String) default_path_prefix

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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

Returns:

  • (String)

    Default path prefix



111
112
113
114
115
116
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 111

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

+ (Object) default_path_prefix=(path_prefix)

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



124
125
126
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 124

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

Instance Method Details

- (Object) add_header(name, value)

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



192
193
194
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 192

def add_header(name, value)
  @client.unwrap.set_header(name, value)
end

- (Object) add_parameter(name, value)

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



202
203
204
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 202

def add_parameter(name, value)
  @default_query[name] = value
end

- (Object) auth_token=(token)

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



182
183
184
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 182

def auth_token=(token)
  @auth_token = token
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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)



427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 427

def delete(method, uesuri = nil)
  # Delete is not implemented in CommandClient, so we have to use HTTPClient
  http_client = @client.unwrap
  delete = http_client.prepare_delete(@base_path, method)
  delete.parameters[:uesuri] = UU::OS::UESURI.new(uesuri)
  delete.parameters.merge!(@default_query)
  delete.headers[:authorization] = @auth_token || UU::OS::Security::Session.send(:get_authn_token)
  if !UU::OS::Env::Process.active_process_uri.nil?
    delete.headers[@@PROCESS_HEADER_PARAM] = UU::OS::Env::Process.active_process_uri.to_s
  end
  response = delete.execute
  return @client.wrap_result(response, :invocation_method => :delete, :main_object_uri_parameter_name => :uesuri).get
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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)



349
350
351
352
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 349

def get(method, uesuri = nil)
  params = {:invocation_method => :get}
  return @client.invoke(method, UU::OS::UESURI.new(uesuri), params)
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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 stream 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)



365
366
367
368
369
370
371
372
373
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 365

def get_binary(method, uesuri = nil)
  params = {:invocation_method => :get, :stream_response => true, :binary_response => true}
  if @client.unwrap.instance_variable_get(:@headers)[:accept][0] == @@DEFAULT_CONTENT_TYPE
    # RemoteClient allowed to reconfigure Accept header for whole instance
    # (probably never used but we need it for backward compatibility).
    params[:accept] = @@STREAM_CONTENT_TYPE
  end
  return @client.invoke(method, UU::OS::UESURI.new(uesuri), params)
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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)



384
385
386
387
388
389
390
391
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 384

def post(method, uesuri = nil, data = nil)
  params = {:invocation_method => :post}
  if data.kind_of?Hash
    params[:multipart_upload] = true
  end
  params[:parameters] = data
  return @client.invoke(method, UU::OS::UESURI.new(uesuri), params)
end

- (BinaryValue) post_binary(method, uesuri = nil, data = nil)

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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

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:

  • (BinaryValue)

    Binary value with response body stream (or throws exception)



405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 405

def post_binary(method, uesuri = nil, data = nil)
  params = {:invocation_method => :post, :stream_response => true, :binary_response => true}
  if data.kind_of?Hash
    params[:multipart_upload] = true
  end
  params[:parameters] = data
  if @client.unwrap.instance_variable_get(:@headers)[:accept][0] == @@DEFAULT_CONTENT_TYPE
    # RemoteClient allowed to reconfigure Accept header for whole instance
    # (probably never used but we need it for backward compatibility).
    params[:accept] = @@STREAM_CONTENT_TYPE
  end
  return @client.invoke(method, UU::OS::UESURI.new(uesuri), params)
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



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

def raw_delete(method, uesuri = nil)
  # Delete is not implemented in CommandClient, so we have to use HTTPClient
  http_client = @client.unwrap
  delete = http_client.prepare_delete(@base_path, method)
  delete.parameters[:uesuri] = UU::OS::UESURI.new(uesuri)
  delete.parameters.merge!(@default_query)
  delete.headers[:authorization] = @auth_token || UU::OS::Security::Session.send(:get_authn_token)
  if !UU::OS::Env::Process.active_process_uri.nil?
    delete.headers[@@PROCESS_HEADER_PARAM] = UU::OS::Env::Process.active_process_uri.to_s
  end
  response = delete.execute
  return [response.status, response.body, transform_headers(response.headers)]
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



233
234
235
236
237
238
239
240
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 233

def raw_get(method, uesuri = nil)
  params = {:invocation_method => :get, :raw_response => true}
  response = @client.invoke(method, UU::OS::UESURI.new(uesuri), params).unwrap
  body = response.body
  body = body.read if body.respond_to?(:read)
  body.force_encoding(@@DEFAULT_ENCODING)
  return [response.status, body, transform_headers(response.headers)]
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

Invokes HTTP GET method without touching the incoming response. Response body is handed as binary read only stream. Caller is responsible for closing this stream 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



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

def raw_get_binary(method, uesuri = nil)
  params = {:invocation_method => :get, :raw_response => true, :stream_response => true, :binary_response => true}
  if @client.unwrap.instance_variable_get(:@headers)[:accept][0] == @@DEFAULT_CONTENT_TYPE
    # RemoteClient allowed to reconfigure Accept header for whole instance
    # (probably never used but we need it for backward compatibility).
    params[:accept] = @@STREAM_CONTENT_TYPE
  end
  response = @client.invoke(method, UU::OS::UESURI.new(uesuri), params).unwrap
  # Some commands rely on ability to rewind stream of downloaded
  # binary data (rest-client used tempfiles so this was possible).
  # Therefore to maintain backward compatibility, we also have to
  # also create tempfiles.
  return [response.status, TempfileIO.new(response.body), transform_headers(response.headers)]
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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



276
277
278
279
280
281
282
283
284
285
286
287
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 276

def raw_post(method, uesuri = nil, data = nil)
  params = {:invocation_method => :post, :raw_response => true}
  if data.kind_of?Hash
    params[:multipart_upload] = true
  end
  params[:parameters] = data
  response = @client.invoke(method, UU::OS::UESURI.new(uesuri), params).unwrap
  body = response.body
  body = body.read if body.respond_to?(:read)
  body.force_encoding(@@DEFAULT_ENCODING)
  return [response.status, body, transform_headers(response.headers)]
end

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

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

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

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, IO, Hash>)

    response status code, response body, response headers



300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 300

def raw_post_binary(method, uesuri = nil, data = nil)
  params = {:invocation_method => :post, :raw_response => true, :stream_response => true, :binary_response => true}
  if data.kind_of?Hash
    params[:multipart_upload] = true
  end
  if @client.unwrap.instance_variable_get(:@headers)[:accept][0] == @@DEFAULT_CONTENT_TYPE
    # RemoteClient allowed to reconfigure Accept header for whole instance
    # (probably never used but we need it for backward compatibility).
    params[:accept] = @@STREAM_CONTENT_TYPE
  end
  params[:parameters] = data
  response = @client.invoke(method, UU::OS::UESURI.new(uesuri), params).unwrap
  # Some commands rely on ability to rewind stream of downloaded
  # binary data (rest-client used tempfiles so this was possible).
  # Therefore to maintain backward compatibility, we also have to
  # also create tempfiles.
  return [response.status, TempfileIO.new(response.body), transform_headers(response.headers)]
end

- (Object) timeout=(timeout)

Deprecated.

To be used only for commands v1. For commands v2 use UU::OS::CMD::CommandClient

Sets request timeout (default value is 120 seconds)

Parameters:

  • timeout (Numeric)

    Request timeout (-1 for infinite timeout)



211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'uu_os_framework-0.29.16/lib/uu/os/rest/remote_client.rb', line 211

def timeout=(timeout)
  if timeout < 0
    # HTTPClient uses zero for infinite timeout
    tval = 0
  elsif timeout == 0
    # therefore zero needs to be translated to minimal timeout
    tval = 0.001
  else
    tval = timeout
  end
  @client.unwrap.connect_timeout = tval
  @client.unwrap.request_timeout = tval
end