Class: CoreLibrary::ApiCall
- Inherits:
-
Object
- Object
- CoreLibrary::ApiCall
- Defined in:
- lib/apimatic-core/api_call.rb
Overview
This class is responsible for executing an API call using HttpClient, RequestBuilder and ResponseHandler objects.
Instance Attribute Summary collapse
-
#global_configuration ⇒ Object
readonly
Returns the value of attribute global_configuration.
-
#pagination_strategy_list ⇒ Object
readonly
Returns the value of attribute pagination_strategy_list.
-
#request_builder ⇒ Object
readonly
Returns the value of attribute request_builder.
Instance Method Summary collapse
-
#clone_with(global_configuration: nil, request_builder: nil) ⇒ ApiCall
Creates a deep clone of the ApiCall instance with optional overrides.
-
#endpoint_context(context_key, context_value) ⇒ ApiCall
The setter for the context for an endpoint call.
-
#execute ⇒ Object
Executes the API call using provided HTTP client, request builder and response handler objects.
-
#initialize(global_configuration) ⇒ ApiCall
constructor
Initializes a new instance of ApiCall.
- #initialize_api_logger(logging_config) ⇒ Object
-
#paginate(page_iterable_creator, paginated_items_converter) ⇒ Object
Invokes the given page iterable creator with a PaginatedData instance.
-
#pagination_strategies(*pagination_strategies) ⇒ self
Sets the pagination strategies for this instance.
-
#request(request_builder) ⇒ ApiCall
The setter for the request builder to be used for building the request of an API call.
-
#response(response_handler) ⇒ ApiCall
The setter for the response handler to be used for handling the response of an API call.
-
#update_http_callback(callable) ⇒ Object
Registers request and response with the provided http_callback.
Constructor Details
#initialize(global_configuration) ⇒ ApiCall
Initializes a new instance of ApiCall.
8 9 10 11 12 13 14 15 |
# File 'lib/apimatic-core/api_call.rb', line 8 def initialize(global_configuration) @global_configuration = global_configuration @request_builder = RequestBuilder.new @response_handler = ResponseHandler.new @endpoint_context = {} initialize_api_logger(@global_configuration.client_configuration.logging_configuration) @pagination_strategy_list = nil end |
Instance Attribute Details
#global_configuration ⇒ Object (readonly)
Returns the value of attribute global_configuration.
4 5 6 |
# File 'lib/apimatic-core/api_call.rb', line 4 def global_configuration @global_configuration end |
#pagination_strategy_list ⇒ Object (readonly)
Returns the value of attribute pagination_strategy_list.
4 5 6 |
# File 'lib/apimatic-core/api_call.rb', line 4 def pagination_strategy_list @pagination_strategy_list end |
#request_builder ⇒ Object (readonly)
Returns the value of attribute request_builder.
4 5 6 |
# File 'lib/apimatic-core/api_call.rb', line 4 def request_builder @request_builder end |
Instance Method Details
#clone_with(global_configuration: nil, request_builder: nil) ⇒ ApiCall
Creates a deep clone of the ApiCall instance with optional overrides.
This method is useful for duplicating an API call while changing select parts like the request builder or pagination strategies.
121 122 123 124 125 126 127 128 129 130 |
# File 'lib/apimatic-core/api_call.rb', line 121 def clone_with(global_configuration: nil, request_builder: nil) clone = ApiCall.new(global_configuration || @global_configuration) clone.request(request_builder || @request_builder.clone_with) clone.response(@response_handler) clone.set_endpoint_context(DeepCloneUtils.deep_copy(@endpoint_context)) clone.pagination_strategies(*pagination_strategy_list) if pagination_strategy_list clone end |
#endpoint_context(context_key, context_value) ⇒ ApiCall
The setter for the context for an endpoint call.
37 38 39 40 |
# File 'lib/apimatic-core/api_call.rb', line 37 def endpoint_context(context_key, context_value) @endpoint_context[context_key] = context_value self end |
#execute ⇒ Object
Executes the API call using provided HTTP client, request builder and response handler objects.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/apimatic-core/api_call.rb', line 53 def execute _client_configuration = @global_configuration.client_configuration begin if _client_configuration.http_client.nil? raise ArgumentError, 'An HTTP client instance is required to execute an Api call.' end _http_request = @request_builder.global_configuration(@global_configuration) .build(@endpoint_context) @logger.log_request(_http_request) _http_callback = _client_configuration.http_callback unless _http_callback.nil? update_http_callback(proc do _http_callback&.on_before_request(_http_request) end) end _http_response = _client_configuration.http_client.execute(_http_request) @logger.log_response(_http_response) unless _http_callback.nil? update_http_callback(proc do _http_callback&.on_after_response(_http_response) end) end _deserialized_response = @response_handler.handle(_http_response, @global_configuration.get_global_errors, @global_configuration.should_symbolize_hash) _deserialized_response rescue StandardError => e raise e end end |
#initialize_api_logger(logging_config) ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/apimatic-core/api_call.rb', line 104 def initialize_api_logger(logging_config) @logger = if logging_config.nil? NilSdkLogger.new else SdkLogger.new(logging_config) end end |
#paginate(page_iterable_creator, paginated_items_converter) ⇒ Object
Invokes the given page iterable creator with a PaginatedData instance.
94 95 96 |
# File 'lib/apimatic-core/api_call.rb', line 94 def paginate(page_iterable_creator, paginated_items_converter) page_iterable_creator.call(PaginatedData.new(self, paginated_items_converter)) end |
#pagination_strategies(*pagination_strategies) ⇒ self
Sets the pagination strategies for this instance.
46 47 48 49 |
# File 'lib/apimatic-core/api_call.rb', line 46 def pagination_strategies(*pagination_strategies) @pagination_strategy_list = pagination_strategies self end |
#request(request_builder) ⇒ ApiCall
The setter for the request builder to be used for building the request of an API call.
20 21 22 23 |
# File 'lib/apimatic-core/api_call.rb', line 20 def request(request_builder) @request_builder = request_builder self end |
#response(response_handler) ⇒ ApiCall
The setter for the response handler to be used for handling the response of an API call.
28 29 30 31 |
# File 'lib/apimatic-core/api_call.rb', line 28 def response(response_handler) @response_handler = response_handler self end |
#update_http_callback(callable) ⇒ Object
Registers request and response with the provided http_callback
100 101 102 |
# File 'lib/apimatic-core/api_call.rb', line 100 def update_http_callback(callable) callable.call end |