Class: Routemaster::APIClient

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

Constant Summary collapse

DEFAULT_USER_AGENT =
ENV.fetch('ROUTEMASTER_API_CLIENT_USER_AGENT') { "RoutemasterDrain - Faraday v#{Faraday::VERSION}" }.freeze
@@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.



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

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
  @retry_exceptions       = options.fetch :retry_exceptions, Faraday::Request::Retry::Options.new.exceptions

  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



72
73
74
75
# File 'lib/routemaster/api_client.rb', line 72

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



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

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



77
78
79
# File 'lib/routemaster/api_client.rb', line 77

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

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



81
82
83
# File 'lib/routemaster/api_client.rb', line 81

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

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



85
86
87
# File 'lib/routemaster/api_client.rb', line 85

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