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) ⇒ ApiClient

Initializes the ApiClient

Parameters:

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

    a customizable set of options

Options Hash (config):

  • Configuration (Configuration)

    for initializing the object, default to Configuration.default



16
17
18
19
20
21
# File 'lib/algolia/api_client.rb', line 16

def initialize(config = Configuration.default)
  @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)
end

Instance Attribute Details

#configObject

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

#loggerObject (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

#transporterObject

Returns the value of attribute transporter.



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

def transporter
  @transporter
end

Class Method Details

.defaultObject



23
24
25
# File 'lib/algolia/api_client.rb', line 23

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



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/algolia/api_client.rb', line 141

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:



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/algolia/api_client.rb', line 34

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



72
73
74
75
76
77
78
79
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
# File 'lib/algolia/api_client.rb', line 72

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>"



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/algolia/api_client.rb', line 51

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



129
130
131
132
133
134
135
136
137
# File 'lib/algolia/api_client.rb', line 129

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



113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/algolia/api_client.rb', line 113

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

#set_client_api_key(api_key) ⇒ Object



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

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