class MagicAdmin::Http::Client
Http
Client
and its methods are accessible on the Magic
instance by the http_client attribute. It provides methods to interact with the http client.
Attributes
backoff[R]
attribute reader for magic api backoff factor
base_url[R]
attribute reader for magic api base api url
http_request[R]
attribute reader for magic http request class
http_response[R]
attribute reader for magic http response class
retries[R]
attribute reader for magic api max retries
timeout[R]
attribute reader for magic api timeout
Public Class Methods
new(api_base, req_retries, req_timeout, req_backoff)
click to toggle source
The constructor allows you to configure HTTP request strategy when your application interacting with the Magic
API
Arguments:
api_base: api base url. req_retries: Total number of retries to allow. req_timeout: A period of time the request is going to wait for a response. req_backoff: A backoff factor to apply between retry attempts.
Returns:
A Http Client object that provides access to all the supported resources.
Examples:
Client.new(<api_base>, <req_retries>, <req_timeout>, <req_backoff>)
# File lib/magic-admin/http/client.rb, line 46 def initialize(api_base, req_retries, req_timeout, req_backoff) @retries = req_retries.to_i @backoff = req_backoff.to_f @timeout = req_timeout.to_f @base_url = api_base @http_request = Request @http_response = Response end
Public Instance Methods
call(method, path, options)
click to toggle source
Description:
call create http request and provide response
Arguments:
method: http method path: api path options: a hash contains params and headers for request
Returns:
A response object
# File lib/magic-admin/http/client.rb, line 65 def call(method, path, options) url = URI("#{base_url}#{path}") req = http_request.request(method, url, options) resp = backoff_retries(retries, backoff) do base_client(url, req, timeout) end http_response.from_net_http(resp, req) end
Private Instance Methods
backoff_retries(max_retries, backoff_factor, &block)
click to toggle source
Description:
backoff_retries implementations of retries strategy with backoff factor
Arguments:
max_retries: max retries count backoff_factor: backoff factor for configure delay in retries block: block of code that uses retries backoff strategy
Returns:
it returns, block return object
# File lib/magic-admin/http/client.rb, line 87 def backoff_retries(max_retries, backoff_factor, &block) attempts = 0 begin attempts += 1 block.call rescue StandardError => e raise e if attempts >= max_retries sleep_seconds = backoff_factor * (2**(attempts - 1)) sleep sleep_seconds retry end end
base_client(url, request, read_timeout)
click to toggle source
Description:
base_client is base http request/response mechanism
Arguments:
url: request url request: request object read_timeout: read_timeout for request
Returns:
response
# File lib/magic-admin/http/client.rb, line 123 def base_client(url, request, read_timeout) begin Net::HTTP.start(url.host, url.port, use_ssl: use_ssl?(url)) do |http| http.read_timeout = read_timeout http.request(request) end rescue SocketError => e raise APIConnectionError.new(e.message) end end
use_ssl?(url)
click to toggle source
Description:
use_ssl? provide true if url uses https protocol otherwise false
Arguments:
url: max retries count
Returns:
boolean value
# File lib/magic-admin/http/client.rb, line 109 def use_ssl?(url) url.scheme == "https" end