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 Method Summary collapse
-
#endpoint_context(context_key, context_value) ⇒ ApiCall
The setter for the context for an endpoint call.
-
#endpoint_name_for_logging(endpoint_name_for_logging) ⇒ ApiCall
The setter for the name of the endpoint controller method to used while logging an endpoint call.
-
#execute ⇒ Object
Executes the API call using provided HTTP client, request builder and response handler objects.
-
#initialize(global_configuration, logger: nil) ⇒ ApiCall
constructor
Initializes a new instance of ApiCall.
-
#new_builder ⇒ ApiCall
Creates a new builder instance of the API call with pre-configured global and logging configurations.
-
#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, log_message) ⇒ Object
Registers request and response with the provided http_callback.
Constructor Details
#initialize(global_configuration, logger: nil) ⇒ ApiCall
Initializes a new instance of ApiCall.
13 14 15 16 17 18 19 20 |
# File 'lib/apimatic-core/api_call.rb', line 13 def initialize(global_configuration, logger: nil) @global_configuration = global_configuration @request_builder = RequestBuilder.new @response_handler = ResponseHandler.new @endpoint_logger = EndpointLogger.new(logger) @endpoint_name_for_logging = nil @endpoint_context = {} end |
Instance Method Details
#endpoint_context(context_key, context_value) ⇒ ApiCall
The setter for the context for an endpoint call.
50 51 52 53 |
# File 'lib/apimatic-core/api_call.rb', line 50 def endpoint_context(context_key, context_value) @endpoint_context[context_key] = context_value self end |
#endpoint_name_for_logging(endpoint_name_for_logging) ⇒ ApiCall
The setter for the name of the endpoint controller method to used while logging an endpoint call.
41 42 43 44 |
# File 'lib/apimatic-core/api_call.rb', line 41 def endpoint_name_for_logging(endpoint_name_for_logging) @endpoint_name_for_logging = endpoint_name_for_logging self end |
#execute ⇒ Object
Executes the API call using provided HTTP client, request builder and response handler objects.
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 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/apimatic-core/api_call.rb', line 57 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.endpoint_logger(@endpoint_logger) .endpoint_name_for_logging(@endpoint_name_for_logging) .global_configuration(@global_configuration) .build(@endpoint_context) @endpoint_logger.debug("Raw request for #{@endpoint_name_for_logging} is: #{_http_request.inspect}") _http_callback = _client_configuration.http_callback unless _http_callback.nil? update_http_callback(proc do _http_callback&.on_before_request(_http_request) end, "Calling the on_before_request method of http_call_back for #{@endpoint_name_for_logging}.") end _http_response = _client_configuration.http_client.execute(_http_request) @endpoint_logger.debug("Raw response for #{@endpoint_name_for_logging} is: #{_http_response.inspect}") unless _http_callback.nil? update_http_callback(proc do _http_callback&.on_after_response(_http_response) end, "Calling the on_after_response method of http_call_back for #{@endpoint_name_for_logging}.") end _deserialized_response = @response_handler.endpoint_logger(@endpoint_logger) .endpoint_name_for_logging(@endpoint_name_for_logging) .handle(_http_response, @global_configuration.get_global_errors, @global_configuration.should_symbolize_hash) _deserialized_response rescue StandardError => e @endpoint_logger.error(e) raise e end end |
#new_builder ⇒ ApiCall
Creates a new builder instance of the API call with pre-configured global and logging configurations.
6 7 8 |
# File 'lib/apimatic-core/api_call.rb', line 6 def new_builder ApiCall.new(@global_configuration, logger: @endpoint_logger.logger) end |
#request(request_builder) ⇒ ApiCall
The setter for the request builder to be used for building the request of an API call.
25 26 27 28 |
# File 'lib/apimatic-core/api_call.rb', line 25 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.
33 34 35 36 |
# File 'lib/apimatic-core/api_call.rb', line 33 def response(response_handler) @response_handler = response_handler self end |
#update_http_callback(callable, log_message) ⇒ Object
Registers request and response with the provided http_callback
100 101 102 103 |
# File 'lib/apimatic-core/api_call.rb', line 100 def update_http_callback(callable, ) @endpoint_logger.info() callable.call end |