Class: CoreLibrary::RequestBuilder

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

Overview

This class is the builder of the http request for an API call.

Instance Method Summary collapse

Constructor Details

#initializeRequestBuilder

Creates an instance of RequestBuilder.



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

def initialize
  @server = nil
  @path = nil
  @http_method = nil
  @template_params = {}
  @header_params = {}
  @query_params = {}
  @form_params = {}
  @additional_form_params = {}
  @additional_query_params = {}
  @multipart_params = {}
  @body_param = nil
  @should_wrap_body_param = nil
  @body_serializer = nil
  @auth = nil
  @array_serialization_format = ArraySerializationFormat::INDEXED
  @xml_attributes = nil
  @endpoint_name_for_logging = nil
  @endpoint_logger = nil
  @template_validation_array = []
end

Instance Method Details

#additional_form_params(additional_form_params) ⇒ RequestBuilder

The setter for the additional form parameter to be sent in the request.

Parameters:

  • additional_form_params (Hash)

    The additional form parameter to be sent in the request.

Returns:



95
96
97
98
# File 'lib/apimatic-core/request_builder.rb', line 95

def additional_form_params(additional_form_params)
  @additional_form_params = additional_form_params
  self
end

#additional_query_params(additional_query_params) ⇒ RequestBuilder

The setter for the additional query parameter to be sent in the request.

Parameters:

  • additional_query_params (Hash)

    The additional query parameter to be sent in the request.

Returns:



103
104
105
106
# File 'lib/apimatic-core/request_builder.rb', line 103

def additional_query_params(additional_query_params)
  @additional_query_params = additional_query_params
  self
end

#apply_auth(auth_managers, http_request) ⇒ Object

Applies the configured auth onto the http request.

Parameters:

  • auth_managers (Hash)

    The hash of auth managers.

  • http_request (HttpRequest)

    The HTTP request on which the auth is to be applied.

Raises:



356
357
358
359
360
# File 'lib/apimatic-core/request_builder.rb', line 356

def apply_auth(auth_managers, http_request)
  is_valid_auth = @auth.with_auth_managers(auth_managers).valid unless @auth.nil?
  @auth.apply(http_request) if is_valid_auth
  raise InvalidAuthCredential, @auth.error_message if !@auth.nil? && !is_valid_auth
end

#array_serialization_format(array_serialization_format) ⇒ RequestBuilder

The setter for the serialization format to be used for arrays in query or form parameters of the request.

Parameters:

  • array_serialization_format (ArraySerializationFormat)

    The serialization format to be used for arrays.

Returns:



152
153
154
155
# File 'lib/apimatic-core/request_builder.rb', line 152

def array_serialization_format(array_serialization_format)
  @array_serialization_format = array_serialization_format
  self
end

#auth(auth) ⇒ RequestBuilder

The setter for the auth to be used for the request.

Parameters:

  • auth (Authentication)

    The instance of single or multiple auths.

Returns:



144
145
146
147
# File 'lib/apimatic-core/request_builder.rb', line 144

def auth(auth)
  @auth = auth
  self
end

#body_param(body_param) ⇒ RequestBuilder

The setter for the body parameter to be sent in the request.

Parameters:

  • body_param (Parameter)

    The body parameter to be sent in the request.

Returns:



121
122
123
124
125
126
127
128
129
130
131
# File 'lib/apimatic-core/request_builder.rb', line 121

def body_param(body_param)
  body_param.validate
  conditional_add_to_template_validation_array(body_param)
  if !body_param.get_key.nil?
    @body_param = {} if @body_param.nil?
    @body_param[body_param.get_key] = body_param.get_value
  else
    @body_param = body_param.get_value
  end
  self
end

#body_serializer(body_serializer) ⇒ RequestBuilder

The setter for the callable of serializing the body.

Parameters:

  • body_serializer (Callable)

    The callable for serializing the body.

Returns:



136
137
138
139
# File 'lib/apimatic-core/request_builder.rb', line 136

def body_serializer(body_serializer)
  @body_serializer = body_serializer
  self
end

#build(endpoint_context) ⇒ HttpRequest

Builds the Http Request.

Parameters:

  • endpoint_context (Hash)

    The endpoint configuration to be used while executing the request.

Returns:



207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'lib/apimatic-core/request_builder.rb', line 207

def build(endpoint_context)
  validate_templates
  _url = process_url
  _request_body = process_body
  _request_headers = process_headers(@global_configuration)
  _http_request = HttpRequest.new(@http_method, _url,
                                  headers: _request_headers,
                                  parameters: _request_body,
                                  context: endpoint_context)
  apply_auth(@global_configuration.get_auth_managers, _http_request)

  _http_request
end

#conditional_add_to_template_validation_array(parameter) ⇒ Object

Add param to the template validation array, in case a template is provided.



188
189
190
191
192
# File 'lib/apimatic-core/request_builder.rb', line 188

def conditional_add_to_template_validation_array(parameter)
  return if parameter.get_template.nil?

  @template_validation_array.push(parameter)
end

#endpoint_logger(endpoint_logger) ⇒ RequestBuilder

The setter for the name of the endpoint controller method to used while logging an endpoint call.

Parameters:

  • endpoint_logger (EndpointLogger)

    The name of the endpoint controller method to used while logging.

Returns:



176
177
178
179
# File 'lib/apimatic-core/request_builder.rb', line 176

def endpoint_logger(endpoint_logger)
  @endpoint_logger = endpoint_logger
  self
end

#endpoint_name_for_logging(endpoint_name_for_logging) ⇒ RequestBuilder

The setter for the name of the endpoint controller method to used while logging an endpoint call.

Parameters:

  • endpoint_name_for_logging (String)

    The name of the endpoint controller method to used while logging.

Returns:



168
169
170
171
# File 'lib/apimatic-core/request_builder.rb', line 168

def endpoint_name_for_logging(endpoint_name_for_logging)
  @endpoint_name_for_logging = endpoint_name_for_logging
  self
end

#form_param(form_param) ⇒ RequestBuilder

The setter for the form parameter to be sent in the request.

Parameters:

  • form_param (Parameter)

    The form parameter to be sent in the request.

Returns:



85
86
87
88
89
90
# File 'lib/apimatic-core/request_builder.rb', line 85

def form_param(form_param)
  form_param.validate
  conditional_add_to_template_validation_array(form_param)
  @form_params[form_param.get_key] = form_param.get_value
  self
end

#get_part(multipart_param) ⇒ UploadIO

Processes the part of a multipart request and assign appropriate part value and its content-type.

Parameters:

  • multipart_param (Parameter)

    The multipart parameter to be sent in the request.

Returns:

  • (UploadIO)

    The translated Faraday’s UploadIO instance.



314
315
316
317
318
319
320
321
322
323
324
# File 'lib/apimatic-core/request_builder.rb', line 314

def get_part(multipart_param)
  param_value = multipart_param.get_value
  if param_value.is_a? FileWrapper
    part = param_value.file
    part_content_type = param_value.content_type
  else
    part = param_value
    part_content_type = multipart_param.get_default_content_type
  end
  Faraday::UploadIO.new(part, part_content_type)
end

#get_updated_url_with_query_params(url) ⇒ String

Returns the URL with resolved query parameters if any.

Parameters:

  • url (String)

    The URL of the endpoint.

Returns:

  • (String)

    The URL with resolved query parameters if any.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/apimatic-core/request_builder.rb', line 235

def get_updated_url_with_query_params(url)
  _has_additional_query_params = (!@additional_query_params.nil? and @additional_query_params.any?)
  _has_query_params = (!@query_params.nil? and @query_params.any?)
  _query_params = @query_params
  _query_params.merge!(@additional_query_params) if _has_additional_query_params

  if !_query_params.nil? && _query_params.any?
    return ApiHelper.append_url_with_query_parameters(url,
                                                      _query_params,
                                                      @array_serialization_format)
  end

  url
end

#global_configuration(global_configuration) ⇒ Object

Sets global configuration object for the request builder.



182
183
184
185
# File 'lib/apimatic-core/request_builder.rb', line 182

def global_configuration(global_configuration)
  @global_configuration = global_configuration
  self
end

#header_param(header_param) ⇒ RequestBuilder

The setter for the header parameter to be sent in the request.

Parameters:

  • header_param (Parameter)

    The header parameter to be sent in the request.

Returns:



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

def header_param(header_param)
  header_param.validate
  conditional_add_to_template_validation_array(header_param)
  @header_params[header_param.get_key] = header_param.get_value
  self
end

#http_method(http_method) ⇒ RequestBuilder

The setter for the http method of the request.

Parameters:

  • http_method (HttpMethod)

    The http method of the request.

Returns:



46
47
48
49
# File 'lib/apimatic-core/request_builder.rb', line 46

def http_method(http_method)
  @http_method = http_method
  self
end

#multipart_param(multipart_param) ⇒ RequestBuilder

The setter for the multipart parameter to be sent in the request.

Parameters:

  • multipart_param (Parameter)

    The multipart parameter to be sent in the request.

Returns:



111
112
113
114
115
116
# File 'lib/apimatic-core/request_builder.rb', line 111

def multipart_param(multipart_param)
  multipart_param.validate
  conditional_add_to_template_validation_array(multipart_param)
  @multipart_params[multipart_param.get_key] = get_part(multipart_param)
  self
end

#path(path) ⇒ RequestBuilder

The setter for the URI of the endpoint.

Parameters:

  • path (string)

    The URI of the endpoint.

Returns:



38
39
40
41
# File 'lib/apimatic-core/request_builder.rb', line 38

def path(path)
  @path = path
  self
end

#process_bodyObject

Processes the body parameter of the request (including form param, json body or xml body).

Returns:

  • (Object)

    The body param to be sent in the request.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'lib/apimatic-core/request_builder.rb', line 279

def process_body
  _has_form_params = (!@form_params.nil? && @form_params.any?)
  _has_additional_form_params = (!@additional_form_params.nil? && @additional_form_params.any?)
  _has_multipart_param = (!@multipart_params.nil? && @multipart_params.any?)
  _has_body_param = !@body_param.nil?
  _has_body_serializer = !@body_serializer.nil?
  _has_xml_attributes = !@xml_attributes.nil?

  if _has_form_params || _has_additional_form_params
    @endpoint_logger.info("Preparing form parameters for #{@endpoint_name_for_logging}.")
  elsif _has_body_param
    @endpoint_logger.info("Preparing body parameters for #{@endpoint_name_for_logging}.")
  end

  if _has_xml_attributes
    return process_xml_parameters
  elsif _has_form_params || _has_additional_form_params || _has_multipart_param
    _form_params = @form_params
    _form_params.merge!(@form_params) if _has_form_params
    _form_params.merge!(@multipart_params) if _has_multipart_param
    _form_params.merge!(@additional_form_params) if _has_additional_form_params
    # TODO: add Array serialization format support while writing the POC
    return ApiHelper.form_encode_parameters(_form_params, @array_serialization_format)
  elsif _has_body_param && _has_body_serializer
    return @body_serializer.call(resolve_body_param)
  elsif _has_body_param && !_has_body_serializer
    return resolve_body_param
  end

  {}
end

#process_headers(global_configuration) ⇒ Hash

Processes all request headers (including local, global and additional).

Parameters:

  • global_configuration (GlobalConfiguration)

    The global configuration to be used while processing the URL.

Returns:

  • (Hash)

    The processed request headers to be sent in the request.



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/apimatic-core/request_builder.rb', line 253

def process_headers(global_configuration)
  _request_headers = {}
  _global_headers = global_configuration.get_global_headers
  _additional_headers = global_configuration.get_additional_headers

  _has_global_headers = (!_global_headers.nil? && _global_headers.any?)
  _has_additional_headers = (!_additional_headers.nil? && _additional_headers.any?)
  _has_local_headers = (!@header_params.nil? and @header_params.any?)

  if _has_global_headers || _has_additional_headers || _has_local_headers
    @endpoint_logger.info("Preparing headers for #{@endpoint_name_for_logging}.")
  end

  _request_headers.merge!(_global_headers) if _has_global_headers
  _request_headers.merge!(_additional_headers) if _has_additional_headers

  if _has_local_headers
    ApiHelper.clean_hash(@header_params)
    _request_headers.merge!(@header_params)
  end

  _request_headers
end

#process_urlString

Processes and resolves the endpoint URL.

Returns:

  • (String)

    The processed URL.



223
224
225
226
227
228
229
230
# File 'lib/apimatic-core/request_builder.rb', line 223

def process_url
  @endpoint_logger.info("Preparing query URL for #{@endpoint_name_for_logging}.")
  _base_url = @global_configuration.get_base_uri_executor.call(@server)
  _updated_url_with_template_params = ApiHelper.append_url_with_template_parameters(@path, @template_params)
  _url = _base_url + _updated_url_with_template_params
  _url = get_updated_url_with_query_params(_url)
  ApiHelper.clean_url(_url)
end

#process_xml_parametersString

Returns The serialized xml body.

Returns:

  • (String)

    The serialized xml body.



329
330
331
332
333
334
335
336
337
# File 'lib/apimatic-core/request_builder.rb', line 329

def process_xml_parameters
  unless @xml_attributes.get_array_item_name.nil?
    return @body_serializer.call(@xml_attributes.get_root_element_name,
                                 @xml_attributes.get_array_item_name,
                                 @xml_attributes.get_value)
  end

  @body_serializer.call(@xml_attributes.get_root_element_name, @xml_attributes.get_value)
end

#query_param(query_param) ⇒ RequestBuilder

The setter for the query parameter to be sent in the request.

Parameters:

  • query_param (Parameter)

    The query parameter to be sent in the request.

Returns:



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

def query_param(query_param)
  query_param.validate
  conditional_add_to_template_validation_array(query_param)
  @query_params[query_param.get_key] = query_param.get_value
  self
end

#resolve_body_paramHash

Resolves the body parameter to appropriate type.

Returns:

  • (Hash)

    The resolved body parameter as per the type.



341
342
343
344
345
346
347
348
349
350
351
# File 'lib/apimatic-core/request_builder.rb', line 341

def resolve_body_param
  if !@body_param.nil? && @body_param.is_a?(FileWrapper)
    @header_params['content-type'] = @body_param.content_type if !@body_param.file.nil? &&
                                                                 !@body_param.content_type.nil?
    @header_params['content-length'] = @body_param.file.size.to_s
    return @body_param.file
  elsif !@body_param.nil? && @body_param.is_a?(File)
    @header_params['content-length'] = @body_param.size.to_s
  end
  @body_param
end

#server(server) ⇒ RequestBuilder

The setter for the server.

Parameters:

  • server (string)

    The server to use for the API call.

Returns:



30
31
32
33
# File 'lib/apimatic-core/request_builder.rb', line 30

def server(server)
  @server = server
  self
end

#template_param(template_param) ⇒ RequestBuilder

The setter for the template parameter of the request.

Parameters:

  • template_param (Parameter)

    The template parameter of the request.

Returns:



54
55
56
57
58
59
60
# File 'lib/apimatic-core/request_builder.rb', line 54

def template_param(template_param)
  template_param.validate
  conditional_add_to_template_validation_array(template_param)
  @template_params[template_param.get_key] = {  'value' => template_param.get_value,
                                                'encode' => template_param.need_to_encode }
  self
end

#validate_templatesObject

Validates the template for the params provided with a template.



195
196
197
198
199
200
201
202
# File 'lib/apimatic-core/request_builder.rb', line 195

def validate_templates
  return if @template_validation_array.nil? || @template_validation_array.empty?

  @template_validation_array.each do |parameter|
    parameter.validate_template(@global_configuration.get_sdk_module,
                                @global_configuration.should_symbolize_hash)
  end
end

#xml_attributes(xml_attributes) ⇒ RequestBuilder

The setter for the xml attributes to used while serialization of the xml body.

Parameters:

  • xml_attributes (XmlAttribute)

    The xml attribute to used while serialization of the xml body.

Returns:



160
161
162
163
# File 'lib/apimatic-core/request_builder.rb', line 160

def xml_attributes(xml_attributes)
  @xml_attributes = xml_attributes
  self
end