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
# 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
  @body_serializer = nil
  @auth = nil
  @array_serialization_format = ArraySerializationFormat::INDEXED
  @xml_attributes = nil
  @endpoint_name_for_logging = nil
  @endpoint_logger = nil
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:



89
90
91
92
# File 'lib/apimatic-core/request_builder.rb', line 89

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:



97
98
99
100
# File 'lib/apimatic-core/request_builder.rb', line 97

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:



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

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 AuthValidationException, @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:



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

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:



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

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:



114
115
116
117
118
119
120
121
122
123
# File 'lib/apimatic-core/request_builder.rb', line 114

def body_param(body_param)
  body_param.validate
  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:



128
129
130
131
# File 'lib/apimatic-core/request_builder.rb', line 128

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:



182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/apimatic-core/request_builder.rb', line 182

def build(endpoint_context)
  _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

#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:



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

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:



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

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:



80
81
82
83
84
# File 'lib/apimatic-core/request_builder.rb', line 80

def form_param(form_param)
  form_param.validate
  @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.



287
288
289
290
291
292
293
294
295
296
297
# File 'lib/apimatic-core/request_builder.rb', line 287

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.



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

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.



174
175
176
177
# File 'lib/apimatic-core/request_builder.rb', line 174

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:



62
63
64
65
66
# File 'lib/apimatic-core/request_builder.rb', line 62

def header_param(header_param)
  header_param.validate
  @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:



44
45
46
47
# File 'lib/apimatic-core/request_builder.rb', line 44

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:



105
106
107
108
109
# File 'lib/apimatic-core/request_builder.rb', line 105

def multipart_param(multipart_param)
  multipart_param.validate
  @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:



36
37
38
39
# File 'lib/apimatic-core/request_builder.rb', line 36

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.



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

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
    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

  nil
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.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/apimatic-core/request_builder.rb', line 227

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.



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

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.



302
303
304
305
306
307
308
309
310
# File 'lib/apimatic-core/request_builder.rb', line 302

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:



71
72
73
74
75
# File 'lib/apimatic-core/request_builder.rb', line 71

def query_param(query_param)
  query_param.validate
  @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.



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

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:



28
29
30
31
# File 'lib/apimatic-core/request_builder.rb', line 28

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:



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

def template_param(template_param)
  template_param.validate
  @template_params[template_param.get_key] = {  'value' => template_param.get_value,
                                                'encode' => template_param.need_to_encode }
  self
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:



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

def xml_attributes(xml_attributes)
  @xml_attributes = xml_attributes
  self
end