Class: Algolia::ApiClient

Inherits:
Object
  • Object
show all
Defined in:
lib/algolia/api_client.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config = Configuration.default, request_id_support: false) ⇒ ApiClient

Initializes the ApiClient

Parameters:

  • request_id_support (true, false) (defaults to: false) —

    the generated per-client Request-ID capability, applied when config.request_id_enabled is nil. Kept on the transport so a shared Configuration is never mutated.

  • config (Hash) (defaults to: Configuration.default) —

    a customizable set of options

Options Hash (config):

  • Configuration (Configuration) —

    for initializing the object, default to Configuration.default



18
19
20
21
22
23
# File 'lib/algolia/api_client.rb', line 18

def initialize(config = Configuration.default, request_id_support: false)
  @config = config
  @requester = config.requester || Http::HttpRequester.new("net_http_persistent", LoggerHelper.create)
  @logger = (@requester.logger if @requester.respond_to?(:logger)) || LoggerHelper.create
  @transporter = Transport::Transport.new(config, @requester, request_id_support: request_id_support)
end

Instance Attribute Details

#config ⇒ Object

The Configuration object holding settings to be used in the API client.



7
8
9
# File 'lib/algolia/api_client.rb', line 7

def config
  @config
end

#logger ⇒ Object (readonly)

The logger, shared with the requester when it provides one.



12
13
14
# File 'lib/algolia/api_client.rb', line 12

def logger
  @logger
end

#transporter ⇒ Object

Returns the value of attribute transporter.



9
10
11
# File 'lib/algolia/api_client.rb', line 9

def transporter
  @transporter
end

Class Method Details

.default ⇒ Object



31
32
33
# File 'lib/algolia/api_client.rb', line 31

def self.default
  @@default ||= ApiClient.new
end

Instance Method Details

#build_collection_param(param, collection_format) ⇒ Object

Build parameter value according to the given collection format.

Parameters:

  • collection_format (String) —

    one of :csv, :ssv, :tsv, :pipes and :multi



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/algolia/api_client.rb', line 149

def build_collection_param(param, collection_format)
  case collection_format
  when :csv
    param.join(",")
  when :ssv
    param.join(" ")
  when :tsv
    param.join("\t")
  when :pipes
    param.join("|")
  when :multi
    # return the array directly as typhoeus will handle it as expected
    param
  else
    raise "unknown collection format: #{collection_format.inspect}"
  end
end

#call_api(http_method, path, opts = {}) ⇒ Http::Response

Call an API with given options.

Returns:



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/algolia/api_client.rb', line 42

def call_api(http_method, path, opts = {})
  begin
    call_type = opts[:use_read_transporter] || http_method == :GET ? CallType::READ : CallType::WRITE
    response = transporter.request(call_type, http_method, path, opts[:body], opts)
  rescue Faraday::TimeoutError
    raise ApiError, "Connection timed out"
  rescue Faraday::ConnectionFailed
    raise ApiError, "Connection failed"
  end

  response
end

#convert_to_type(data, return_type) ⇒ Mixed

Convert data to the given return type.

Parameters:

  • data (Object) —

    Data to be converted

  • return_type (String) —

    Return type

Returns:

  • (Mixed) —

    Data in a particular type



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/algolia/api_client.rb', line 80

def convert_to_type(data, return_type)
  return nil if data.nil?

  case return_type
  when "String"
    data.to_s
  when "Integer"
    data.to_i
  when "Float"
    data.to_f
  when "Boolean"
    data == true
  when "Time"
    # parse date time (expecting ISO 8601 format)
    Time.parse(data)
  when "Date"
    # parse date time (expecting ISO 8601 format)
    Date.parse(data)
  when "Object"
    # generic object (usually a Hash), return directly
    data
  when /\AArray<(.+)>\z/
    # e.g. Array<Pet>
    sub_type = ::Regexp.last_match(1)
    data.map { |item| convert_to_type(item, sub_type) }
  when /\AHash<String, (.+)>\z/
    # e.g. Hash<String, Integer>
    sub_type = ::Regexp.last_match(1)
    {}.tap do |hash|
      data.each { |k, v| hash[k] = convert_to_type(v, sub_type) }
    end
  else
    # models (e.g. Pet), enum, or oneOf
    klass = Algolia.const_get(return_type)
    klass.respond_to?(:openapi_one_of) ? klass.build(data) : klass.build_from_hash(data)
  end
end

#deserialize(body, return_type) ⇒ Object

Deserialize the response to the given return type.

Parameters:

  • body (String) —

    of the HTTP response

  • return_type (String) —

    some examples: "User", "Array", "Hash<String, Integer>"



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/algolia/api_client.rb', line 59

def deserialize(body, return_type)
  return nil if body.nil? || body.empty?

  # return response body directly for String return type
  return body.to_s if return_type == "String"

  begin
    data = JSON.parse("[#{body}]", :symbolize_names => true)[0]
  rescue JSON::ParserError => e
    raise e unless %w[String Date Time].include?(return_type)

    data = body
  end

  convert_to_type(data, return_type)
end

#object_to_hash(obj) ⇒ String

Convert object(non-array) to hash.

Parameters:

  • obj (Object) —

    object to be converted into JSON string

Returns:

  • (String) —

    JSON string representation of the object



137
138
139
140
141
142
143
144
145
# File 'lib/algolia/api_client.rb', line 137

def object_to_hash(obj)
  if obj.is_a?(Hash)
    obj.to_h { |k, v| [k, object_to_hash(v)] }
  elsif obj.respond_to?(:to_hash)
    obj.to_hash
  else
    obj
  end
end

#object_to_http_body(model) ⇒ String

Convert object (array, hash, object, etc) to JSON string.

Parameters:

  • model (Object) —

    object to be converted into JSON string

Returns:

  • (String) —

    JSON string representation of the object



121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/algolia/api_client.rb', line 121

def object_to_http_body(model)
  return "{}" if model.nil?
  return model if model.is_a?(String)

  body = if model.is_a?(Array)
    model.map { |m| object_to_hash(m) }
  else
    object_to_hash(model)
  end

  body.to_json
end

#request_id_enabled? ⇒ true, false

Whether the transport mints Request-ID headers, resolved per request.

Returns:

  • (true, false)


27
28
29
# File 'lib/algolia/api_client.rb', line 27

def request_id_enabled?
  @transporter.request_id_enabled?
end

#set_client_api_key(api_key) ⇒ Object



35
36
37
# File 'lib/algolia/api_client.rb', line 35

def set_client_api_key(api_key)
  @config.set_client_api_key(api_key)
end