Class: Ingenico::Connect::SDK::DefaultImpl::DefaultConnection

Inherits:
PooledConnection show all
Defined in:
lib/ingenico/connect/sdk/defaultimpl/default_connection.rb

Constant Summary collapse

CONTENT_TYPE =
'Content-Type'.freeze
JSON_CONTENT_TYPE =
'application/json'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ DefaultConnection

Initialized using a hash containing the following parameters:

connect_timeout

Connection timeout in seconds.

socket_timeout

Socket timeout in seconds.

max_connections

Number of connections kept alive in the thread pool. Uses CommunicatorConfiguration.DEFAULT_MAX_CONNECTIONS if not given.

proxy_configuration

ProxyConfiguration object that stores the proxy to use. If not given the system default proxy is used; if there is no system default proxy set either, no proxy is used.

Raises:

  • (ArgumentError)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 33

def initialize(args)
  raise ArgumentError unless args.is_a? Hash
  # Set timeouts to nil if they are negative
  @connect_timeout = args[:connect_timeout]
  @connect_timeout = nil unless @connect_timeout.nil? || @connect_timeout > 0
  @socket_timeout = args[:socket_timeout]
  @socket_timeout = nil unless @socket_timeout.nil? || @socket_timeout > 0
  @max_connections = args[:max_connections] || CommunicatorConfiguration.DEFAULT_MAX_CONNECTIONS
  @proxy_configuration = args[:proxy_configuration]

  # HTTPClient provides the following features:
  # 1) thread safe, an instance can be used by multiple threads without
  # explicit synchronization
  # 2) use persistent connection if HTTP 1.1 is supported. The connection
  # will be left open until explicitly closed or keep_alive_timeout
  # 3) a built-in connection pool with no limit for max connections
  @http_client = create_http_client
  @http_client.connect_timeout = @connect_timeout
  @http_client.send_timeout = @socket_timeout
  @http_client.receive_timeout = @socket_timeout
end

Instance Method Details

#closeObject

Frees all networking resources used.



93
94
95
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 93

def close
  @http_client.reset_all
end

#close_expired_connectionsObject

HTTPClient automatically closes expired connections so close_expired_connections is a no-operation.



86
87
88
89
90
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 86

def close_expired_connections
  # By default the keep alive timeout is 15 sec, which is the HTTP 1.1
  # standard. To change the value, use keep_alive_timeout= method
  # do nothing, handled by HTTPClient
end

#close_idle_connections(idle_time) ⇒ Object

Closes all connections idle for longer than idle_time seconds. In addition, the keep_alive_timeout is set to idle_time so any future connections idle for longer than idle_time seconds will also be closed.



80
81
82
83
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 80

def close_idle_connections(idle_time) # in sec
  @http_client.keep_alive_timeout = idle_time # set timeout value
  close_expired_connections
end

#delete(uri, request_headers) ⇒ Object

Performs a DELETE request to the Ingenico ePayments platform

See Also:



110
111
112
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 110

def delete(uri, request_headers)
  request('delete', uri, request_headers)
end

#disable_loggingObject

Disables logging by unregistering any previous logger that might be registered.



180
181
182
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 180

def disable_logging
  @communicator_logger = nil
end

#enable_logging(communicator_logger) ⇒ Object

Enables logging outgoing requests and incoming responses by registering the communicator_logger. Note that only one logger can be registered at a time and calling enable_logging a second time will override the old logger instance with the new one.

communicator_logger

The Logging::CommunicatorLogger the requests and responses are logged to

Raises:

  • (ArgumentError)


174
175
176
177
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 174

def enable_logging(communicator_logger)
  raise ArgumentError.new('communicatorLogger is required') unless communicator_logger
  @communicator_logger = communicator_logger
end

#get(uri, request_headers) ⇒ Object

Performs a GET request to the Ingenico ePayments platform

See Also:



104
105
106
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 104

def get(uri, request_headers)
  request('get', uri, request_headers)
end

#post(uri, request_headers, body) ⇒ Object

Performs a POST request to the Ingenico ePayments platform

See Also:



116
117
118
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 116

def post(uri, request_headers, body)
  request('post', uri, request_headers, body)
end

#put(uri, request_headers, body) ⇒ Object

Performs a PUT request to the Ingenico ePayments platform

See Also:



122
123
124
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 122

def put(uri, request_headers, body)
  request('put', uri, request_headers, body)
end

#request(method, uri, request_headers, body = nil) ⇒ Object

performs a HTTP request and returns the response as an Response object. Also ensures the request is logged when sent and its response is logged when received. Raises CommunicationException when communication with the Ingenico ePayments platform was not successful.

method

‘GET’, ‘DELETE’, ‘POST’ or ‘PUT’ depending on the HTTP method being used.

uri

Full URI of the location the request is targeted at, including query parameters.

request_headers

RequestHeader list of headers that should be used as HTTP headers in the request.

body

Request body as a String.



133
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
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 133

def request(method, uri, request_headers, body=nil)
  request_headers = convert_from_sdk_headers(request_headers)
  request_id = SecureRandom.uuid
  request_headers[CONTENT_TYPE] = JSON_CONTENT_TYPE if body
  content_type = request_headers[CONTENT_TYPE]
  info = { headers: request_headers, content_type: content_type }
  info[:body] = body unless body.nil?
  log_request(request_id, method.upcase, uri, info)

  start_time = Time.now
  begin
    response = if body
                 @http_client.send(method, uri, header: request_headers, body: body)
               else
                 @http_client.send(method, uri, header: request_headers)
               end
  rescue HTTPClient::TimeoutError => e
    log_error(request_id, start_time, e)
    raise Ingenico::Connect::SDK::CommunicationException.new(e)
  rescue HTTPClient::KeepAliveDisconnected, HTTPClient::RetryableResponse => e  # retry these?
    log_error(request_id, start_time, e)
    raise Ingenico::Connect::SDK::CommunicationException.new(e)
  rescue => e
    log_error(request_id, start_time, e)
    raise Ingenico::Connect::SDK::CommunicationException.new(e)
  end

  log_response(request_id, response.status, start_time,
               headers: response.headers, body: response.body,
               content_type: response.content_type)
  convert_to_sdk_response(response)
end

#session_countObject

Returns the number of open connections



98
99
100
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 98

def session_count
  @http_client.session_count
end