Class: TddiumClient::InternalClient

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

Direct Known Subclasses

Client

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = nil, scheme = 'https', version = 1, caller_version = nil, options = {}) ⇒ InternalClient

Returns a new instance of InternalClient.



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/tddium_client.rb', line 129

def initialize(host, port=nil, scheme='https', version=1, caller_version=nil, options={})
@debug = ENV['SOLANO_CLIENT_DEBUG']=='true' || false
@client = HTTPClient.new
if options[:insecure]
  @client.ssl_config.verify_mode = OpenSSL::SSL::VERIFY_NONE
end

uri = URI.parse("")
uri.host = host
uri.port = port
uri.scheme = scheme

options[:cookies].each do |name,value|
    cookie = WebAgent::Cookie.new
    cookie.name = name
    cookie.value = value
    cookie.url = uri

    @client.cookie_manager.add(cookie)
end

if options[:receive_timeout] then
  @client.receive_timeout = options[:receive_timeout].to_i
end

  
@tddium_config = {"host" => host,
                  "port" => port,
                  "scheme" => scheme,
                  "version" => version,
                  "caller_version" => caller_version}
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



127
128
129
# File 'lib/tddium_client.rb', line 127

def client
  @client
end

Instance Method Details

#call_api(method, api_path, params = {}, api_key = nil, retries = 5, xid = nil) ⇒ Object

Raises:



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/tddium_client.rb', line 162

def call_api(method, api_path, params = {}, api_key = nil, retries = 5, xid=nil)
  headers = {'Content-Type' => 'application/json'}
  headers.merge!(API_KEY_HEADER => api_key) if api_key
  headers.merge!(CLIENT_VERSION_HEADER => version_header)

  xid ||= xid_gen
  call_params = params.merge({:xid => xid})

  tries = 0
  debug_msg({:method => method, :path => tddium_uri(api_path), :headers => headers, :body => call_params.to_json})
  begin
    http = @client.send(method, tddium_uri(api_path), :body => call_params.to_json, :header => headers)
  rescue *ERRORS => e
    raise Error::APICert.new(e) if e.message =~ /certificate verify failed/
    tries += 1
    delay = (tries>>1)*0.05*rand()
    Kernel.sleep(delay)
    retry if retries > 0 && tries <= retries
  end

  raise Error::Timeout if retries >= 0 && tries > retries
  res = Result::API.new(http)
  debug_msg({:result => res.tddium_response})
  return res
end

#caller_versionObject



188
189
190
# File 'lib/tddium_client.rb', line 188

def caller_version
  @tddium_config["caller_version"]
end

#caller_version=(version) ⇒ Object



192
193
194
# File 'lib/tddium_client.rb', line 192

def caller_version=(version)
  @tddium_config["caller_version"] = version
end

#xid_genObject



196
197
198
# File 'lib/tddium_client.rb', line 196

def xid_gen
  return SecureRandom.hex(8)
end