class ProductAggregator::Connection
ProductAggregator::Connection
provides an instance to connect to product aggregator service.
Constants
- GET_PRODUCT
- RETRIABLE_EXCEPTIONS
- RETRIABLE_METHODS
Public Class Methods
get_product(product_id, origin = 'readonly')
click to toggle source
# File lib/product-aggregator/connection.rb, line 16 def get_product(product_id, origin = 'readonly') endpoint = "#{GET_PRODUCT}#{product_id}?origin=#{origin}" begin response = connection.get endpoint do |req| req.options.timeout = 0.2 req.options.open_timeout = 1 end result = ::JSON.parse(response.body) return result['data'] if response.status == 200 msg = result['errors'][0]['message'] if result['errors'] && result['errors'][0] e = case response.status when 401 ProductAggregator::UnauthorizedError.new(msg) when 404 ProductAggregator::NotFoundError.new(msg) when 500 ProductAggregator::InternalServerError.new(msg) else ProductAggregator::Error.new(msg) end raise e rescue Faraday::ConnectionFailed, Faraday::TimeoutError, Faraday::Error => e ProductAggregator.configuration.logger&.error(message: "GET #{endpoint} TIMEOUT: #{e.message}") raise e end end
Private Class Methods
connection()
click to toggle source
# File lib/product-aggregator/connection.rb, line 48 def connection @connection ||= Faraday.new(url: ProductAggregator.configuration.host) do |f| f.use :circuit_breaker, timeout: ProductAggregator.configuration.circuit_breaker_timeout, threshold: ProductAggregator.configuration.circuit_breaker_threshold, fallback: ->(_, exception){ raise exception if exception; Faraday::Response.new(status: 503, response_headers: {}) } f.use Faraday::Request::BasicAuthentication, ProductAggregator.configuration.user, ProductAggregator.configuration.password f.request :retry, max: ProductAggregator.configuration.max_retry.to_i, interval: 0.1, backoff_factor: 2, methods: RETRIABLE_METHODS, exceptions: RETRIABLE_EXCEPTIONS f.adapter :net_http_persistent end end