Class: CoreLibrary::HttpCallContext

Inherits:
HttpCallback
  • Object
show all
Defined in:
lib/apimatic-core/http/http_call_context.rb

Overview

HttpCallContext is a callback class used to capture the HTTP request and response lifecycle during an API call. It is intended to be passed to an HTTP client or controller that supports pre- and post-request hooks.

This class stores references to the request and response objects, making them accessible after the API call is completed. This can be useful for debugging, logging, or validation purposes.

Example usage:

context = CoreLibrary::HttpCallContext.new
client.execute_request(request, context)
puts context.request  # Inspect the HttpRequest
puts context.response # Inspect the HttpResponse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(user_provided_http_callback = nil) ⇒ HttpCallContext

Initializes a new instance of HttpCallContext.

Parameters:

  • user_provided_http_callback (HttpCallback, nil) (defaults to: nil)

    An optional user-defined callback that will be triggered before and after the HTTP request.



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

def initialize(user_provided_http_callback = nil)
  @request = nil
  @response = nil
  @http_callback = user_provided_http_callback
end

Instance Attribute Details

#requestHttpRequest? (readonly)

Returns The HTTP request object that was sent.

Returns:

  • (HttpRequest, nil)

    The HTTP request object that was sent.



17
18
19
# File 'lib/apimatic-core/http/http_call_context.rb', line 17

def request
  @request
end

#responseHttpResponse? (readonly)

Returns The HTTP response object that was received.

Returns:

  • (HttpResponse, nil)

    The HTTP response object that was received.



20
21
22
# File 'lib/apimatic-core/http/http_call_context.rb', line 20

def response
  @response
end

Instance Method Details

#on_after_response(response) ⇒ Object

Called after receiving the HTTP response. Stores the response and invokes the user-provided callback, if any.

Parameters:

  • response (HttpResponse)

    The HttpResponse of the API call.



45
46
47
48
# File 'lib/apimatic-core/http/http_call_context.rb', line 45

def on_after_response(response)
  @response = response
  @http_callback&.on_after_response(response)
end

#on_before_request(request) ⇒ Object

Called before making the HTTP request. Stores the request and invokes the user-provided callback, if any.

Parameters:

  • request (HttpRequest)

    The request object to be sent to the HttpClient.



36
37
38
39
# File 'lib/apimatic-core/http/http_call_context.rb', line 36

def on_before_request(request)
  @request = request
  @http_callback&.on_before_request(request)
end