Class: CoreLibrary::ResponseHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/apimatic-core/response_handler.rb

Overview

Creates an instance of ResponseHandler.

Instance Method Summary collapse

Constructor Details

#initializeResponseHandler

Creates an instance of ResponseHandler.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/apimatic-core/response_handler.rb', line 5

def initialize
  @deserializer = nil
  @convertor = nil
  @deserialize_into = nil
  @is_api_response = false
  @is_nullify404 = false
  @local_errors = {}
  @datetime_format = nil
  @is_xml_response = false
  @xml_attribute = nil
  @endpoint_name_for_logging = nil
  @endpoint_logger = nil
  @is_primitive_response = false
  @is_date_response = false
  @is_response_array = false
  @is_response_void = false
  @type_group = nil
end

Instance Method Details

#apply_api_response(response, deserialized_value) ⇒ Object

Applies API response.

Parameters:

  • response

    The actual HTTP response.

  • deserialized_value

    The deserialized value.



249
250
251
252
253
254
255
256
# File 'lib/apimatic-core/response_handler.rb', line 249

def apply_api_response(response, deserialized_value)
  if @is_api_response
    errors = ApiHelper.map_response(deserialized_value, ['errors'])
    return ApiResponse.new(response, data: deserialized_value, errors: errors)
  end

  deserialized_value
end

#apply_convertor(deserialized_value) ⇒ Object

Applies converter to the response.

Parameters:

  • deserialized_value

    The deserialized value.



260
261
262
263
264
# File 'lib/apimatic-core/response_handler.rb', line 260

def apply_convertor(deserialized_value)
  return @convertor.call(deserialized_value) unless @convertor.nil?

  deserialized_value
end

#apply_deserializer(response, sdk_module, should_symbolize_hash) ⇒ Object

Applies deserializer to the response.

Parameters:

  • sdk_module

    Module of the SDK using the core library.

  • should_symbolize_hash (Boolean)

    Flag to symbolize the hash during response deserialization.



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# File 'lib/apimatic-core/response_handler.rb', line 229

def apply_deserializer(response, sdk_module, should_symbolize_hash)
  return apply_xml_deserializer(response) if @is_xml_response
  return response.raw_body if @deserializer.nil?

  if !@type_group.nil?
    @deserializer.call(@type_group, response.raw_body, sdk_module, should_symbolize_hash)
  elsif @datetime_format
    @deserializer.call(response.raw_body, @datetime_format, @is_response_array, should_symbolize_hash)
  elsif @is_date_response
    @deserializer.call(response.raw_body, @is_response_array, should_symbolize_hash)
  elsif !@deserialize_into.nil? || @is_primitive_response
    @deserializer.call(response.raw_body, @deserialize_into, @is_response_array, should_symbolize_hash)
  else
    @deserializer.call(response.raw_body, should_symbolize_hash)
  end
end

#apply_xml_deserializer(response) ⇒ Object

Applies xml deserializer to the response.



217
218
219
220
221
222
223
224
# File 'lib/apimatic-core/response_handler.rb', line 217

def apply_xml_deserializer(response)
  unless @xml_attribute.get_array_item_name.nil?
    return @deserializer.call(response.raw_body, @xml_attribute.get_root_element_name,
                              @xml_attribute.get_array_item_name, @deserialize_into, @datetime_format)
  end

  @deserializer.call(response.raw_body, @xml_attribute.get_root_element_name, @deserialize_into, @datetime_format)
end

#convertor(convertor) ⇒ ResponseHandler

Sets converter for the response.

Parameters:

  • convertor (Method)

    The method to be called while converting the deserialized response.

Returns:



35
36
37
38
# File 'lib/apimatic-core/response_handler.rb', line 35

def convertor(convertor)
  @convertor = convertor
  self
end

#datetime_format(datetime_format) ⇒ ResponseHandler

Sets the datetime format.

Parameters:

  • datetime_format (DateTimeFormat)

    The date time format to deserialize against.

Returns:



75
76
77
78
# File 'lib/apimatic-core/response_handler.rb', line 75

def datetime_format(datetime_format)
  @datetime_format = datetime_format
  self
end

#deserialize_into(deserialize_into) ⇒ ResponseHandler

Sets the model to deserialize into.

Parameters:

  • deserialize_into (Method)

    The method to be called while deserializing.

Returns:



43
44
45
46
# File 'lib/apimatic-core/response_handler.rb', line 43

def deserialize_into(deserialize_into)
  @deserialize_into = deserialize_into
  self
end

#deserializer(deserializer) ⇒ ResponseHandler

Sets deserializer for the response.

Parameters:

  • deserializer (Method)

    The method to be called for deserializing the response.

Returns:



27
28
29
30
# File 'lib/apimatic-core/response_handler.rb', line 27

def deserializer(deserializer)
  @deserializer = deserializer
  self
end

#endpoint_logger(endpoint_logger) ⇒ ResponseHandler

Sets endpoint logger to be used.

Parameters:

  • endpoint_logger (EndpointLogger)

    The logger to be used for logging API call steps.

Returns:



99
100
101
102
# File 'lib/apimatic-core/response_handler.rb', line 99

def endpoint_logger(endpoint_logger)
  @endpoint_logger = endpoint_logger
  self
end

#endpoint_name_for_logging(endpoint_name_for_logging) ⇒ ResponseHandler

Sets the endpoint_name_for_logging property.

Parameters:

  • endpoint_name_for_logging (String)

    The endpoint method name to be used while logging.

Returns:



91
92
93
94
# File 'lib/apimatic-core/response_handler.rb', line 91

def endpoint_name_for_logging(endpoint_name_for_logging)
  @endpoint_name_for_logging = endpoint_name_for_logging
  self
end

#handle(response, global_errors, sdk_module, should_symbolize_hash = false) ⇒ Object

Main method to handle the response with all the set properties. rubocop:disable Style/OptionalBooleanParameter

Parameters:

  • response (HttpResponse)

    The response received.

  • global_errors (Hash)

    The global errors object.

  • sdk_module (Module)

    The module of the SDK core library is being used for.

  • should_symbolize_hash (Boolean) (defaults to: false)

    Flag to symbolize the hash during response deserialization.

Returns:

  • (Object)

    The deserialized response of the API Call.



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
# File 'lib/apimatic-core/response_handler.rb', line 177

def handle(response, global_errors, sdk_module, should_symbolize_hash = false)
  @endpoint_logger.info("Validating response for #{@endpoint_name_for_logging}.")

  # checking Nullify 404
  if response.status_code == 404 && @is_nullify404
    @endpoint_logger.info("Status code 404 received for #{@endpoint_name_for_logging}. Returning None.")
    return nil
  end

  # validating response if configured
  validate(response, global_errors)

  return if @is_response_void

  # applying deserializer if configured
  deserialized_value = apply_deserializer(response, sdk_module, should_symbolize_hash)

  # applying api_response if configured
  deserialized_value = apply_api_response(response, deserialized_value)

  # applying convertor if configured
  deserialized_value = apply_convertor(deserialized_value)

  deserialized_value
end

#is_api_response(is_api_response) ⇒ ResponseHandler

Sets the is_api_response property.

Parameters:

  • is_api_response (Boolean)

    Flag to return the complete http response.

Returns:



116
117
118
119
# File 'lib/apimatic-core/response_handler.rb', line 116

def is_api_response(is_api_response)
  @is_api_response = is_api_response
  self
end

#is_date_response(is_date_response) ⇒ ResponseHandler

Sets the is_date_response property.

Parameters:

  • is_date_response (Boolean)

    Flag if the response is a date.

Returns:



140
141
142
143
# File 'lib/apimatic-core/response_handler.rb', line 140

def is_date_response(is_date_response)
  @is_date_response = is_date_response
  self
end

#is_nullify404(is_nullify404) ⇒ ResponseHandler

Sets the is_nullify404 property.

Parameters:

  • is_nullify404 (Boolean)

    Flag to return early in case of 404 error code.

Returns:



124
125
126
127
# File 'lib/apimatic-core/response_handler.rb', line 124

def is_nullify404(is_nullify404)
  @is_nullify404 = is_nullify404
  self
end

#is_primitive_response(is_primitive_response) ⇒ ResponseHandler

Sets the is_primitive_response property. rubocop:disable Naming/PredicateName

Parameters:

  • is_primitive_response (Boolean)

    Flag if the response is of primitive type.

Returns:



108
109
110
111
# File 'lib/apimatic-core/response_handler.rb', line 108

def is_primitive_response(is_primitive_response)
  @is_primitive_response = is_primitive_response
  self
end

#is_response_array(is_response_array) ⇒ ResponseHandler

Sets the is_response_array property.

Parameters:

  • is_response_array (Boolean)

    Flag if the response is an array.

Returns:



148
149
150
151
# File 'lib/apimatic-core/response_handler.rb', line 148

def is_response_array(is_response_array)
  @is_response_array = is_response_array
  self
end

#is_response_void(is_response_void) ⇒ ResponseHandler

Sets the is_response_void property.

Parameters:

  • is_response_void (Boolean)

    Flag if the response is void.

Returns:



156
157
158
159
# File 'lib/apimatic-core/response_handler.rb', line 156

def is_response_void(is_response_void)
  @is_response_void = is_response_void
  self
end

#is_xml_response(is_xml_response) ⇒ ResponseHandler

Set the is_xml_response property.

Parameters:

  • is_xml_response (Boolean)

    Flag if the response is XML.

Returns:



132
133
134
135
# File 'lib/apimatic-core/response_handler.rb', line 132

def is_xml_response(is_xml_response)
  @is_xml_response = is_xml_response
  self
end

#local_error(error_code, error_message, exception_type) ⇒ ResponseHandler

Registers an entry with error message in the local errors hash.

Parameters:

  • error_code (String)

    The error code to check against.

  • error_message (String)

    The reason for the exception.

  • exception_type (ApiException)

    The type of the exception to raise.

Returns:



53
54
55
56
57
58
# File 'lib/apimatic-core/response_handler.rb', line 53

def local_error(error_code, error_message, exception_type)
  @local_errors[error_code.to_s] = ErrorCase.new
                                            .error_message(error_message)
                                            .exception_type(exception_type)
  self
end

#local_error_template(error_code, error_message_template, exception_type) ⇒ ResponseHandler

Registers an entry with error template in the local errors hash.

Parameters:

  • error_code (String)

    The error code to check against.

  • error_message_template (String)

    The reason template for the exception.

  • exception_type (ApiException)

    The type of the exception to raise.

Returns:



65
66
67
68
69
70
# File 'lib/apimatic-core/response_handler.rb', line 65

def local_error_template(error_code, error_message_template, exception_type)
  @local_errors[error_code.to_s] = ErrorCase.new
                                            .error_message_template(error_message_template)
                                            .exception_type(exception_type)
  self
end

#type_group(type_group) ⇒ ResponseHandler

Sets type group for the response.

Parameters:

  • type_group (String)

    The oneOf/anyOf type group template.

Returns:



165
166
167
168
# File 'lib/apimatic-core/response_handler.rb', line 165

def type_group(type_group)
  @type_group = type_group
  self
end

#validate(response, global_errors) ⇒ Object

Validates the response provided and throws an error against the configured status code.

Parameters:

  • response (HttpResponse)

    The received response.

  • global_errors (Hash)

    Global errors hash.

Raises:

  • (ApiException)

    Throws the exception when the response contains errors.



208
209
210
211
212
213
214
# File 'lib/apimatic-core/response_handler.rb', line 208

def validate(response, global_errors)
  return unless response.status_code < 200 || response.status_code > 299

  validate_against_error_cases(response, @local_errors)

  validate_against_error_cases(response, global_errors)
end

#validate_against_error_cases(response, error_cases) ⇒ Object

Validates the response against the provided error cases hash, if matches, it raises the exception.

Parameters:

  • response (HttpResponse)

    The received response.

  • error_cases (Hash)

    The error cases hash.

Raises:

  • (ApiException)

    Raises the APIException when configured error code matches.



270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/apimatic-core/response_handler.rb', line 270

def validate_against_error_cases(response, error_cases)
  actual_status_code = response.status_code.to_s

  # Handling error case when configured as explicit error code
  error_case = error_cases[actual_status_code]
  error_case&.raise_exception(response)

  # Handling error case when configured as explicit error codes range
  default_range_entry = error_cases&.filter do |error_code, _|
    error_code.match?("^#{actual_status_code[0]}XX$")
  end

  default_range_error_case = default_range_entry&.map { |_, error_case_instance| error_case_instance }

  default_range_error_case[0].raise_exception(response) unless
    default_range_error_case.nil? || default_range_error_case.empty?

  # Handling default error case if configured
  default_error_case = error_cases['default']
  default_error_case&.raise_exception(response)
end

#xml_attribute(xml_attribute) ⇒ ResponseHandler

Set the xml_attribute property.

Parameters:

  • xml_attribute (XmlAttributes)

    The xml configuration if the response is XML.

Returns:



83
84
85
86
# File 'lib/apimatic-core/response_handler.rb', line 83

def xml_attribute(xml_attribute)
  @xml_attribute = xml_attribute
  self
end