Class: Routemaster::APIClient
- Inherits:
-
Object
- Object
- Routemaster::APIClient
- 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
- #delete(url, headers: {}) ⇒ Object
- #discover(url) ⇒ Object
-
#fget(url, **options) ⇒ Object
Same as {get}, except with.
-
#get(url, params: {}, headers: {}, options: {}) ⇒ Object
Performs a GET HTTP request for the
url
, with optional query parameters (params
) and additional headers (headers
). -
#initialize(options = {}) ⇒ APIClient
constructor
A new instance of APIClient.
- #patch(url, body: {}, headers: {}) ⇒ Object
- #post(url, body: {}, headers: {}) ⇒ Object
- #put(url, body: {}, headers: {}) ⇒ Object
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( = {}) @listener = .fetch :listener, nil @middlewares = .fetch :middlewares, [] @default_response_class = .fetch :response_class, nil @metrics_client = .fetch :metrics_client, nil @source_peer = .fetch :source_peer, nil @retry_attempts = .fetch :retry_attempts, 2 @retry_methods = .fetch :retry_methods, Faraday::Request::Retry::IDEMPOTENT_METHODS @retry_exceptions = .fetch :retry_exceptions, Faraday::Request::Retry::Options.new.exceptions @timeout = .fetch :timeout, nil @open_timeout = .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, **) uri = _assert_uri(url) Responses::ResponsePromise.new { get(uri, **) } 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.
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 = .fetch(:enable_caching, true) response_class = [: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 |