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.
15 16 17 18 19 |
# File 'lib/algolia/transport/transport.rb', line 15 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
28 29 30 |
# File 'lib/algolia/transport/transport.rb', line 28 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.
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 79 80 |
# File 'lib/algolia/transport/transport.rb', line 51 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
39 40 41 |
# File 'lib/algolia/transport/transport.rb', line 39 def write(method, path, body = {}, opts = {}) request(WRITE, method, path, body, opts) end |