Class: Algolia::Transport::Transport

Inherits:
Object
  • Object
show all
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

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.

Parameters:

  • config (Search::Config)

    config used for search

  • requester (Object)

    requester used for sending requests. Uses Algolia::Http::HttpRequester by default



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

Parameters:

  • method (Symbol)

    method used for request

  • path (String)

    path of the request

  • body (Hash) (defaults to: {})

    request body

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



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.

Parameters:

  • call_type (Binary)

    READ or WRITE operation

  • method (Symbol)

    method used for request

  • path (String)

    path of the request

  • body (Hash) (defaults to: {})

    request body

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query

Returns:

  • (Response)

    response of the request

Raises:



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)

    request_options = RequestOptions.new(@config)
    request_options.create(opts)
    request_options.params.merge!(request_options.data) if method == :GET

    request  = build_request(method, path, body, request_options)
    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

Parameters:

  • method (Symbol)

    method used for request

  • path (String)

    path of the request

  • body (Hash) (defaults to: {})

    request body

  • opts (Hash) (defaults to: {})

    contains extra parameters to send with your query



39
40
41
# File 'lib/algolia/transport/transport.rb', line 39

def write(method, path, body = {}, opts = {})
  request(WRITE, method, path, body, opts)
end