class Routemaster::APIClient
Constants
- DEFAULT_USER_AGENT
Public Class Methods
new(options = {})
click to toggle source
# 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
Public Instance Methods
delete(url, headers: {})
click to toggle source
# File lib/routemaster/api_client.rb, line 90 def delete(url, headers: {}) _request(:delete, url: url, body: nil, headers: headers) end
discover(url)
click to toggle source
# File lib/routemaster/api_client.rb, line 94 def discover(url) @@root_resources[url] ||= get(url) end
fget(url, **options)
click to toggle source
Same as {{get}}, except with
# 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: {})
click to toggle source
Performs a GET HTTP request for the ‘url`, with optional query parameters (`params`) and additional headers (`headers`).
@return an object that responds to ‘status` (integer), `headers` (hash), and `body`. The body is a `Hashie::Mash` if the response was JSON, a string otherwise.
# 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: {})
click to toggle source
# 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: {})
click to toggle source
# 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: {})
click to toggle source
# File lib/routemaster/api_client.rb, line 86 def put(url, body: {}, headers: {}) patch_post_or_put(:put, url, body, headers) end
Private Instance Methods
_assert_uri(url)
click to toggle source
# File lib/routemaster/api_client.rb, line 110 def _assert_uri(url) return url if url.kind_of?(URI) URI.parse(url) end
_request(method, url:, body: nil, headers:, params: {})
click to toggle source
# File lib/routemaster/api_client.rb, line 115 def _request(method, url:, body: nil, headers:, params: {}) uri = _assert_uri(url) auth = auth_header(uri.host) headers = [*user_agent_header, *auth, *headers].to_h connection.public_send(method) do |req| req.url uri.to_s req.params.merge! params req.headers = headers req.body = body end end
_wrapped_response(response, response_class: nil)
click to toggle source
# File lib/routemaster/api_client.rb, line 127 def _wrapped_response(response, response_class: nil) response_class = response_class || @default_response_class response_class ? response_class.new(response, client: self) : response end
auth_header(host)
click to toggle source
# File lib/routemaster/api_client.rb, line 159 def auth_header(host) auth_string = Config.cache_auth.fetch(host, []).join(':') { 'Authorization' => "Basic #{Base64.strict_encode64(auth_string)}" } end
connection()
click to toggle source
# File lib/routemaster/api_client.rb, line 132 def connection @connection ||= Faraday.new do |f| f.request :json f.request :retry, max: @retry_attempts, interval: 100e-3, backoff_factor: 2, methods: @retry_methods, exceptions: @retry_exceptions f.response :mashify f.response :json, content_type: /\bjson/ f.use Routemaster::Middleware::ResponseCaching, listener: @listener f.use Routemaster::Middleware::Metrics, client: @metrics_client, source_peer: @source_peer f.use Routemaster::Middleware::ErrorHandling @middlewares.each do |middleware| f.use(*middleware) end f.adapter :typhoeus f.options.timeout = (@timeout || ENV.fetch('ROUTEMASTER_CACHE_TIMEOUT', 1)).to_f f.options.open_timeout = (@open_timeout || ENV.fetch('ROUTEMASTER_CACHE_TIMEOUT', 1)).to_f f.ssl.verify = ENV.fetch('ROUTEMASTER_CACHE_VERIFY_SSL', 'false') == 'true' end end
patch_post_or_put(type, url, body, headers)
click to toggle source
# File lib/routemaster/api_client.rb, line 100 def patch_post_or_put(type, url, body, headers) APIClientCircuit.new(url).call do _wrapped_response _request( type, url: url, body: body, headers: headers) end end
response_cache_opt_headers(value)
click to toggle source
# File lib/routemaster/api_client.rb, line 168 def response_cache_opt_headers(value) { Routemaster::Middleware::ResponseCaching::RESPONSE_CACHING_OPT_HEADER => value.to_s } end
user_agent_header()
click to toggle source
# File lib/routemaster/api_client.rb, line 164 def user_agent_header { 'User-Agent' => @source_peer || DEFAULT_USER_AGENT } end