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.



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

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



87
88
89
# File 'lib/routemaster/api_client.rb', line 87

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

#discover(url) ⇒ Object



91
92
93
# File 'lib/routemaster/api_client.rb', line 91

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

#fget(url, **options) ⇒ Object

Same as {get}, except with



70
71
72
73
# File 'lib/routemaster/api_client.rb', line 70

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



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

def get(url, params: {}, headers: {}, options: {})
  enable_caching = options.fetch(:enable_caching, true)
  response_class = options[:response_class]
  APIClientCircuit.new(url).call do
    _wrapped_response _request(
      :get,
      url: url,
      params: params,
      headers: headers.merge(response_cache_opt_headers(enable_caching))),
      response_class: response_class
  end
end

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



75
76
77
# File 'lib/routemaster/api_client.rb', line 75

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

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



79
80
81
# File 'lib/routemaster/api_client.rb', line 79

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

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



83
84
85
# File 'lib/routemaster/api_client.rb', line 83

def put(url, body: {}, headers: {})
  patch_post_or_put(:put, url, body, headers)
end