Class: CoreLibrary::RequestBuilder
- Inherits:
-
Object
- Object
- CoreLibrary::RequestBuilder
- 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
-
#additional_form_params(additional_form_params) ⇒ RequestBuilder
The setter for the additional form parameter to be sent in the request.
-
#additional_query_params(additional_query_params) ⇒ RequestBuilder
The setter for the additional query parameter to be sent in the request.
-
#apply_auth(auth_managers, http_request) ⇒ Object
Applies the configured auth onto the http request.
-
#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.
-
#auth(auth) ⇒ RequestBuilder
The setter for the auth to be used for the request.
-
#body_param(body_param) ⇒ RequestBuilder
The setter for the body parameter to be sent in the request.
-
#body_serializer(body_serializer) ⇒ RequestBuilder
The setter for the callable of serializing the body.
-
#build(endpoint_context) ⇒ HttpRequest
Builds the Http Request.
-
#conditional_add_to_template_validation_array(parameter) ⇒ Object
Add param to the template validation array, in case a template is provided.
-
#endpoint_logger(endpoint_logger) ⇒ RequestBuilder
The setter for the name of the endpoint controller method to used while logging an endpoint call.
-
#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.
-
#form_param(form_param) ⇒ RequestBuilder
The setter for the form parameter to be sent in the request.
-
#get_part(multipart_param) ⇒ UploadIO
Processes the part of a multipart request and assign appropriate part value and its content-type.
-
#get_updated_url_with_query_params(url) ⇒ String
Returns the URL with resolved query parameters if any.
-
#global_configuration(global_configuration) ⇒ Object
Sets global configuration object for the request builder.
-
#header_param(header_param) ⇒ RequestBuilder
The setter for the header parameter to be sent in the request.
-
#http_method(http_method) ⇒ RequestBuilder
The setter for the http method of the request.
-
#initialize ⇒ RequestBuilder
constructor
Creates an instance of RequestBuilder.
-
#multipart_param(multipart_param) ⇒ RequestBuilder
The setter for the multipart parameter to be sent in the request.
-
#path(path) ⇒ RequestBuilder
The setter for the URI of the endpoint.
-
#process_body ⇒ Object
Processes the body parameter of the request (including form param, json body or xml body).
-
#process_headers(global_configuration) ⇒ Hash
Processes all request headers (including local, global and additional).
-
#process_url ⇒ String
Processes and resolves the endpoint URL.
-
#process_xml_parameters ⇒ String
The serialized xml body.
-
#query_param(query_param) ⇒ RequestBuilder
The setter for the query parameter to be sent in the request.
-
#resolve_body_param ⇒ Hash
Resolves the body parameter to appropriate type.
-
#server(server) ⇒ RequestBuilder
The setter for the server.
-
#template_param(template_param) ⇒ RequestBuilder
The setter for the template parameter of the request.
-
#validate_templates ⇒ Object
Validates the template for the params provided with a template.
-
#xml_attributes(xml_attributes) ⇒ RequestBuilder
The setter for the xml attributes to used while serialization of the xml body.
Constructor Details
#initialize ⇒ RequestBuilder
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.
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.
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.
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. 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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
38 39 40 41 |
# File 'lib/apimatic-core/request_builder.rb', line 38 def path(path) @path = path self end |
#process_body ⇒ Object
Processes the body parameter of the request (including form param, json body or xml body).
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).
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_url ⇒ String
Processes and resolves the endpoint 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_parameters ⇒ String
Returns 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.
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_param ⇒ Hash
Resolves the body parameter to appropriate 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.
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.
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_templates ⇒ Object
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.
160 161 162 163 |
# File 'lib/apimatic-core/request_builder.rb', line 160 def xml_attributes(xml_attributes) @xml_attributes = xml_attributes self end |