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



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

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:



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

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:



300
301
302
303
304
# File 'lib/apimatic-core/request_builder.rb', line 300

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:



142
143
144
145
# File 'lib/apimatic-core/request_builder.rb', line 142

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:



134
135
136
137
# File 'lib/apimatic-core/request_builder.rb', line 134

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:



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

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:



126
127
128
129
# File 'lib/apimatic-core/request_builder.rb', line 126

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:



164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/apimatic-core/request_builder.rb', line 164

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

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



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

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.



258
259
260
261
262
263
264
265
266
267
268
# File 'lib/apimatic-core/request_builder.rb', line 258

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.



190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/apimatic-core/request_builder.rb', line 190

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.



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

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:



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

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:



42
43
44
45
# File 'lib/apimatic-core/request_builder.rb', line 42

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:



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

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:



34
35
36
37
# File 'lib/apimatic-core/request_builder.rb', line 34

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.



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

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



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

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?

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



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

def process_url
  _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.



273
274
275
276
277
278
279
280
281
# File 'lib/apimatic-core/request_builder.rb', line 273

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:



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

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.



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

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:



26
27
28
29
# File 'lib/apimatic-core/request_builder.rb', line 26

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:



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

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:



150
151
152
153
# File 'lib/apimatic-core/request_builder.rb', line 150

def xml_attributes(xml_attributes)
  @xml_attributes = xml_attributes
  self
end