Class: Algolia::Transport::Transport
- Inherits:
-
Object
- Object
- Algolia::Transport::Transport
- Includes:
- CallType, RetryOutcomeType
- Defined in:
- lib/algolia/transport/transport.rb
Constant Summary
Constants included from CallType
CallType::READ, CallType::WRITE
Constants included from RetryOutcomeType
RetryOutcomeType::FAILURE, RetryOutcomeType::RETRY, RetryOutcomeType::SUCCESS
Instance Method Summary collapse
-
#initialize(config, requester) ⇒ Transport
constructor
A new instance of Transport.
-
#request(call_type, method, path, body, opts = {}) ⇒ Response
Response of the request.
Constructor Details
#initialize(config, requester) ⇒ Transport
Returns a new instance of Transport.
19 20 21 22 23 |
# File 'lib/algolia/transport/transport.rb', line 19 def initialize(config, requester) @config = config @requester = requester @retry_strategy = RetryStrategy.new(config.hosts) end |
Instance Method Details
#request(call_type, method, path, body, opts = {}) ⇒ Response
Returns response of the request.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/algolia/transport/transport.rb', line 33 def request(call_type, method, path, body, opts = {}) @retry_strategy.get_tryable_hosts(call_type).each do |host| opts[:timeout] ||= get_timeout(call_type) * (host.retry_count + 1) opts[:connect_timeout] ||= @config.connect_timeout * (host.retry_count + 1) = RequestOptions.new(@config) .create(opts) # TODO: what is this merge for ? # request_options.query_params.merge!(request_options.data) if method == :GET request = build_request(method, path, body, ) response = @requester.send_request( host, request[:method], request[:path], request[:body], request[:query_params], request[:header_params], request[:timeout], request[:connect_timeout] ) outcome = @retry_strategy.decide(host, http_response_code: response.status, is_timed_out: response.has_timed_out, network_failure: response.network_failure) if outcome == FAILURE decoded_error = JSON.parse(response.error, :symbolize_names => true) raise Algolia::AlgoliaHttpError.new(decoded_error[:status], decoded_error[:message]) end return response unless outcome == RETRY end raise Algolia::AlgoliaUnreachableHostError, 'Unreachable hosts' end |