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

Inherits:
PooledConnection show all
Includes:
Logging::Obfuscation::ObfuscationCapable
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

Returns a new instance of DefaultConnection.

Parameters:

  • args (Hash)

    the parameters to initialize the connection with

Options Hash (args):

  • :connect_timeout (Integer)

    connection timeout in seconds.

  • :socket_timeout (Integer)

    socket timeout in seconds.

  • :max_connections (Integer)

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

  • :proxy_configuration (Ingenico::Connect::SDK::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)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 44

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

  @body_obfuscator = Ingenico::Connect::SDK::Logging::Obfuscation::BodyObfuscator.default_obfuscator
  @header_obfuscator = Ingenico::Connect::SDK::Logging::Obfuscation::HeaderObfuscator.default_obfuscator
end

Instance Method Details

#closeObject

Frees all networking resources used.



112
113
114
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 112

def close
  @http_client.reset_all
end

#close_expired_connectionsObject

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



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

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.



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

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:



131
132
133
134
135
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 131

def delete(uri, request_headers)
  request('delete', uri, request_headers) do |response_status_code, response_headers, response_body|
    yield response_status_code, response_headers, response_body
  end
end

#disable_loggingObject

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



250
251
252
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 250

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.

Parameters:

Raises:

  • (ArgumentError)


243
244
245
246
247
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 243

def enable_logging(communicator_logger)
  raise ArgumentError, 'communicator_logger 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:



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

def get(uri, request_headers)
  request('get', uri, request_headers) do |response_status_code, response_headers, response_body|
    yield response_status_code, response_headers, response_body
  end
end

#post(uri, request_headers, body) ⇒ Object

Performs a POST request to the Ingenico ePayments platform

See Also:



139
140
141
142
143
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 139

def post(uri, request_headers, body)
  request('post', uri, request_headers, body) do |response_status_code, response_headers, response_body|
    yield response_status_code, response_headers, response_body
  end
end

#put(uri, request_headers, body) ⇒ Object

Performs a PUT request to the Ingenico ePayments platform

See Also:



147
148
149
150
151
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 147

def put(uri, request_headers, body)
  request('put', uri, request_headers, body) do |response_status_code, response_headers, response_body|
    yield response_status_code, response_headers, response_body
  end
end

#request(method, uri, request_headers, body = nil) {|Integer, Array<Ingenico::Connect::SDK::ResponseHeader>, IO| ... } ⇒ Object

Performs a HTTP request and yields the response as the status code, headers and body. Also ensures the request is logged when sent and its response is logged when received.

Parameters:

  • method (String)

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

  • uri (URI::HTTP)

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

  • request_headers (Array<Ingenico::Connect::SDK::RequestHeader>)

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

  • body (String, Ingenico::Connect::SDK::MultipartFormDataObject) (defaults to: nil)

    request body.

Yields:

Raises:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 162

def request(method, uri, request_headers, body = nil)
  request_headers = convert_from_sdk_headers(request_headers)
  request_id = SecureRandom.uuid
  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_headers = nil
    response_status_code = nil
    response_content_type = nil
    response_body = ''

    if body.is_a? Ingenico::Connect::SDK::MultipartFormDataObject
      multipart_request(method, uri, request_headers, body) do |status_code, headers, r_content_type, r_body|
        response_headers = headers
        response_status_code = status_code
        response_content_type = r_content_type
        unless binary_content_type? response_content_type
          response_body = r_body.read.force_encoding('UTF-8')
          r_body = StringIO.new(response_body)
        end

        yield status_code, headers, r_body
      end
    else
      raw_request(method, uri, request_headers, body) do |status_code, headers, r_content_type, r_body|
        response_headers = headers
        response_status_code = status_code
        response_content_type = r_content_type
        unless binary_content_type? response_content_type
          response_body = r_body.read.force_encoding('UTF-8')
          r_body = StringIO.new(response_body)
        end

        yield status_code, headers, r_body
      end
    end

    log_response(request_id, response_status_code, start_time,
                 headers: response_headers, body: response_body,
                 content_type: response_content_type)
  rescue HTTPClient::TimeoutError => e
    log_error(request_id, start_time, e)
    raise Ingenico::Connect::SDK::CommunicationException, e
  rescue HTTPClient::KeepAliveDisconnected, HTTPClient::RetryableResponse => e # retry these?
    log_error(request_id, start_time, e)
    raise Ingenico::Connect::SDK::CommunicationException, e
  rescue StandardError => e
    log_error(request_id, start_time, e)
    raise Ingenico::Connect::SDK::CommunicationException, e
  end
end

#session_countObject

Returns the number of open connections



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

def session_count
  @http_client.session_count
end

#set_body_obfuscator(body_obfuscator) ⇒ Object

Sets the current body obfuscator to use.

Parameters:

Raises:

  • (ArgumentError)


224
225
226
227
228
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 224

def set_body_obfuscator(body_obfuscator)
  raise ArgumentError, 'body_obfuscator is required' unless body_obfuscator

  @body_obfuscator = body_obfuscator
end

#set_header_obfuscator(header_obfuscator) ⇒ Object

Sets the current header obfuscator to use.

Parameters:

Raises:

  • (ArgumentError)


232
233
234
235
236
# File 'lib/ingenico/connect/sdk/defaultimpl/default_connection.rb', line 232

def set_header_obfuscator(header_obfuscator)
  raise ArgumentError, 'header_obfuscator is required' unless header_obfuscator

  @header_obfuscator = header_obfuscator
end