Class: Algolia::Transport::Transport
- Inherits:
-
Object
- Object
- Algolia::Transport::Transport
- Includes:
- CallType, Helpers, 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.
-
#read(method, path, body = {}, opts = {}) ⇒ Object
Build a request with call type READ.
-
#request(call_type, method, path, body = {}, opts = {}) ⇒ Response
Response of the request.
-
#write(method, path, body = {}, opts = {}) ⇒ Object
Build a request with call type WRITE.
Methods included from Helpers
#check_array, #check_object, #chunk, #deserialize_settings, #get_object_id, #get_option, #handle_params, #hash_includes_subset?, included, #json_to_hash, #path_encode, #symbolize_hash, #to_json, #to_query_string
Constructor Details
#initialize(config, requester) ⇒ Transport
Returns a new instance of Transport.
13 14 15 16 17 |
# File 'lib/algolia/transport/transport.rb', line 13 def initialize(config, requester) @config = config @http_requester = requester @retry_strategy = RetryStrategy.new(config) end |
Instance Method Details
#read(method, path, body = {}, opts = {}) ⇒ Object
Build a request with call type READ
26 27 28 |
# File 'lib/algolia/transport/transport.rb', line 26 def read(method, path, body = {}, opts = {}) request(READ, method, path, body, opts) end |
#request(call_type, method, path, body = {}, opts = {}) ⇒ Response
Returns response of the request.
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/algolia/transport/transport.rb', line 49 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) .params.merge!(.data) if method == :GET request = build_request(method, path, body, ) response = @http_requester.send_request( host, request[:method], request[:path], request[:body], request[:headers], 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_to_hash(response.error, @config.symbolize_keys) raise AlgoliaHttpError.new(get_option(decoded_error, 'status'), get_option(decoded_error, 'message')) end return json_to_hash(response.body, @config.symbolize_keys) unless outcome == RETRY end raise AlgoliaUnreachableHostError, 'Unreachable hosts' end |
#write(method, path, body = {}, opts = {}) ⇒ Object
Build a request with call type WRITE
37 38 39 |
# File 'lib/algolia/transport/transport.rb', line 37 def write(method, path, body = {}, opts = {}) request(WRITE, method, path, body, opts) end |