Class: Routemaster::APIClient

Inherits:
Object
  • Object
show all
Defined in:
lib/routemaster/api_client.rb

Constant Summary collapse

@@root_resources =

Memoize the root resources at Class level so that we don't hit the cache all the time to fetch the root resource before doing anything else.

{}

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ APIClient

Returns a new instance of APIClient.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/routemaster/api_client.rb', line 37

def initialize(options = {})
  @listener               = options.fetch :listener, nil
  @middlewares            = options.fetch :middlewares, []
  @default_response_class = options.fetch :response_class, nil
  @metrics_client         = options.fetch :metrics_client, nil
  @source_peer            = options.fetch :source_peer, nil
  @retry_attempts         = options.fetch :retry_attempts, 2
  @retry_methods          = options.fetch :retry_methods, Faraday::Request::Retry::IDEMPOTENT_METHODS

  connection # warm up connection so Faraday does all it's magical file loading in the main thread
end

Instance Method Details

#delete(url, headers: {}) ⇒ Object



89
90
91
# File 'lib/routemaster/api_client.rb', line 89

def delete(url, headers: {})
  _request(:delete, url: url, body: nil, headers: headers)
end

#discover(url) ⇒ Object



93
94
95
# File 'lib/routemaster/api_client.rb', line 93

def discover(url)
  @@root_resources[url] ||= get(url)
end

#fget(url, **options) ⇒ Object

Same as {get}, except with



68
69
70
71
# File 'lib/routemaster/api_client.rb', line 68

def fget(url, **options)
  uri = _assert_uri(url)
  Responses::ResponsePromise.new { get(uri, options) }
end

#get(url, params: {}, headers: {}, options: {}) ⇒ Object

Performs a GET HTTP request for the url, with optional query parameters (params) and additional headers (headers).

and body. The body is a Hashie::Mash if the response was JSON, a string otherwise.

Returns:

  • an object that responds to status (integer), headers (hash),



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/routemaster/api_client.rb', line 55

def get(url, params: {}, headers: {}, options: {})
  enable_caching = options.fetch(:enable_caching, true)
  response_class = options[:response_class]

  _wrapped_response _request(
    :get,
    url: url,
    params: params,
    headers: headers.merge(response_cache_opt_headers(enable_caching))),
    response_class: response_class
end

#patch(url, body: {}, headers: {}) ⇒ Object



81
82
83
84
85
86
87
# File 'lib/routemaster/api_client.rb', line 81

def patch(url, body: {}, headers: {})
  _wrapped_response _request(
    :patch,
    url: url,
    body: body,
    headers: headers)
end

#post(url, body: {}, headers: {}) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/routemaster/api_client.rb', line 73

def post(url, body: {}, headers: {})
  _wrapped_response _request(
    :post,
    url: url,
    body: body,
    headers: headers)
end