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.
Constant Summary collapse
- PATH_PARAM_POINTER =
'$request.path'.freeze
- QUERY_PARAM_POINTER =
'$request.query'.freeze
- HEADER_PARAM_POINTER =
'$request.headers'.freeze
- BODY_PARAM_POINTER =
'$request.body'.freeze
Instance Attribute Summary collapse
-
#body_params ⇒ Object
readonly
Returns the value of attribute body_params.
-
#form_params ⇒ Object
readonly
Returns the value of attribute form_params.
-
#header_params ⇒ Object
readonly
Returns the value of attribute header_params.
-
#query_params ⇒ Object
readonly
Returns the value of attribute query_params.
-
#template_params ⇒ Object
readonly
Returns the value of attribute template_params.
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.
-
#clone_with(template_params: nil, query_params: nil, header_params: nil, body_params: nil, form_params: nil) ⇒ RequestBuilder
Creates a deep copy of this RequestBuilder instance with optional overrides.
-
#form_param(form_param) ⇒ RequestBuilder
The setter for the form parameter to be sent in the request.
-
#get_parameter_value_by_json_pointer(json_pointer) ⇒ Object?
Extracts the initial pagination offset value from the request builder using the specified JSON pointer.
-
#get_part(multipart_param) ⇒ UploadIO
Processes the part of a multipart request and assign appropriate part value and its content-type.
-
#get_updated_request_by_json_pointer(json_pointer, value) ⇒ Object
Updates the given request builder by modifying its path, query, or header parameters based on the specified JSON pointer and value.
-
#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.
-
#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.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/apimatic-core/request_builder.rb', line 12 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_params = nil @body_serializer = nil @auth = nil @array_serialization_format = ArraySerializationFormat::INDEXED @xml_attributes = nil end |
Instance Attribute Details
#body_params ⇒ Object (readonly)
Returns the value of attribute body_params.
9 10 11 |
# File 'lib/apimatic-core/request_builder.rb', line 9 def body_params @body_params end |
#form_params ⇒ Object (readonly)
Returns the value of attribute form_params.
9 10 11 |
# File 'lib/apimatic-core/request_builder.rb', line 9 def form_params @form_params end |
#header_params ⇒ Object (readonly)
Returns the value of attribute header_params.
9 10 11 |
# File 'lib/apimatic-core/request_builder.rb', line 9 def header_params @header_params end |
#query_params ⇒ Object (readonly)
Returns the value of attribute query_params.
9 10 11 |
# File 'lib/apimatic-core/request_builder.rb', line 9 def query_params @query_params end |
#template_params ⇒ Object (readonly)
Returns the value of attribute template_params.
9 10 11 |
# File 'lib/apimatic-core/request_builder.rb', line 9 def template_params @template_params end |
Instance Method Details
#additional_form_params(additional_form_params) ⇒ RequestBuilder
The setter for the additional form parameter to be sent in the request.
94 95 96 97 |
# File 'lib/apimatic-core/request_builder.rb', line 94 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.
102 103 104 105 |
# File 'lib/apimatic-core/request_builder.rb', line 102 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.
311 312 313 314 315 |
# File 'lib/apimatic-core/request_builder.rb', line 311 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. 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.
149 150 151 152 |
# File 'lib/apimatic-core/request_builder.rb', line 149 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.
141 142 143 144 |
# File 'lib/apimatic-core/request_builder.rb', line 141 def auth(auth) @auth = auth self end |
#body_param(body_param) ⇒ RequestBuilder
The setter for the body parameter to be sent in the request.
119 120 121 122 123 124 125 126 127 128 |
# File 'lib/apimatic-core/request_builder.rb', line 119 def body_param(body_param) body_param.validate if !body_param.get_key.nil? @body_params = {} if @body_params.nil? @body_params[body_param.get_key] = body_param.get_value else @body_params = body_param.get_value end self end |
#body_serializer(body_serializer) ⇒ RequestBuilder
The setter for the callable of serializing the body.
133 134 135 136 |
# File 'lib/apimatic-core/request_builder.rb', line 133 def body_serializer(body_serializer) @body_serializer = body_serializer self end |
#build(endpoint_context) ⇒ HttpRequest
Builds the Http Request.
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/apimatic-core/request_builder.rb', line 171 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 |
#clone_with(template_params: nil, query_params: nil, header_params: nil, body_params: nil, form_params: nil) ⇒ RequestBuilder
Creates a deep copy of this RequestBuilder instance with optional overrides.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 |
# File 'lib/apimatic-core/request_builder.rb', line 402 def clone_with( template_params: nil, query_params: nil, header_params: nil, body_params: nil, form_params: nil ) clone = RequestBuilder.new # Manually copy internal state clone.instance_variable_set(:@server, DeepCloneUtils.deep_copy(@server)) clone.instance_variable_set(:@path, DeepCloneUtils.deep_copy(@path)) clone.instance_variable_set(:@http_method, DeepCloneUtils.deep_copy(@http_method)) clone.instance_variable_set(:@template_params, template_params || DeepCloneUtils.deep_copy(@template_params)) clone.instance_variable_set(:@header_params, header_params || DeepCloneUtils.deep_copy(@header_params)) clone.instance_variable_set(:@query_params, query_params || DeepCloneUtils.deep_copy(@query_params)) clone.instance_variable_set(:@form_params, form_params || DeepCloneUtils.deep_copy(@form_params)) clone.instance_variable_set(:@additional_form_params, DeepCloneUtils.deep_copy(@additional_form_params)) clone.instance_variable_set(:@additional_query_params, DeepCloneUtils.deep_copy(@additional_query_params)) clone.instance_variable_set(:@multipart_params, DeepCloneUtils.deep_copy(@multipart_params)) clone.instance_variable_set(:@body_params, body_params || DeepCloneUtils.deep_copy(@body_params)) clone.instance_variable_set(:@body_serializer, DeepCloneUtils.deep_copy(@body_serializer)) clone.instance_variable_set(:@auth, DeepCloneUtils.deep_copy(@auth)) clone.instance_variable_set(:@array_serialization_format, DeepCloneUtils.deep_copy(@array_serialization_format)) clone.instance_variable_set(:@xml_attributes, DeepCloneUtils.deep_copy(@xml_attributes)) clone end |
#form_param(form_param) ⇒ RequestBuilder
The setter for the form parameter to be sent in the request.
85 86 87 88 89 |
# File 'lib/apimatic-core/request_builder.rb', line 85 def form_param(form_param) form_param.validate @form_params[form_param.get_key] = form_param.get_value self end |
#get_parameter_value_by_json_pointer(json_pointer) ⇒ Object?
Extracts the initial pagination offset value from the request builder using the specified JSON pointer.
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/apimatic-core/request_builder.rb', line 372 def get_parameter_value_by_json_pointer(json_pointer) param_pointer, field_pointer = JsonPointerHelper.split_into_parts(json_pointer) case param_pointer when PATH_PARAM_POINTER JsonPointerHelper.get_value_by_json_pointer( @template_params, "#{field_pointer}/value" ) when QUERY_PARAM_POINTER JsonPointerHelper.get_value_by_json_pointer(@query_params, field_pointer) when HEADER_PARAM_POINTER JsonPointerHelper.get_value_by_json_pointer(@header_params, field_pointer) when BODY_PARAM_POINTER JsonPointerHelper.get_value_by_json_pointer( @body_params || @form_params, field_pointer ) else nil end end |
#get_part(multipart_param) ⇒ UploadIO
Processes the part of a multipart request and assign appropriate part value and its content-type.
269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/apimatic-core/request_builder.rb', line 269 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_request_by_json_pointer(json_pointer, value) ⇒ Object
Updates the given request builder by modifying its path, query, or header parameters based on the specified JSON pointer and value.
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/apimatic-core/request_builder.rb', line 323 def get_updated_request_by_json_pointer(json_pointer, value) param_pointer, field_pointer = JsonPointerHelper.split_into_parts(json_pointer) template_params = nil query_params = nil header_params = nil body_params = nil form_params = nil case param_pointer when PATH_PARAM_POINTER template_params = JsonPointerHelper.update_entry_by_json_pointer( DeepCloneUtils.deep_copy(@template_params), "#{field_pointer}/value", value ) when QUERY_PARAM_POINTER query_params = JsonPointerHelper.update_entry_by_json_pointer( DeepCloneUtils.deep_copy(@query_params), field_pointer, value ) when HEADER_PARAM_POINTER header_params = JsonPointerHelper.update_entry_by_json_pointer( DeepCloneUtils.deep_copy(@header_params), field_pointer, value ) when BODY_PARAM_POINTER if @body_params body_params = JsonPointerHelper.update_entry_by_json_pointer( DeepCloneUtils.deep_copy(@body_params), field_pointer, value ) else form_params = JsonPointerHelper.update_entry_by_json_pointer( DeepCloneUtils.deep_copy(@form_params), field_pointer, value ) end else clone_with end clone_with( template_params: template_params, query_params: query_params, header_params: header_params, body_params: body_params, form_params: form_params ) end |
#get_updated_url_with_query_params(url) ⇒ String
Returns the URL with resolved query parameters if any.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
# File 'lib/apimatic-core/request_builder.rb', line 197 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.
163 164 165 166 |
# File 'lib/apimatic-core/request_builder.rb', line 163 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.
67 68 69 70 71 |
# File 'lib/apimatic-core/request_builder.rb', line 67 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.
49 50 51 52 |
# File 'lib/apimatic-core/request_builder.rb', line 49 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.
110 111 112 113 114 |
# File 'lib/apimatic-core/request_builder.rb', line 110 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.
41 42 43 44 |
# File 'lib/apimatic-core/request_builder.rb', line 41 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).
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/apimatic-core/request_builder.rb', line 241 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_params.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).
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/apimatic-core/request_builder.rb', line 215 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 _serialized_headers = _request_headers.transform_values do |value| value.nil? ? value : ApiHelper.json_serialize(value) end _serialized_headers end |
#process_url ⇒ String
Processes and resolves the endpoint URL.
186 187 188 189 190 191 192 |
# File 'lib/apimatic-core/request_builder.rb', line 186 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_parameters ⇒ String
Returns The serialized xml body.
284 285 286 287 288 289 290 291 292 |
# File 'lib/apimatic-core/request_builder.rb', line 284 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.
76 77 78 79 80 |
# File 'lib/apimatic-core/request_builder.rb', line 76 def query_param(query_param) query_param.validate @query_params[query_param.get_key] = query_param.get_value self end |
#resolve_body_param ⇒ Hash
Resolves the body parameter to appropriate type.
296 297 298 299 300 301 302 303 304 305 306 |
# File 'lib/apimatic-core/request_builder.rb', line 296 def resolve_body_param if !@body_params.nil? && @body_params.is_a?(FileWrapper) @header_params['content-type'] = @body_params.content_type if !@body_params.file.nil? && !@body_params.content_type.nil? @header_params['content-length'] = @body_params.file.size.to_s return @body_params.file elsif !@body_params.nil? && @body_params.is_a?(File) @header_params['content-length'] = @body_params.size.to_s end @body_params end |
#server(server) ⇒ RequestBuilder
The setter for the server.
33 34 35 36 |
# File 'lib/apimatic-core/request_builder.rb', line 33 def server(server) @server = server self end |
#template_param(template_param) ⇒ RequestBuilder
The setter for the template parameter of the request.
57 58 59 60 61 62 |
# File 'lib/apimatic-core/request_builder.rb', line 57 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.
157 158 159 160 |
# File 'lib/apimatic-core/request_builder.rb', line 157 def xml_attributes(xml_attributes) @xml_attributes = xml_attributes self end |