Class: Angus::Remote::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/angus/remote/client.rb

Overview

A client for service invocation

Instance Method Summary collapse

Constructor Details

#initialize(api_url, timeout = nil, options = {}) ⇒ Client

Returns a new instance of Client.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/angus/remote/client.rb', line 15

def initialize(api_url, timeout = nil, options = {})
  api_url = api_url[0..-2] if api_url[-1] == '/'

  @connection = PersistentHTTP.new(
    :pool_size    => 10,
    :pool_timeout => 10,
    :warn_timeout => 0.25,
    :force_retry  => false,
    :url          => api_url,

    :read_timeout => timeout,
    :open_timeout => timeout
  )

  @api_base_path = @connection.default_path

  store_namespace = "#{options['code_name']}.#{options['version']}"
  client_settings = { :public_key => options['public_key'],
                      :private_key => options['private_key'],
                      :service_id => store_namespace,
                      :store => Settings.redis.merge({ :namespace =>  store_namespace }) }

  @authentication_client = Authentication::Client.new(client_settings)
end

Instance Method Details

#make_request(path, method, encode_as_json, path_params, request_params) ⇒ Net::HTTPResponse

Makes a request to the service

Parameters:

  • path (String)

    The operation URL path. It can have place holders, ex: /user/:user_id/profile

  • method (String)

    The http method for the request: get, post, put, delete

  • encode_as_json (String)

    If true, the request params are encoded as json in the request body

  • path_params (String)

    Params that go into the path. This is an array, the first element in the array goes in the first path placeholder.

  • request_params (String)

    Params that go as url params or as data encoded in the body.

Returns:

  • (Net::HTTPResponse)

    The remote service response.

Raises:

  • (RemoteSevereError)

    When the remote response status code is of severe error. see Utils.severe_error_response?

  • (RemoteConnectionError)

    When the remote service refuses the connection.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/angus/remote/client.rb', line 57

def make_request(path, method, encode_as_json, path_params, request_params)
  path = @api_base_path + Utils.build_path(path, path_params)

  request = Utils.build_request(method, path, request_params, encode_as_json)

  begin
    @authentication_client.prepare_request(request, method.upcase, path)

    response = @connection.request(request)

    @authentication_client.store_session_private_key(response)

    if Utils.severe_error_response?(response)
      raise RemoteSevereError.new(get_error_messages(response.body))
    end

    response
  rescue Errno::ECONNREFUSED, PersistentHTTP::Error => e
    raise RemoteConnectionError.new("#@api_base_path - #{e.class}: #{e.message}")
  end
end

#to_sObject



79
80
81
# File 'lib/angus/remote/client.rb', line 79

def to_s
  "#<#{self.class}:#{object_id}>"
end