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.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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
  @retry_exceptions       = options.fetch :retry_exceptions, Faraday::Request::Retry::Options.new.exceptions
  @timeout                = options.fetch :timeout, nil
  @open_timeout           = options.fetch :open_timeout, nil

  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



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

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

#discover(url) ⇒ Object



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

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

#fget(url, **options) ⇒ Object

Same as {get}, except with



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

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



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

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



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

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

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



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

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

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



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

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