Class: MagicAdmin::Http::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/magic-admin/http/client.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_base, req_retries, req_timeout, req_backoff) ⇒ Client

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>)


46
47
48
49
50
51
52
53
# 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

Instance Attribute Details

#backoffObject (readonly)

attribute reader for magic api backoff factor



16
17
18
# File 'lib/magic-admin/http/client.rb', line 16

def backoff
  @backoff
end

#base_urlObject (readonly)

attribute reader for magic api base api url



22
23
24
# File 'lib/magic-admin/http/client.rb', line 22

def base_url
  @base_url
end

#http_requestObject (readonly)

attribute reader for magic http request class



25
26
27
# File 'lib/magic-admin/http/client.rb', line 25

def http_request
  @http_request
end

#http_responseObject (readonly)

attribute reader for magic http response class



28
29
30
# File 'lib/magic-admin/http/client.rb', line 28

def http_response
  @http_response
end

#retriesObject (readonly)

attribute reader for magic api max retries



13
14
15
# File 'lib/magic-admin/http/client.rb', line 13

def retries
  @retries
end

#timeoutObject (readonly)

attribute reader for magic api timeout



19
20
21
# File 'lib/magic-admin/http/client.rb', line 19

def timeout
  @timeout
end

Instance Method Details

#call(method, path, options) ⇒ Object

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


65
66
67
68
69
70
71
72
# 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